/** * Copyright 2017-2018 ZTE Corporation. *
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.holmes.rulemgt;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.CommonUtils;
import org.onap.holmes.common.utils.MsbRegister;
import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
import org.onap.msb.sdk.discovery.entity.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.onap.holmes.common.config.MicroServiceConfig.POD_IP;
import static org.onap.holmes.common.utils.CommonUtils.getEnv;
import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
@Service
public class Initializer {
private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
private volatile static boolean readyForMsbReg = false;
private MsbRegister msbRegister;
@Inject
public Initializer(MsbRegister msbRegister) {
this.msbRegister = msbRegister;
}
@PostConstruct
private void init() {
Executors.newSingleThreadExecutor().execute(() -> {
waitUntilReady();
try {
msbRegister.register2Msb(createMicroServiceInfo());
} catch (CorrelationException e) {
logger.error(e.getMessage(), e);
}
});
}
private void waitUntilReady() {
int count = 1;
while (!readyForMsbReg) {
if (count > 20) {
break;
}
int interval = 5 * count++;
logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
try {
TimeUnit.SECONDS.sleep(interval);
} catch (InterruptedException e) {
logger.info(e.getMessage(), e);
}
}
}
public static void setReadyForMsbReg(boolean readyForMsbReg) {
Initializer.readyForMsbReg = readyForMsbReg;
}
private MicroServiceInfo createMicroServiceInfo() {
String[] serviceIpAndPort = MicroServiceConfig.getMicroServiceIpAndPort();
MicroServiceInfo msinfo = new MicroServiceInfo();
msinfo.setServiceName("holmes-rule-mgmt");
msinfo.setVersion("v1");
msinfo.setUrl("/api/holmes-rule-mgmt/v1");
msinfo.setPath("/api/holmes-rule-mgmt/v1");
msinfo.setProtocol("REST");
msinfo.setVisualRange("0|1");
msinfo.setLb_policy("round-robin");
msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled());
Set