db986978cd54178c3872616ed0c5e94cb615530c
[vfc/nfvo/driver/sfc.git] / zte / sfc-driver / sfc-driver / src / main / java / org / openo / 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.openo.sfc.resources;
17
18 import com.google.gson.Gson;
19 import org.openo.sfc.entity.MsbRegisterEntity;
20 import org.openo.sfc.service.ConfigInfo;
21 import org.openo.sfc.service.SdnServiceConsumer;
22 import org.openo.sfc.utils.SfcDriverUtil;
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     public MsbServiceRegister()
30     {
31         initInfo();
32     }
33
34     private void initInfo()
35     {
36         entity = SfcDriverUtil.getMsbRegisterInfo();
37
38     }
39
40     @Override
41     public void run() {
42         boolean flag = false;
43         int retryTimes=0;
44         while (!flag && retryTimes<20)
45         {
46             try {
47                 LOGGER.info("Register Msb start:");
48                 LOGGER.info(SfcDriverUtil.toJson(entity));
49                 SdnServiceConsumer.getMsbRegisterService(ConfigInfo.getConfig().getMsbServiceUrl()).
50                         registerServce("false",entity);
51                 LOGGER.info("Register Msb end:");
52                 flag = true;
53                 break;
54             } catch (Exception e) {
55                 LOGGER.info("Register Msb failed");
56                 e.printStackTrace();
57                 threadSleep(30000);
58             }
59         }
60
61     }
62
63     private void threadSleep(int second) {
64         LOGGER.info("start sleep ....");
65         try {
66             Thread.sleep(second);
67         } catch (InterruptedException error) {
68             LOGGER.error("thread sleep error.errorMsg:" + error.getMessage());
69         }
70         LOGGER.info("sleep end .");
71     }
72 }