Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / 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
17 package org.onap.aai.esr.common;
18
19 import org.onap.aai.esr.externalservice.entity.ServiceRegisterEntity;
20 import org.onap.aai.esr.externalservice.msb.MicroserviceBusConsumer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Parameters. <br/>
26  * 
27  * @author sun qi
28  * @version ESR V1
29  */
30 public class ServiceRegistrer implements Runnable {
31   private final ServiceRegisterEntity extsysEntity = new ServiceRegisterEntity();
32   private static final Logger LOG = LoggerFactory.getLogger(ServiceRegistrer.class);
33
34   public ServiceRegistrer() {
35     initServiceEntity();
36   }
37
38   @Override
39   public void run() {
40     LOG.info("start extsys microservice register");
41     boolean flag = false;
42     int retry = 0;
43     while (!flag && retry < 1000) {
44       LOG.info("extsys microservice register.retry:" + retry);
45       retry++;
46       flag = MicroserviceBusConsumer.registerService(extsysEntity);
47       if (flag == false) {
48         LOG.warn("microservice register failed, sleep 30S and try again.");
49         threadSleep(30000);
50       } else {
51         LOG.info("microservice register success!");
52         break;
53       }
54     }
55     LOG.info("extsys microservice register end.");
56   }
57
58   private void threadSleep(int second) {
59     LOG.info("start sleep ....");
60     try {
61       Thread.sleep(second);
62     } catch (InterruptedException error) {
63       LOG.error("thread sleep error.errorMsg:" + error.getMessage());
64     }
65     LOG.info("sleep end .");
66   }
67
68   private void initServiceEntity() {
69     extsysEntity.setServiceName("extsys");
70     extsysEntity.setProtocol("REST");
71     extsysEntity.setVersion("v1");
72     extsysEntity.setUrl("/openoapi/extsys/v1");
73     extsysEntity.setSingleNode(Config.getConfigration().getServiceIp(), "8100", 0);
74     extsysEntity.setVisualRange("1");
75   }
76 }