TOSCA-182 Catalog service does not relay on other services when it start
authorsunqi310 <sun.qi310@zte.com.cn>
Fri, 18 Nov 2016 07:56:53 +0000 (15:56 +0800)
committersunqi310 <sun.qi310@zte.com.cn>
Fri, 18 Nov 2016 07:56:53 +0000 (15:56 +0800)
Change-Id: I2d37b43f2905c7259196afd7f9b11df32be3ddf3
Issue-id: TOSCA-182
Signed-off-by: sunqi310 <sun.qi310@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/db/dao/DaoManager.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java [new file with mode: 0644]
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusConsumer.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/MicroserviceBusRest.java
catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java [new file with mode: 0644]
catalog-core/httpserver/src/main/assembly/tomcat/conf/web.xml

index 88e3b16..1528edc 100644 (file)
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.openo.commontosca.catalog;
 
 import com.fasterxml.jackson.annotation.JsonInclude;
@@ -28,10 +29,6 @@ import io.dropwizard.setup.Environment;
 import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.jaxrs.listing.ApiListingResource;
 
-import java.util.EnumSet;
-
-import javax.servlet.DispatcherType;
-
 import org.eclipse.jetty.servlets.CrossOriginFilter;
 import org.glassfish.jersey.media.multipart.MultiPartFeature;
 import org.openo.commontosca.catalog.common.Config;
@@ -44,6 +41,7 @@ import org.openo.commontosca.catalog.db.entity.NodeTemplateData;
 import org.openo.commontosca.catalog.db.entity.PackageData;
 import org.openo.commontosca.catalog.db.entity.ServiceTemplateData;
 import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData;
+import org.openo.commontosca.catalog.db.hibernate.HibernateBundleAgent;
 import org.openo.commontosca.catalog.health.ConsoleHealthCheck;
 import org.openo.commontosca.catalog.resources.PackageResource;
 import org.openo.commontosca.catalog.resources.TemplateResource;
@@ -51,6 +49,11 @@ import org.openo.commontosca.catalog.resources.TemplateResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.EnumSet;
+import javax.servlet.DispatcherType;
+
+
+
 
 public class CatalogApp extends Application<CatalogAppConfiguration> {
 
@@ -64,25 +67,16 @@ public class CatalogApp extends Application<CatalogAppConfiguration> {
   public String getName() {
     return "OPENO-Catalog";
   }
-
-  private final HibernateBundle<CatalogAppConfiguration> bundle =
-      new HibernateBundle<CatalogAppConfiguration>(ServiceTemplateData.class, PackageData.class,
-          NodeTemplateData.class, ServiceTemplateMappingData.class) {
-        @Override
-        public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) {
-          return configuration.getDataSourceFactory();
-        }
-      };
-
+  private final HibernateBundleAgent bundle = new HibernateBundleAgent();
   @Override
   public void initialize(Bootstrap<CatalogAppConfiguration> bootstrap) {
     bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
     initDb(bootstrap);
   }
 
-  private void initDao() {
-    DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory());
-  }
+  //  private void initDao() {
+  //    DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory());
+  //  }
 
   private void initDb(Bootstrap<CatalogAppConfiguration> bootstrap) {
     bootstrap.addBundle(bundle);
@@ -100,7 +94,7 @@ public class CatalogApp extends Application<CatalogAppConfiguration> {
     MsbAddrConfig.setMsbAddress(configuration.getMsbServerAddr());
     HttpServerAddrConfig.setHttpServerAddress(configuration.getHttpServerAddr());
     HttpServerPathConfig.setHttpServerPath(configuration.getHttpServerPath());
-    initDao();
+    //initDao();
     final ConsoleHealthCheck healthCheck = new ConsoleHealthCheck(configuration.getTemplate());
     environment.healthChecks().register("template", healthCheck);
 
index c7d2cc1..cdd1f66 100644 (file)
@@ -16,7 +16,9 @@
 package org.openo.commontosca.catalog.db.dao;
 
 import org.hibernate.SessionFactory;
+
 import org.openo.commontosca.catalog.db.common.CatalogResuorceType;
+import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
 
 /**
  * DAO manager class.
@@ -44,7 +46,10 @@ public class DaoManager {
    * @param type data type
    * @return BaseDao<?>
    */
-  public BaseDao<?> getDao(String type) {
+  public BaseDao<?> getDao(String type) throws CatalogResourceException{
+    if (sessionFactory == null) {
+      throw new CatalogResourceException("errorMsg:database connect init faild!");
+    }
     switch (CatalogResuorceType.getType(type)) {
       case SERVICETEMPLATE:
         return getServiceTemplateDao();
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleAgent.java
new file mode 100644 (file)
index 0000000..43b9333
--- /dev/null
@@ -0,0 +1,91 @@
+/**
+ * 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.db.hibernate;
+
+import io.dropwizard.ConfiguredBundle;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+
+import org.openo.commontosca.catalog.CatalogAppConfiguration;
+import org.openo.commontosca.catalog.db.dao.DaoManager;
+import org.openo.commontosca.catalog.db.entity.NodeTemplateData;
+import org.openo.commontosca.catalog.db.entity.PackageData;
+import org.openo.commontosca.catalog.db.entity.ServiceTemplateData;
+import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HibernateBundleAgent implements ConfiguredBundle<CatalogAppConfiguration> {
+
+  private final HibernateBundleExt bundle = new HibernateBundleExt(ServiceTemplateData.class,
+      PackageData.class, NodeTemplateData.class, ServiceTemplateMappingData.class);
+  private static final Logger LOGGER = LoggerFactory.getLogger(HibernateBundleAgent.class);
+
+  @Override
+  public void run(final CatalogAppConfiguration configuration, final Environment environment)
+      throws Exception {
+    Thread thread = new Thread(new Runnable() {
+      int retry = 0;
+      boolean flag = true;
+
+      public void run() {
+        while (retry < 1000) {
+          LOGGER.info("init hibernateBundle.retry:" + retry);
+          retry++;
+          try {
+            bundle.runExt(configuration, environment);
+          } catch (Exception e1) {
+            flag = false;
+            LOGGER.warn(
+                "init hibernateBundle failed, sleep 15S and try again.errorMsg:" + e1.getMessage());
+            threadSleep(15000);
+          }
+          if (flag) {
+            LOGGER.info("init hibernateBundle success!");
+            initDao();
+            break;
+          }
+        }
+      }
+    });
+    thread.setName("init hibernateBundle");
+    thread.start();
+  }
+
+  private void initDao() {
+    DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory());
+  }
+
+  private void threadSleep(int second) {
+    LOGGER.info("start sleep ....");
+    try {
+      Thread.sleep(second);
+    } catch (InterruptedException error) {
+      LOGGER.error("thread sleep error.errorMsg:" + error.getMessage());
+    }
+    LOGGER.info("sleep end .");
+  }
+
+  @Override
+  public void initialize(Bootstrap<?> bootstrap) {
+    bundle.initializeExt(bootstrap);
+  }
+  //
+  // public SessionFactory getSessionFactory() {
+  // return bundle.getSessionFactory();
+  // }
+}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/db/hibernate/HibernateBundleExt.java
new file mode 100644 (file)
index 0000000..2075949
--- /dev/null
@@ -0,0 +1,87 @@
+/**
+ * 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.db.hibernate;
+
+import com.google.common.collect.ImmutableList;
+
+import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
+
+import io.dropwizard.db.DataSourceFactory;
+import io.dropwizard.hibernate.HibernateBundle;
+import io.dropwizard.hibernate.SessionFactoryFactory;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+
+import org.hibernate.SessionFactory;
+import org.openo.commontosca.catalog.CatalogAppConfiguration;
+
+
+public class HibernateBundleExt extends HibernateBundle<CatalogAppConfiguration> {
+
+  private static final String DEFAULT_NAME = "hibernate";
+
+  private SessionFactory sessionFactory;
+
+  private final ImmutableList<Class<?>> entities;
+  private final SessionFactoryFactory sessionFactoryFactory;
+
+  protected HibernateBundleExt(Class<?> entity, Class<?>... entities) {
+    this(ImmutableList.<Class<?>>builder().add(entity).add(entities).build(),
+        new SessionFactoryFactory());
+  }
+
+  protected HibernateBundleExt(ImmutableList<Class<?>> entities,
+      SessionFactoryFactory sessionFactoryFactory) {
+    super(entities, sessionFactoryFactory);
+    this.entities = entities;
+    this.sessionFactoryFactory = sessionFactoryFactory;
+  }
+
+  public final void initializeExt(Bootstrap<?> bootstrap) {
+    bootstrap.getObjectMapper().registerModule(createHibernate4Module());
+  }
+
+  /**
+   * Override to configure the {@link Hibernate4Module}.
+   */
+  protected Hibernate4Module createHibernate4Module() {
+    return new Hibernate4Module();
+  }
+
+  protected String name() {
+    return DEFAULT_NAME;
+  }
+
+  public final void runExt(CatalogAppConfiguration configuration, Environment environment)
+      throws Exception {
+    final DataSourceFactory dbConfig = getDataSourceFactory(configuration);
+    this.sessionFactory =
+        sessionFactoryFactory.build(this, environment, dbConfig, entities, name());
+
+  }
+
+  public SessionFactory getSessionFactory() {
+    return sessionFactory;
+  }
+
+
+  @Override
+  public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) {
+    return configuration.getDataSourceFactory();
+  }
+
+}
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/ApiRouteInfo.java
new file mode 100644 (file)
index 0000000..d98af25
--- /dev/null
@@ -0,0 +1,106 @@
+/**
+ * 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.io.Serializable;
+
+public class ApiRouteInfo implements Serializable {
+  private static final long serialVersionUID = 1L;
+  private String serviceName;
+  private String version = "";
+  private String url;
+  private String apiJson = "";
+  private String apiJsonType = "1";
+  private String metricsUrl = "";
+  private String control = "0";
+  private String status = "1";
+
+  private RouteServer []servers;
+
+
+  public String getServiceName() {
+    return serviceName;
+  }
+
+  public void setServiceName(String serviceName) {
+    this.serviceName = serviceName;
+  }
+
+  public String getVersion() {
+    return version;
+  }
+
+  public void setVersion(String version) {
+    this.version = version;
+  }
+
+  public String getApiJson() {
+    return apiJson;
+  }
+
+  public void setApiJson(String apiJson) {
+    this.apiJson = apiJson;
+  }
+
+  public String getUrl() {
+    return url;
+  }
+
+  public void setUrl(String url) {
+    this.url = url;
+  }
+
+  public RouteServer[] getServers() {
+    return servers;
+  }
+
+  public void setServers(RouteServer[] servers) {
+    this.servers = servers;
+  }
+
+  public String getApiJsonType() {
+    return apiJsonType;
+  }
+
+  public void setApiJsonType(String apiJsonType) {
+    this.apiJsonType = apiJsonType;
+  }
+
+  public String getMetricsUrl() {
+    return metricsUrl;
+  }
+
+  public void setMetricsUrl(String metricsUrl) {
+    this.metricsUrl = metricsUrl;
+  }
+
+  public String getControl() {
+    return control;
+  }
+
+  public void setControl(String control) {
+    this.control = control;
+  }
+
+  public String getStatus() {
+    return status;
+  }
+
+  public void setStatus(String status) {
+    this.status = status;
+  }
+}
index 33e1700..6732d6e 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.openo.commontosca.catalog.externalservice.msb;
 
 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
 import org.glassfish.jersey.client.ClientConfig;
-import org.openo.commontosca.catalog.db.util.CatalogDbUtil;
+
 import org.openo.commontosca.catalog.common.Config;
+import org.openo.commontosca.catalog.db.util.CatalogDbUtil;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -28,6 +31,7 @@ public class MicroserviceBusConsumer {
 
   /**
    * register service to MSB.
+   * 
    * @param entity ServiceRegisterEntity
    * @return boolean
    */
@@ -35,9 +39,8 @@ public class MicroserviceBusConsumer {
     ClientConfig config = new ClientConfig();
     LOG.info("microservice register body:" + CatalogDbUtil.objectToString(entity));
     try {
-      MicroserviceBusRest resourceserviceproxy =
-          ConsumerFactory.createConsumer(Config.getConfigration().getMsbServerAddr(), config,
-              MicroserviceBusRest.class);
+      MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(
+          Config.getConfigration().getMsbServerAddr(), config, MicroserviceBusRest.class);
       resourceserviceproxy.registerServce("false", entity);
     } catch (Exception e1) {
       LOG.error("microservice register failed!" + e1.getMessage());
@@ -45,4 +48,19 @@ public class MicroserviceBusConsumer {
     }
     return true;
   }
+
+  public static ApiRouteInfo queryApiRouteInfo(String serviceName, String version) {
+    ClientConfig config = new ClientConfig();
+    LOG.info("microservice register body:" + "serviceName:" + serviceName + " version:" + version);
+    ApiRouteInfo apiRouteInfo = null;
+    try {
+      MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(
+          Config.getConfigration().getMsbServerAddr(), config, MicroserviceBusRest.class);
+      apiRouteInfo = resourceserviceproxy.queryApiRouteInfo(serviceName, version);
+    } catch (Exception e1) {
+      LOG.error("query api route failed!" + e1.getMessage());
+
+    }
+    return apiRouteInfo;
+  }
 }
index 11c2349..51a5971 100644 (file)
  * 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.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
@@ -31,4 +34,11 @@ public interface MicroserviceBusRest {
   @Produces(MediaType.APPLICATION_JSON)
   public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,
       ServiceRegisterEntity entity) throws Exception;
+
+  @Path("")
+  @GET
+  @Consumes(MediaType.APPLICATION_JSON)
+  @Produces(MediaType.APPLICATION_JSON)
+  public ApiRouteInfo queryApiRouteInfo(@PathParam("serviceName") String serviceName,
+      @PathParam("version") String version ) throws Exception;
 }
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/externalservice/msb/RouteServer.java
new file mode 100644 (file)
index 0000000..8aa22a0
--- /dev/null
@@ -0,0 +1,53 @@
+/**
+ * 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.io.Serializable;
+
+public class RouteServer implements Serializable {
+  private static final long serialVersionUID = 1L;
+  private String ip;
+  private int weight;
+
+  public String getIp() {
+    return ip;
+  }
+
+  public void setIp(String ip) {
+    this.ip = ip;
+  }
+
+
+
+  public int getWeight() {
+    return weight;
+  }
+
+  public void setWeight(int weight) {
+    this.weight = weight;
+  }
+
+  public RouteServer() {
+
+  }
+
+  public RouteServer(String ip) {
+    this.ip = ip;
+    this.weight = 0;
+  }
+
+}
index acaeaa5..9e4756d 100644 (file)
         </init-param>
         <init-param>
             <param-name>listings</param-name>
-            <param-value>false</param-value>
+            <param-value>true</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>