diff options
Diffstat (limited to 'holmes-actions/src/main')
4 files changed, 187 insertions, 77 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java index 33bd1d2..7cbbb3e 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java @@ -35,6 +35,8 @@ public class MicroServiceConfig { final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
final static public String DOCKER_HOST = "DOCKER_HOST";
final static public String MSB_ADDR = "MSB_ADDR";
+ final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
+ final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");
final static public String AAI_HOSTNAME = "aai.onap";
@@ -105,7 +107,7 @@ public class MicroServiceConfig { }
public static String[] getMsbIpAndPort() {
- return split(getEnv(MSB_ADDR));
+ return new String[] {getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
}
public static String[] getMicroServiceIpAndPort() {
@@ -124,7 +126,7 @@ public class MicroServiceConfig { return serviceAddrInfo;
}
- private static boolean isIpAddress(String info) {
+ public static boolean isIpAddress(String info) {
return IP_REG.matcher(info).matches();
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java new file mode 100644 index 0000000..2d770ad --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java @@ -0,0 +1,75 @@ +/** + * Copyright 2020 ZTE Corporation. + * <p> + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.common.utils; + +import org.glassfish.jersey.client.ClientConfig; +import org.jvnet.hk2.annotations.Service; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.PostConstruct; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +@Service +public class JerseyClient { + private static Logger logger = LoggerFactory.getLogger(JerseyClient.class); + public static final String PROTOCOL_HTTP = "http"; + public static final String PROTOCOL_HTTPS = "https"; + private SSLContext sslcontext = null; + + @PostConstruct + private void init() { + try { + sslcontext = SSLContext.getInstance("TLS"); + sslcontext.init(null, new TrustManager[]{new X509TrustManager() { + public void checkClientTrusted(X509Certificate[] arg0, String arg1) { + } + + public void checkServerTrusted(X509Certificate[] arg0, String arg1) { + } + + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }}, new java.security.SecureRandom()); + } catch (NoSuchAlgorithmException | KeyManagementException e) { + logger.error("Failed to initialize the SSLContext instance!", e); + } + } + + public Client httpClient() { + return ClientBuilder.newClient(new ClientConfig()); + } + + public Client httpsClient() { + return ClientBuilder.newBuilder() + .sslContext(sslcontext) + .hostnameVerifier((s1, s2) -> true) + .build(); + } + + public Client client(boolean isHttps) { + return isHttps ? httpsClient() : httpClient(); + } +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java deleted file mode 100644 index a849ab8..0000000 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java +++ /dev/null @@ -1,75 +0,0 @@ -/**
- * Copyright 2017-2020 ZTE Corporation.
- * <p>
- * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * 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.common.utils;
-
-import lombok.extern.slf4j.Slf4j;
-import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.config.MicroServiceConfig;
-import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.msb.sdk.discovery.common.RouteException;
-import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
-import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
-import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
-
-@Slf4j
-@Service
-public class MSBRegisterUtil {
-
- public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
- String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
- MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],
- Integer.parseInt(msbAddrInfo[1]));
-
- log.info("Start to register Holmes Service to MSB...");
- MicroServiceFullInfo microServiceFullInfo = null;
- int retry = 0;
- while (null == microServiceFullInfo && retry < 20) {
- log.info("Holmes Service Registration. Retry: " + retry);
- retry++;
- try {
- microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);
- } catch (RouteException e) {
-
- }
-
- if (null == microServiceFullInfo) {
- log.warn("Failed to register the service to MSB. Sleep 30s and try again.");
- threadSleep(30000);
- } else {
- log.info("Registration succeeded!");
- break;
- }
- }
-
- if (null == microServiceFullInfo) {
- throw new CorrelationException("Failed to register the service to MSB!");
- }
-
- log.info("Service registration completed.");
- }
-
- private void threadSleep(int second) {
- log.info("Start sleeping...");
- try {
- Thread.sleep(second);
- } catch (InterruptedException error) {
- log.error("thread sleep error message:" + error.getMessage(), error);
- Thread.currentThread().interrupt();
- }
- log.info("Wake up.");
- }
-}
\ No newline at end of file diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java new file mode 100644 index 0000000..caff931 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java @@ -0,0 +1,108 @@ +/** + * Copyright 2017-2020 ZTE Corporation. + * <p> + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.common.utils; + +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jetty.http.HttpStatus; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.concurrent.TimeUnit; + +import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP; +import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS; + +@Service +public class MsbRegister { + private static final Logger log = LoggerFactory.getLogger(MsbRegister.class); + + private JerseyClient jerseyClient; + + @Inject + public MsbRegister(JerseyClient jerseyClient) { + this.jerseyClient = jerseyClient; + } + + public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException { + String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort(); + boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1]) + && msbAddrInfo[1].equals("443"); + + Client client = jerseyClient.client(isHttpsEnabled); + WebTarget target = client.target(String.format("%s://%s:%s/api/microservices/v1/services", + isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1])); + + log.info("Start to register Holmes Service to MSB..."); + + MicroServiceFullInfo microServiceFullInfo = null; + int retry = 0; + int interval = 5; + while (null == microServiceFullInfo && retry < 20) { + log.info("Holmes Service Registration. Retry: " + retry++); + + Response response = target.queryParam("createOrUpdate", true) + .request(MediaType.APPLICATION_JSON) + .post(Entity.entity(msinfo, MediaType.APPLICATION_JSON)); + + if (response != null) { + String ret = response.readEntity(String.class); + int statusCode = response.getStatus(); + log.info(String.format("=========MSB REG=========\nStatus Code: %d\nInformation: %s", statusCode, ret)); + if (HttpStatus.isSuccess(statusCode)) { + microServiceFullInfo = GsonUtil.jsonToBean(ret, MicroServiceFullInfo.class); + } + } + + if (null == microServiceFullInfo) { + log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", interval)); + threadSleep(TimeUnit.SECONDS.toSeconds(interval)); + interval += 5; + } else { + log.info("Registration succeeded!"); + break; + } + } + + if (null == microServiceFullInfo) { + throw new CorrelationException("Failed to register the service to MSB!"); + } + + log.info("Service registration completed."); + } + + private void threadSleep(long second) { + log.info("Start sleeping..."); + try { + TimeUnit.SECONDS.sleep(second); + } catch (InterruptedException error) { + log.error("thread sleep error message:" + error.getMessage(), error); + Thread.currentThread().interrupt(); + } + log.info("Wake up."); + } +}
\ No newline at end of file |