TOSCA-159 Add serviceIp property which is used to config catalog microservice ipaddress
[vfc/nfvo/catalog.git] / catalog-core / catalog-mgr / src / main / java / org / openo / commontosca / catalog / common / ServiceRegistrer.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.commontosca.catalog.common;
17
18 import org.openo.commontosca.catalog.common.Config;
19 import org.openo.commontosca.catalog.externalservice.msb.MicroserviceBusConsumer;
20 import org.openo.commontosca.catalog.externalservice.msb.ServiceRegisterEntity;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26
27 public class ServiceRegistrer implements Runnable {
28   private final ArrayList<ServiceRegisterEntity> serviceEntityList =
29       new ArrayList<ServiceRegisterEntity>();
30   private static final Logger LOG = LoggerFactory.getLogger(ServiceRegistrer.class);
31
32   public ServiceRegistrer() {
33     initServiceEntity();
34   }
35
36   @Override
37   public void run() {
38     LOG.info("start  microservice register");
39     boolean flag = false;
40     ServiceRegisterEntity entity = new ServiceRegisterEntity();
41     int retry = 0;
42     while (retry < 1000 && serviceEntityList.size() > 0) {
43       Iterator<ServiceRegisterEntity> it = serviceEntityList.iterator();
44       while (it.hasNext()) {
45         entity = it.next();
46         LOG.info("start" + entity.getServiceName() + " catalog microservice register.retry:"
47             + retry);
48         flag = MicroserviceBusConsumer.registerService(entity);
49         if (flag == false) {
50           LOG.warn(entity.getServiceName()
51               + " microservice register failed, sleep 30S and try again.");
52           threadSleep(30000);
53         } else {
54           LOG.info(entity.getServiceName() + " microservice register success!");
55           it.remove();
56         }
57       }
58       retry++;
59
60     }
61     LOG.info("catalog microservice register end.");
62
63   }
64
65   /**
66    * sleep thread.
67    * @param second sleep second
68    */
69   private void threadSleep(int seconds) {
70     LOG.info("start sleep ....");
71     try {
72       Thread.sleep(seconds);
73     } catch (InterruptedException e1) {
74       LOG.error("thread sleep error.errorMsg:" + e1.getMessage());
75     }
76     LOG.info("sleep end .");
77   }
78
79   private void initServiceEntity() {
80     ServiceRegisterEntity catalogEntity = new ServiceRegisterEntity();
81     catalogEntity.setServiceName("catalog");
82     catalogEntity.setProtocol("REST");
83     catalogEntity.setVersion("v1");
84     catalogEntity.setUrl("/openoapi/catalog/v1");
85     catalogEntity.setSingleNode(Config.getConfigration().getServiceIp(), "8200", 0);
86     catalogEntity.setVisualRange("1");
87     serviceEntityList.add(catalogEntity);
88     ServiceRegisterEntity httpServiceEntity = new ServiceRegisterEntity();
89     httpServiceEntity.setServiceName("/files/catalog-http");
90     httpServiceEntity.setProtocol("REST");
91     httpServiceEntity.setVersion("v1");
92     httpServiceEntity.setUrl("/");
93     httpServiceEntity.setSingleNode(Config.getConfigration().getServiceIp(), "8201", 0);
94     httpServiceEntity.setVisualRange("1");
95     serviceEntityList.add(httpServiceEntity);
96   }
97 }