e801578c3a9edbf1d40c629474d9a3bb3dfb7563
[vfc/nfvo/driver/sfc.git] / zte / sfc-driver / sfc-driver / src / main / java / org / onap / sfc / resources / MsbServiceRegister.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.onap.sfc.resources;
17
18 import lombok.Setter;
19 import org.onap.sfc.utils.SfcDriverUtil;
20 import org.onap.sfc.entity.MsbRegisterEntity;
21 import org.onap.sfc.service.ConfigInfo;
22 import org.onap.sfc.service.SdnServiceConsumer;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class MsbServiceRegister implements Runnable {
27     private final Logger LOGGER = LoggerFactory.getLogger(MsbServiceRegister.class);
28     MsbRegisterEntity entity;
29
30     @Setter
31     private int sleepSeconds = 30000;
32
33     @Setter
34     private int retry = 20;
35
36     public MsbServiceRegister()
37     {
38         initInfo();
39     }
40
41     private void initInfo()
42     {
43         entity = SfcDriverUtil.getMsbRegisterInfo();
44
45     }
46
47     @Override
48     public void run() {
49         boolean flag = false;
50         int retryTimes=0;
51         while (!flag && retryTimes< retry)
52         {
53             try {
54                 LOGGER.info("Register Msb start:");
55                 LOGGER.info(SfcDriverUtil.toJson(entity));
56                 SdnServiceConsumer.getMsbRegisterService(ConfigInfo.getConfig().getMsbServiceUrl()).
57                         registerServce("false",entity);
58                 LOGGER.info("Register Msb end:");
59                 flag = true;
60                 break;
61             } catch (Exception e) {
62                 LOGGER.error("Register Msb failed",e);
63                 //e.printStackTrace();
64                 threadSleep(sleepSeconds);
65                 retryTimes++;
66             }
67         }
68
69     }
70
71     private void threadSleep(int second) {
72         LOGGER.info("start sleep ....");
73         try {
74             Thread.sleep(second);
75         } catch (Exception e) {
76             LOGGER.error("thread sleep error.errorMsg:", e);
77         }
78         LOGGER.info("sleep end .");
79     }
80 }