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 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr);
62 String responseStr = httpResult.getContent();
64 logger.info("MsbMgmrImpl -> register, responseStr is " + responseStr);
66 if(httpResult.getStatusCode() == 201)
68 logger.info("MsbMgmrImpl -> register, Successfully ");
72 logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
75 } catch (IOException e) {
76 logger.error("IOException Failed to register nokia vnfm driver! ", e);
81 private String readVfcAdaptorInfoFromJsonFile() throws IOException {
82 String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
83 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
89 public void unregister() {
91 String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH);
93 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.DELETE, null, null);
94 String responseStr = httpResult.getContent();
96 logger.info("MsbMgmrImpl -> unregister, responseStr is " + responseStr);
97 if(httpResult.getStatusCode() == 204)
99 logger.info("MsbMgmrImpl -> register, Successfully ");
103 logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
106 } catch (Exception e) {
107 logger.error("IOException Failed to unregister nokia vnfm driver! ", e);
112 public String getServiceUrlInMsbBySeriveNameAndVersion(String serviceName, String version) throws ClientProtocolException, IOException {
113 String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_QUERY_SERVICE_PATH, serviceName, version);
115 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.GET, null, null);
117 String responseStr = httpResult.getContent();
118 logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
119 String serviceUrl = "";
120 if(httpResult.getStatusCode() == 200)
122 MsbServiceInfo serviceInfo = gson.fromJson(responseStr, MsbServiceInfo.class);
123 if (null == serviceInfo) {
124 logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
127 serviceUrl = serviceInfo.getUrl();
128 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
132 logger.error("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion Error, statusCode = " + httpResult.getStatusCode());
139 public void setAdaptorEnv(AdaptorEnv env) {
140 this.adaptorEnv = env;
143 public static final void main(String[] args) {
144 MsbMgmrImpl impl = new MsbMgmrImpl();