From: lizi00164331 Date: Mon, 4 Sep 2017 07:59:43 +0000 (+0800) Subject: Register esr-server to MSB with java-sdk X-Git-Tag: v1.0.0~92 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fesr-server.git;a=commitdiff_plain;h=efaaa2f9abb12a7154b139ef7c160c7c5d0852a8 Register esr-server to MSB with java-sdk register esr-server to MSB with java-sdk, remove the unused file. Change-Id: Ida2bccd06db36430850fc1610ce2bd62b25e7cb6 Issue-ID: MSB-44 Signed-off-by: lizi00164331 --- diff --git a/esr-core/distribution/standalone/src/main/assembly/conf/extsys.yml b/esr-core/distribution/standalone/src/main/assembly/conf/extsys.yml index b7a6c69..a66ccc1 100644 --- a/esr-core/distribution/standalone/src/main/assembly/conf/extsys.yml +++ b/esr-core/distribution/standalone/src/main/assembly/conf/extsys.yml @@ -19,6 +19,8 @@ template: Hello, %s! defaultName: ${DW_DEFAULT_NAME:-Stranger} # the micro service ip serviceIp: +msbIp: 127.0.0.1 +msbPort: 10081 msbServerAddr: http://127.0.0.1:80 # use the simple server factory if you only want to run on a single port diff --git a/esr-core/esr-mgr/dependency-reduced-pom.xml b/esr-core/esr-mgr/dependency-reduced-pom.xml index 1465618..92b69ff 100644 --- a/esr-core/esr-mgr/dependency-reduced-pom.xml +++ b/esr-core/esr-mgr/dependency-reduced-pom.xml @@ -1,22 +1,4 @@ - - esr-core-parent diff --git a/esr-core/esr-mgr/pom.xml b/esr-core/esr-mgr/pom.xml index 13a5fb6..37d529a 100644 --- a/esr-core/esr-mgr/pom.xml +++ b/esr-core/esr-mgr/pom.xml @@ -191,6 +191,11 @@ ant test - + + + org.onap.msb.java-sdk + msb-java-sdk + 1.0.0-SNAPSHOT + diff --git a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysApp.java b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysApp.java index 46595c4..6552d95 100644 --- a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysApp.java +++ b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysApp.java @@ -18,8 +18,6 @@ package org.onap.aai.esr; import com.fasterxml.jackson.annotation.JsonInclude; import io.dropwizard.Application; import io.dropwizard.assets.AssetsBundle; -import io.dropwizard.db.DataSourceFactory; -import io.dropwizard.hibernate.HibernateBundle; import io.dropwizard.server.SimpleServerFactory; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; @@ -28,12 +26,8 @@ import io.swagger.jaxrs.listing.ApiListingResource; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.onap.aai.esr.common.Config; -import org.onap.aai.esr.common.ServiceRegistrer; -import org.onap.aai.esr.entity.aai.BaseData; -import org.onap.aai.esr.entity.aai.EmsData; -import org.onap.aai.esr.entity.aai.SdncData; -import org.onap.aai.esr.entity.aai.VimData; -import org.onap.aai.esr.entity.aai.VnfmData; +import org.onap.aai.esr.externalservice.msb.MsbHelper; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,21 +50,25 @@ public class ExtsysApp extends Application { bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc")); } - private void initService() { - Thread registerExtsysService = new Thread(new ServiceRegistrer()); - registerExtsysService.setName("register extsys service to Microservice Bus"); - registerExtsysService.start(); - } - - @Override public void run(ExtsysAppConfiguration configuration, Environment environment) { - LOGGER.info("Start to initialize extsys."); + LOGGER.info("Start to initialize esr."); environment.jersey().packages("org.onap.aai.esr.resource"); environment.jersey().register(MultiPartFeature.class); + String MSB_IP=configuration.getMsbIp(); + int MSB_Port=configuration.getMsbPort(); + initSwaggerConfig(environment, configuration); Config.setConfigration(configuration); - initService(); + MSBServiceClient msbClient = new MSBServiceClient(MSB_IP, MSB_Port); + + MsbHelper helper = new MsbHelper(msbClient); + try { + helper.registerMsb(); + } catch (Exception e) { + LOGGER.error("Register ESR to msb failed", e); + } + LOGGER.info("Initialize extsys finished."); } diff --git a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysAppConfiguration.java b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysAppConfiguration.java index 5e3bdf6..862c6a5 100644 --- a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysAppConfiguration.java +++ b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/ExtsysAppConfiguration.java @@ -31,27 +31,20 @@ public class ExtsysAppConfiguration extends Configuration { private String template; @NotEmpty - private String defaultName = "OPENO-Extsys"; + private String defaultName = "ONAP-A&AI-ESR"; + @NotEmpty + private String msbIp; + + @NotEmpty + private int msbPort; + @NotEmpty private String msbServerAddr; + @Valid private String serviceIp; - - @Valid - @NotNull - private DataSourceFactory database = new DataSourceFactory(); - - @JsonProperty("database") - public DataSourceFactory getDataSourceFactory() { - return database; - } - - @JsonProperty("database") - public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { - this.database = dataSourceFactory; - } - + @JsonProperty public String getTemplate() { return template; @@ -71,6 +64,26 @@ public class ExtsysAppConfiguration extends Configuration { public void setDefaultName(String name) { this.defaultName = name; } + + @JsonProperty + public String getMsbIp() { + return msbIp; + } + + @JsonProperty + public String setMsbIp() { + return msbIp; + } + + @JsonProperty + public int getMsbPort() { + return msbPort; + } + + @JsonProperty + public int setMsbPort() { + return msbPort; + } @JsonProperty public String getMsbServerAddr() { diff --git a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/common/ServiceRegistrer.java b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/common/ServiceRegistrer.java deleted file mode 100644 index 867a203..0000000 --- a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/common/ServiceRegistrer.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright 2016-2017 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.aai.esr.common; - -import org.onap.aai.esr.externalservice.entity.ServiceRegisterEntity; -import org.onap.aai.esr.externalservice.msb.MicroserviceBusConsumer; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Parameters.
- * - * @author sun qi - * @version ESR V1 - */ -public class ServiceRegistrer implements Runnable { - private final ServiceRegisterEntity extsysEntity = new ServiceRegisterEntity(); - private static final Logger LOG = LoggerFactory.getLogger(ServiceRegistrer.class); - - public ServiceRegistrer() { - initServiceEntity(); - } - - @Override - public void run() { - LOG.info("start extsys microservice register"); - boolean flag = false; - int retry = 0; - while (!flag && retry < 1000) { - LOG.info("extsys microservice register.retry:" + retry); - retry++; - flag = MicroserviceBusConsumer.registerService(extsysEntity); - if (flag == false) { - LOG.warn("microservice register failed, sleep 30S and try again."); - threadSleep(30000); - } else { - LOG.info("microservice register success!"); - break; - } - } - LOG.info("extsys microservice register end."); - } - - private void threadSleep(int second) { - LOG.info("start sleep ...."); - try { - Thread.sleep(second); - } catch (InterruptedException error) { - LOG.error("thread sleep error.errorMsg:" + error.getMessage()); - } - LOG.info("sleep end ."); - } - - private void initServiceEntity() { - extsysEntity.setServiceName("extsys"); - extsysEntity.setProtocol("REST"); - extsysEntity.setVersion("v1"); - extsysEntity.setUrl("/onapapi/aai/esr/v1"); - extsysEntity.setSingleNode(Config.getConfigration().getServiceIp(), "8100", 0); - extsysEntity.setVisualRange("1"); - } -} diff --git a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/AaiRest.java b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/AaiRest.java deleted file mode 100644 index e4b7f92..0000000 --- a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/aai/AaiRest.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2017 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.aai.esr.externalservice.aai; - -import javax.ws.rs.Consumes; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; - -import org.onap.aai.esr.externalservice.entity.ServiceRegisterEntity; - -@Path("/aai/v11") -public interface AaiRest { - -@Path("") -@POST -@Consumes(MediaType.APPLICATION_JSON) -@Produces(MediaType.APPLICATION_JSON) -public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate, - ServiceRegisterEntity entity)throws Exception; -} \ No newline at end of file diff --git a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/msb/MsbHelper.java b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/msb/MsbHelper.java new file mode 100644 index 0000000..f385350 --- /dev/null +++ b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/externalservice/msb/MsbHelper.java @@ -0,0 +1,56 @@ +/** + * Copyright 2017 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.aai.esr.externalservice.msb; + +import java.net.InetAddress; +import java.util.HashSet; +import java.util.Set; + +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.onap.msb.sdk.discovery.entity.Node; + +public class MsbHelper { + + private MSBServiceClient msbClient; + + public MsbHelper(MSBServiceClient msbClient) { + super(); + this.msbClient = msbClient; + } + + + + public void registerMsb() throws Exception { + + + MicroServiceInfo msinfo = new MicroServiceInfo(); + + msinfo.setServiceName("animals"); + msinfo.setVersion("v1"); + msinfo.setUrl("/api/rpc/v1"); + msinfo.setProtocol("REST"); + msinfo.setVisualRange("0|1"); + + Set nodes = new HashSet<>(); + Node node1 = new Node(); + node1.setIp(InetAddress.getLocalHost().getHostAddress()); + node1.setPort("9090"); + nodes.add(node1); + msinfo.setNodes(nodes); + msbClient.registerMicroServiceInfo(msinfo, false); + } +} diff --git a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/util/HqlFactory.java b/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/util/HqlFactory.java deleted file mode 100644 index 06ba344..0000000 --- a/esr-core/esr-mgr/src/main/java/org/onap/aai/esr/util/HqlFactory.java +++ /dev/null @@ -1,164 +0,0 @@ -/** - * Copyright 2016-2017 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.aai.esr.util; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Arrays; - -public class HqlFactory { - - private static final Logger logger = LoggerFactory.getLogger(HqlFactory.class); - - /** - * get update hql. - * @param obj the object that used to be generate the hql - * @param excludeProperties the properties that need not to be used - * @param filter the condition after "where" - * @return hibernate hql - */ - public static String getUpdateHql(Object obj, String[] excludeProperties, String filter) { - StringBuffer hql = new StringBuffer(); - String objName = obj.getClass().getSimpleName(); - hql.append("update "); - hql.append(objName); - hql.append(" set "); - Field[] fields = obj.getClass().getDeclaredFields(); - if (obj.getClass().getGenericSuperclass() != null) { - Field[] parentFields = obj.getClass().getSuperclass().getDeclaredFields(); - fields = concat(fields, parentFields); - } - for (Field field : fields) { - String name = field.getName(); - Method method = null; - Object value = null; - if (!contain(excludeProperties, name)) { - String upperName = name.substring(0, 1).toUpperCase() + name.substring(1); - try { - method = obj.getClass().getMethod("get" + upperName); - value = method.invoke(obj); - if (value != null) { - if (value instanceof String) { - hql.append(name); - hql.append("="); - hql.append("'"); - hql.append(value); - hql.append("'"); - hql.append(","); - } else { - hql.append(name); - hql.append("="); - hql.append(value); - hql.append(","); - } - } - } catch (Exception error) { - logger.error("error while creating update hql", error); - } - } - } - - String sql = hql.toString(); - sql = sql.substring(0, sql.lastIndexOf(",")); - if (filter != null) { - sql = sql + " where " + filter; - } - logger.info("update hql is : " + sql); - return sql; - } - - /** - * identify whether or not to include target string in source string. - */ - public static boolean contain(String[] src, String target) { - if (src == null || src.length == 0 || target == null) { - return false; - } else { - for (String str : src) { - if (str.equals(target)) { - return true; - } - } - } - return false; - } - - /** - * concat str. - */ - public static T[] concat(T[] first, T[] second) { - T[] result = Arrays.copyOf(first, first.length + second.length); - System.arraycopy(second, 0, result, first.length, second.length); - return result; - } - - public static String getOidFilter(String key, String value) { - return key + "= '" + value + "'"; - } - - /** - * get query hql. - */ - public static String getQueryHql(Object data, String column) { - StringBuffer hql = new StringBuffer(); - String objName = data.getClass().getSimpleName(); - hql.append("select q."); - hql.append(column); - hql.append(" from "); - hql.append(objName); - hql.append(" as q where "); - Field[] fields = data.getClass().getDeclaredFields(); - if (data.getClass().getGenericSuperclass() != null) { - Field[] parentFields = data.getClass().getSuperclass().getDeclaredFields(); - fields = concat(fields, parentFields); - } - for (Field field : fields) { - String name = field.getName(); - Method method = null; - Object value = null; - String upperName = name.substring(0, 1).toUpperCase() + name.substring(1); - try { - method = data.getClass().getMethod("get" + upperName); - value = method.invoke(data); - if (value != null) { - if (value instanceof String) { - hql.append("q." + name); - hql.append("="); - hql.append("'"); - hql.append(value); - hql.append("'"); - hql.append(" and "); - } else { - hql.append("q." + name); - hql.append("="); - hql.append(value); - hql.append("and "); - } - } - } catch (Exception e) { - logger.error("error while creating update hql", e); - } - } - String sql = hql.toString(); - sql = sql.substring(0, sql.lastIndexOf("and")); - - logger.info("query hql is : " + sql); - return sql.trim(); - } -}