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