modify msb 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.FileNotFoundException;
20 import java.io.IOException;
21 import java.util.HashMap;
22
23 import org.apache.http.client.ClientProtocolException;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.bo.MsbServiceInfo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.MediaType;
34 import org.springframework.stereotype.Component;
35 import org.springframework.web.bind.annotation.RequestMethod;
36
37 import com.google.gson.Gson;
38
39 @Component
40 public class MsbMgmrImpl implements IMsbMgmr {
41         private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
42         
43         @Autowired
44         AdaptorEnv adaptorEnv;
45         
46         @Autowired
47         HttpClientProcessorInf httpClientProcessor;
48         
49         private Gson gson = new Gson();
50         
51         @Override
52         public void register() {
53                 
54                 try {
55                         String url = adaptorEnv.getMsbApiUriFront() + CommonConstants.MSB_REGISTER_SERVICE_PATH;
56                         HashMap<String, String> map = new HashMap<>();
57                         map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
58                         
59                         String bodyPostStr = readVfcAdaptorInfoFromJsonFile();;
60                         
61                         String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
62                         
63                         logger.info("MsbMgmrImpl -> register, responseStr is " + responseStr);
64                         
65                 }  catch (IOException e) {
66                         logger.error("IOException Failed to register nokia vnfm driver! ", e);
67                 } 
68                         
69         }
70         
71         private String readVfcAdaptorInfoFromJsonFile() throws IOException {
72         String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
73                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
74
75         return fileContent;
76     }
77
78         @Override
79         public void unregister() {
80                 try {
81                         String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH);
82                         
83                         String responseStr = httpClientProcessor.process(url, RequestMethod.DELETE, null, null).getContent();
84                         
85                         logger.info("MsbMgmrImpl -> unregister, responseStr is " + responseStr);
86                         
87                 }  catch (Exception e) {
88                         logger.error("IOException Failed to unregister nokia vnfm driver! ", e);
89                 }
90                 
91         }
92         
93    public String getServiceUrlInMsbBySeriveNameAndVersion(String serviceName, String version) throws ClientProtocolException, IOException
94    {
95            String url=adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_QUERY_SERVICE_PATH, serviceName, version);
96                 
97                 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, null, null).getContent();
98                 logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
99                 
100                 MsbServiceInfo serviceInfo = gson.fromJson(responseStr, MsbServiceInfo.class);
101                 if(null == serviceInfo)
102                 {
103                            logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
104                 }
105                 
106                 String serviceUrl = serviceInfo.getUrl();
107                 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
108                 
109                 return serviceUrl;
110    }
111
112         public void setAdaptorEnv(AdaptorEnv env) {
113                 this.adaptorEnv = env;
114         }
115         
116         public static final void main(String[] args) {
117                 MsbMgmrImpl impl = new MsbMgmrImpl();
118                 impl.register();
119         }
120
121 }