Modify service register part
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / msb / impl / MsbMgmrImpl.java
1 /*
2  * Copyright 2016-2017, Nokia 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.vfc.nfvo.driver.vnfm.svnfm.msb.impl;
18
19 import java.io.IOException;
20
21 import org.onap.msb.sdk.discovery.common.RouteException;
22 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
24 import org.onap.msb.sdk.discovery.entity.RouteResult;
25 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 import com.alibaba.fastjson.JSON;
35 import com.google.gson.Gson;
36
37 @Component
38 public class MsbMgmrImpl implements IMsbMgmr {
39         private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
40         
41         @Autowired
42         AdaptorEnv adaptorEnv;
43         
44         private Gson gson = new Gson();
45         
46         @Override
47         public void register() {
48                 try {
49                         String vfcAdaptorInfoJsonStr = readVfcAdaptorInfoFromJsonFile();
50 //                      MicroServiceInfo msinfo = gson.fromJson(vfcAdaptorInfoJsonStr, MicroServiceInfo.class);
51                         
52                         JSON json = com.alibaba.fastjson.JSON.parseObject(vfcAdaptorInfoJsonStr);
53                         MicroServiceInfo msinfo = com.alibaba.fastjson.JSON.toJavaObject(json , MicroServiceInfo.class);
54                         
55                         MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
56                         MicroServiceFullInfo microServiceInfo = msbClient.registerMicroServiceInfo(msinfo);
57                         logger.info("Registered service response info is " + microServiceInfo.toString());
58                         
59                 } catch (RouteException e) {
60                         logger.error("RouteException Failed to register nokia vnfm driver! ", e);
61                 } catch (IOException e) {
62                         logger.error("IOException Failed to register nokia vnfm driver! ", e);
63                 }
64                         
65         }
66         
67         private String readVfcAdaptorInfoFromJsonFile() throws IOException {
68         String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
69                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
70
71         return fileContent;
72     }
73
74         @Override
75         public void unregister() {
76                 try {
77                         String jsonStr = readVfcAdaptorInfoFromJsonFile();
78                         MicroServiceInfo msinfo = gson.fromJson(jsonStr, MicroServiceInfo.class);
79                         
80                         MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
81                         RouteResult routeResult = msbClient.cancelMicroServiceInfo(msinfo.getServiceName(), msinfo.getVersion());
82                         logger.info("unregistered service response info is " + routeResult.toString());
83                         
84                 } catch (IOException e) {
85                         logger.error("Failed to read vfcadaptor info! ", e);
86                 } catch (RouteException e) {
87                         logger.error("Failed to register nokia vnfm driver! ", e);
88                 }
89         }
90         
91    public String getServiceUrlInMsbBySeriveNameAndPort(String serviceName, String version) throws RouteException
92    {
93            String serviceUrl = null;
94            MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
95            MicroServiceFullInfo microServiceInfo = msbClient.queryMicroServiceInfo(serviceName, version);
96            if(null == microServiceInfo)
97            {
98                    logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
99            }
100            else
101            {
102                    serviceUrl = microServiceInfo.getUrl();
103                    logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
104            }
105            return serviceUrl;
106                 
107    }
108
109         public void setAdaptorEnv(AdaptorEnv env) {
110                 this.adaptorEnv = env;
111         }
112         
113         public static final void main(String[] args) {
114                 MsbMgmrImpl impl = new MsbMgmrImpl();
115                 impl.register();
116         }
117
118 }