TOSCA-47 Automatically registered to the MSB
authorhuangjian <huang.jian12@zte.com.cn>
Mon, 29 Aug 2016 05:52:39 +0000 (13:52 +0800)
committerhuangjian <huang.jian12@zte.com.cn>
Mon, 29 Aug 2016 05:52:39 +0000 (13:52 +0800)
Change-Id: I32fa81e5dc7e3fbac511712853c8219f06b70ce3
Issue-id: TOSCA-47
Signed-off-by: huangjian <huang.jian12@zte.com.cn>
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/common/ServiceRegistrer.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceNode.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ServiceRegisterEntity.java [new file with mode: 0644]

index 0b97867..903a0a2 100644 (file)
@@ -34,6 +34,7 @@ import org.eclipse.jetty.servlets.CrossOriginFilter;
 import org.glassfish.jersey.media.multipart.MultiPartFeature;\r
 import org.openo.commontosca.catalog.common.Config;\r
 import org.openo.commontosca.catalog.common.MsbAddrConfig;\r
+import org.openo.commontosca.catalog.common.ServiceRegistrer;\r
 import org.openo.commontosca.catalog.db.entity.ServiceTemplateData;\r
 import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData;\r
 import org.openo.commontosca.catalog.health.ConsoleHealthCheck;\r
@@ -114,6 +115,7 @@ public class CatalogApp extends Application<CatalogAppConfiguration> {
         initSwaggerConfig(environment, configuration);\r
         initCometd(environment);\r
         Config.setConfigration(configuration);\r
+        initService();\r
         LOGGER.info("Initialize catalogue finished.");\r
     }\r
 \r
@@ -143,7 +145,12 @@ public class CatalogApp extends Application<CatalogAppConfiguration> {
         config.setBasePath(basePath);\r
         config.setScan(true);\r
     }\r
-\r
+    private void initService()\r
+    {\r
+        Thread registerCatalogService=new Thread(new ServiceRegistrer());\r
+        registerCatalogService.setName("register catalog service to Microservice Bus");\r
+        registerCatalogService.start();\r
+    }\r
     /**\r
      * initialize cometd server.\r
      * \r
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 (file)
index 0000000..ac85a39
--- /dev/null
@@ -0,0 +1,98 @@
+/**\r
+ * Copyright 2016 [ZTE] and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.commontosca.catalog.common;\r
+\r
+\r
+\r
+import java.util.ArrayList;\r
+import java.util.Iterator;\r
+\r
+import org.openo.commontosca.catalog.externalservice.msb.MicroserviceBusConsumer;\r
+import org.openo.commontosca.catalog.externalservice.msb.ServiceRegisterEntity;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+/**\r
+ * @author 10159474\r
+ *\r
+ */\r
+public class ServiceRegistrer implements Runnable {\r
+    private final ArrayList<ServiceRegisterEntity> serviceEntityList =\r
+            new ArrayList<ServiceRegisterEntity>();\r
+    private static final Logger LOG = LoggerFactory.getLogger(ServiceRegistrer.class);\r
+\r
+    public ServiceRegistrer() {\r
+        initServiceEntity();\r
+    }\r
+\r
+    @Override\r
+    public void run() {\r
+        LOG.info("start  microservice register");\r
+        boolean flag = false;\r
+        ServiceRegisterEntity entity = new ServiceRegisterEntity();\r
+        int retry = 0;\r
+        while (retry < 1000 && serviceEntityList.size() > 0) {\r
+            Iterator<ServiceRegisterEntity> it = serviceEntityList.iterator();\r
+            while (it.hasNext()) {\r
+                entity = it.next();\r
+                LOG.info("start" + entity.getServiceName() + " catalog microservice register.retry:"\r
+                        + retry);\r
+                flag = MicroserviceBusConsumer.registerService(entity);\r
+                if (flag == false) {\r
+                    LOG.warn(entity.getServiceName()\r
+                            + " microservice register failed, sleep 30S and try again.");\r
+                    ThreadSleep(30000);\r
+                } else {\r
+                    LOG.info(entity.getServiceName() + " microservice register success!");\r
+                    it.remove();\r
+                }\r
+            }\r
+            retry++;\r
+\r
+        }\r
+        LOG.info("catalog microservice register end.");\r
+\r
+    }\r
+\r
+    private void ThreadSleep(int second) {\r
+        LOG.info("start sleep ....");\r
+        try {\r
+            Thread.sleep(second);\r
+        } catch (InterruptedException e) {\r
+            LOG.error("thread sleep error.errorMsg:" + e.getMessage());\r
+        }\r
+        LOG.info("sleep end .");\r
+    }\r
+\r
+    private void initServiceEntity() {\r
+        ServiceRegisterEntity catalogEntity = new ServiceRegisterEntity();\r
+        catalogEntity.setServiceName("catalog");\r
+        catalogEntity.setProtocol("REST");\r
+        catalogEntity.setVersion("v1");\r
+        catalogEntity.setUrl("/openoapi/catalog/v1");\r
+        catalogEntity.setSingleNode(null, "8200", 0);\r
+        catalogEntity.setVisualRange("1");\r
+        serviceEntityList.add(catalogEntity);\r
+        ServiceRegisterEntity httpServiceEntity = new ServiceRegisterEntity();\r
+        httpServiceEntity.setServiceName("/files/catalog-http");\r
+        httpServiceEntity.setProtocol("REST");\r
+        httpServiceEntity.setVersion("v1");\r
+        httpServiceEntity.setUrl("/");\r
+        httpServiceEntity.setSingleNode(null, "8201", 0);\r
+        httpServiceEntity.setVisualRange("1");\r
+        serviceEntityList.add(httpServiceEntity);\r
+    }\r
+}\r
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 (file)
index 0000000..5e0acf4
--- /dev/null
@@ -0,0 +1,45 @@
+/**\r
+ * Copyright 2016 [ZTE] and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.commontosca.catalog.externalservice.msb;\r
+\r
+\r
+import org.glassfish.jersey.client.ClientConfig;\r
+import org.openo.commontosca.catalog.common.Config;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
+\r
+/**\r
+ * @author 10159474\r
+ *\r
+ */\r
+public class MicroserviceBusConsumer {\r
+    private static final Logger LOG = LoggerFactory.getLogger(MicroserviceBusConsumer.class);\r
+\r
+    public static boolean registerService(ServiceRegisterEntity entity) {\r
+        ClientConfig config = new ClientConfig();\r
+        try {\r
+            MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
+                    Config.getConfigration().getMsbServerAddr(),config, MicroserviceBusRest.class);\r
+            resourceserviceproxy.registerServce("false", entity);\r
+        } catch (Exception e) {\r
+            LOG.error("microservice register failed!" + e.getMessage());\r
+            return false;\r
+        }\r
+        return true;\r
+    }\r
+}\r
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 (file)
index 0000000..3afe222
--- /dev/null
@@ -0,0 +1,38 @@
+/**\r
+ * Copyright 2016 [ZTE] and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.commontosca.catalog.externalservice.msb;\r
+\r
+import javax.ws.rs.Consumes;\r
+import javax.ws.rs.POST;\r
+import javax.ws.rs.Path;\r
+import javax.ws.rs.Produces;\r
+import javax.ws.rs.QueryParam;\r
+import javax.ws.rs.core.MediaType;\r
+\r
+\r
+/**\r
+ * @author 10159474\r
+ *\r
+ */\r
+@Path("/openoapi/microservices/v1/services")\r
+//@Path("/api/microservices/v1/services")\r
+public interface MicroserviceBusRest {\r
+    @Path("")\r
+    @POST\r
+    @Consumes(MediaType.APPLICATION_JSON)\r
+    @Produces(MediaType.APPLICATION_JSON)\r
+    public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,ServiceRegisterEntity entity)throws Exception;\r
+}\r
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 (file)
index 0000000..9017894
--- /dev/null
@@ -0,0 +1,37 @@
+/**\r
+ * Copyright 2016 [ZTE] and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.commontosca.catalog.externalservice.msb;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+import lombok.AllArgsConstructor;\r
+import lombok.Data;\r
+import lombok.NoArgsConstructor;\r
+\r
+/**\r
+ * @author 10159474\r
+ *\r
+ */\r
+\r
+@Data\r
+@NoArgsConstructor\r
+@AllArgsConstructor\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class ServiceNode {\r
+    private String ip;\r
+    private String port;\r
+    private int ttl;\r
+}\r
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 (file)
index 0000000..95a1d30
--- /dev/null
@@ -0,0 +1,51 @@
+/**\r
+ * Copyright 2016 [ZTE] and others.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.openo.commontosca.catalog.externalservice.msb;\r
+\r
+import java.util.ArrayList;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+import lombok.AllArgsConstructor;\r
+import lombok.Data;\r
+import lombok.NoArgsConstructor;\r
+\r
+/**\r
+ * @author 10159474\r
+ *\r
+ */\r
+@Data\r
+@NoArgsConstructor\r
+@AllArgsConstructor\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class ServiceRegisterEntity {\r
+    private String serviceName;\r
+    private String version;\r
+    private String url;\r
+    private String protocol;\r
+    private String visualRange;\r
+    private ArrayList<ServiceNode> nodes = new ArrayList<ServiceNode>();\r
+\r
+    public void setSingleNode(String ip, String port, int ttl) {\r
+        ServiceNode node = new ServiceNode();\r
+        node.setIp(ip);\r
+        node.setPort(port);\r
+        node.setTtl(ttl);\r
+        nodes.add(node);\r
+    }\r
+\r
+}\r
+\r