2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.impl;
19 import java.io.IOException;
20 import java.util.HashMap;
22 import org.apache.http.client.ClientProtocolException;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
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;
37 import com.google.gson.Gson;
40 public class MsbMgmrImpl implements IMsbMgmr {
41 private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
44 AdaptorEnv adaptorEnv;
47 HttpClientProcessorInf httpClientProcessor;
49 private Gson gson = new Gson();
52 public void register() {
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);
59 String bodyPostStr = readVfcAdaptorInfoFromJsonFile();
61 logger.info("MSB register content is: " + bodyPostStr);
63 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr);
64 String responseStr = httpResult.getContent();
66 logger.info("MsbMgmrImpl -> register, responseStr is " + responseStr);
68 if(httpResult.getStatusCode() == 201)
70 logger.info("MsbMgmrImpl -> register, Successfully ");
74 logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
77 } catch (IOException e) {
78 logger.error("IOException Failed to register nokia vnfm driver! ", e);
83 private String readVfcAdaptorInfoFromJsonFile() throws IOException {
84 String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
85 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
91 public void unregister() {
93 String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH);
95 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.DELETE, null, null);
96 String responseStr = httpResult.getContent();
98 logger.info("MsbMgmrImpl -> unregister, responseStr is " + responseStr);
99 if(httpResult.getStatusCode() == 204)
101 logger.info("MsbMgmrImpl -> register, Successfully ");
105 logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
108 } catch (Exception e) {
109 logger.error("IOException Failed to unregister nokia vnfm driver! ", e);
114 public String getServiceUrlInMsbBySeriveNameAndVersion(String serviceName, String version) throws ClientProtocolException, IOException {
115 String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_QUERY_SERVICE_PATH, serviceName, version);
117 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.GET, null, null);
119 String responseStr = httpResult.getContent();
120 logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
121 String serviceUrl = "";
122 if(httpResult.getStatusCode() == 200)
124 MsbServiceInfo serviceInfo = gson.fromJson(responseStr, MsbServiceInfo.class);
125 if (null == serviceInfo) {
126 logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
129 serviceUrl = serviceInfo.getUrl();
130 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
134 logger.error("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion Error, statusCode = " + httpResult.getStatusCode());
141 public void setAdaptorEnv(AdaptorEnv env) {
142 this.adaptorEnv = env;
145 public static final void main(String[] args) {
146 MsbMgmrImpl impl = new MsbMgmrImpl();