Add Unit Tests.
[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.google.gson.Gson;
35
36 @Component
37 public class MsbMgmrImpl implements IMsbMgmr {
38         private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
39         
40         @Autowired
41         AdaptorEnv adaptorEnv;
42         
43         private Gson gson = new Gson();
44         
45         @Override
46         public void register() {
47                 try {
48                         String vfcAdaptorInfoJsonStr = readVfcAdaptorInfoFromJsonFile();
49                         MicroServiceInfo msinfo = gson.fromJson(vfcAdaptorInfoJsonStr, MicroServiceInfo.class);
50                         
51                         MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
52                         MicroServiceFullInfo microServiceInfo = msbClient.registerMicroServiceInfo(msinfo);
53                         logger.info("Registered service response info is " + microServiceInfo.toString());
54                         
55                 } catch (IOException e) {
56                         logger.error("Failed to read vfcadaptor info! ", e);
57                 } catch (RouteException e) {
58                         logger.error("Failed to register nokia vnfm driver! ", e);
59                 }
60                         
61         }
62         
63         private String readVfcAdaptorInfoFromJsonFile() throws IOException {
64         String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
65                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
66
67         return fileContent;
68     }
69
70         @Override
71         public void unregister() {
72                 try {
73                         String jsonStr = readVfcAdaptorInfoFromJsonFile();
74                         MicroServiceInfo msinfo = gson.fromJson(jsonStr, MicroServiceInfo.class);
75                         
76                         MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
77                         RouteResult routeResult = msbClient.cancelMicroServiceInfo(msinfo.getServiceName(), msinfo.getVersion());
78                         logger.info("unregistered service response info is " + routeResult.toString());
79                         
80                 } catch (IOException e) {
81                         logger.error("Failed to read vfcadaptor info! ", e);
82                 } catch (RouteException e) {
83                         logger.error("Failed to register nokia vnfm driver! ", e);
84                 }
85         }
86         
87    public String getServiceUrlInMsbBySeriveNameAndPort(String serviceName, String version) throws RouteException
88    {
89            MSBServiceClient msbClient = new MSBServiceClient(adaptorEnv.getMsbIp(), adaptorEnv.getMsbPort());
90            MicroServiceFullInfo microServiceInfo = msbClient.queryMicroServiceInfo(serviceName, version);
91            if(null == microServiceInfo)
92            {
93                    logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
94            }
95            
96            String serviceUrl = microServiceInfo.getUrl();
97                 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
98                 
99                 return serviceUrl;
100                 
101    }
102
103         public void setAdaptorEnv(AdaptorEnv env) {
104                 this.adaptorEnv = env;
105         }
106
107 }