From: huangjian Date: Mon, 29 Aug 2016 05:52:39 +0000 (+0800) Subject: TOSCA-47 Automatically registered to the MSB X-Git-Tag: release/sun~69 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=34e5f44d9c170af67b1b685af339f21119affd31;p=vfc%2Fnfvo%2Fcatalog.git TOSCA-47 Automatically registered to the MSB Change-Id: I32fa81e5dc7e3fbac511712853c8219f06b70ce3 Issue-id: TOSCA-47 Signed-off-by: huangjian --- diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java index 0b97867d..903a0a2e 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java @@ -34,6 +34,7 @@ import org.eclipse.jetty.servlets.CrossOriginFilter; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.openo.commontosca.catalog.common.Config; import org.openo.commontosca.catalog.common.MsbAddrConfig; +import org.openo.commontosca.catalog.common.ServiceRegistrer; import org.openo.commontosca.catalog.db.entity.ServiceTemplateData; import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData; import org.openo.commontosca.catalog.health.ConsoleHealthCheck; @@ -114,6 +115,7 @@ public class CatalogApp extends Application { initSwaggerConfig(environment, configuration); initCometd(environment); Config.setConfigration(configuration); + initService(); LOGGER.info("Initialize catalogue finished."); } @@ -143,7 +145,12 @@ public class CatalogApp extends Application { config.setBasePath(basePath); config.setScan(true); } - + private void initService() + { + Thread registerCatalogService=new Thread(new ServiceRegistrer()); + registerCatalogService.setName("register catalog service to Microservice Bus"); + registerCatalogService.start(); + } /** * initialize cometd server. * diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ServiceRegistrer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ServiceRegistrer.java new file mode 100644 index 00000000..ac85a39d --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ServiceRegistrer.java @@ -0,0 +1,98 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * 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.openo.commontosca.catalog.common; + + + +import java.util.ArrayList; +import java.util.Iterator; + +import org.openo.commontosca.catalog.externalservice.msb.MicroserviceBusConsumer; +import org.openo.commontosca.catalog.externalservice.msb.ServiceRegisterEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author 10159474 + * + */ +public class ServiceRegistrer implements Runnable { + private final ArrayList serviceEntityList = + new ArrayList(); + private static final Logger LOG = LoggerFactory.getLogger(ServiceRegistrer.class); + + public ServiceRegistrer() { + initServiceEntity(); + } + + @Override + public void run() { + LOG.info("start microservice register"); + boolean flag = false; + ServiceRegisterEntity entity = new ServiceRegisterEntity(); + int retry = 0; + while (retry < 1000 && serviceEntityList.size() > 0) { + Iterator it = serviceEntityList.iterator(); + while (it.hasNext()) { + entity = it.next(); + LOG.info("start" + entity.getServiceName() + " catalog microservice register.retry:" + + retry); + flag = MicroserviceBusConsumer.registerService(entity); + if (flag == false) { + LOG.warn(entity.getServiceName() + + " microservice register failed, sleep 30S and try again."); + ThreadSleep(30000); + } else { + LOG.info(entity.getServiceName() + " microservice register success!"); + it.remove(); + } + } + retry++; + + } + LOG.info("catalog microservice register end."); + + } + + private void ThreadSleep(int second) { + LOG.info("start sleep ...."); + try { + Thread.sleep(second); + } catch (InterruptedException e) { + LOG.error("thread sleep error.errorMsg:" + e.getMessage()); + } + LOG.info("sleep end ."); + } + + private void initServiceEntity() { + ServiceRegisterEntity catalogEntity = new ServiceRegisterEntity(); + catalogEntity.setServiceName("catalog"); + catalogEntity.setProtocol("REST"); + catalogEntity.setVersion("v1"); + catalogEntity.setUrl("/openoapi/catalog/v1"); + catalogEntity.setSingleNode(null, "8200", 0); + catalogEntity.setVisualRange("1"); + serviceEntityList.add(catalogEntity); + ServiceRegisterEntity httpServiceEntity = new ServiceRegisterEntity(); + httpServiceEntity.setServiceName("/files/catalog-http"); + httpServiceEntity.setProtocol("REST"); + httpServiceEntity.setVersion("v1"); + httpServiceEntity.setUrl("/"); + httpServiceEntity.setSingleNode(null, "8201", 0); + httpServiceEntity.setVisualRange("1"); + serviceEntityList.add(httpServiceEntity); + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java new file mode 100644 index 00000000..5e0acf4d --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java @@ -0,0 +1,45 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * 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.openo.commontosca.catalog.externalservice.msb; + + +import org.glassfish.jersey.client.ClientConfig; +import org.openo.commontosca.catalog.common.Config; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.eclipsesource.jaxrs.consumer.ConsumerFactory; + +/** + * @author 10159474 + * + */ +public class MicroserviceBusConsumer { + private static final Logger LOG = LoggerFactory.getLogger(MicroserviceBusConsumer.class); + + public static boolean registerService(ServiceRegisterEntity entity) { + ClientConfig config = new ClientConfig(); + try { + MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer( + Config.getConfigration().getMsbServerAddr(),config, MicroserviceBusRest.class); + resourceserviceproxy.registerServce("false", entity); + } catch (Exception e) { + LOG.error("microservice register failed!" + e.getMessage()); + return false; + } + return true; + } +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java new file mode 100644 index 00000000..3afe2220 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java @@ -0,0 +1,38 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * 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.openo.commontosca.catalog.externalservice.msb; + +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; + + +/** + * @author 10159474 + * + */ +@Path("/openoapi/microservices/v1/services") +//@Path("/api/microservices/v1/services") +public interface MicroserviceBusRest { + @Path("") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,ServiceRegisterEntity entity)throws Exception; +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceNode.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceNode.java new file mode 100644 index 00000000..90178942 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceNode.java @@ -0,0 +1,37 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * 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.openo.commontosca.catalog.externalservice.msb; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author 10159474 + * + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class ServiceNode { + private String ip; + private String port; + private int ttl; +} diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceRegisterEntity.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceRegisterEntity.java new file mode 100644 index 00000000..95a1d300 --- /dev/null +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceRegisterEntity.java @@ -0,0 +1,51 @@ +/** + * Copyright 2016 [ZTE] and others. + * + * 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.openo.commontosca.catalog.externalservice.msb; + +import java.util.ArrayList; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author 10159474 + * + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class ServiceRegisterEntity { + private String serviceName; + private String version; + private String url; + private String protocol; + private String visualRange; + private ArrayList nodes = new ArrayList(); + + public void setSingleNode(String ip, String port, int ttl) { + ServiceNode node = new ServiceNode(); + node.setIp(ip); + node.setPort(port); + node.setTtl(ttl); + nodes.add(node); + } + +} +