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;
21 import java.util.List;
24 import org.apache.http.client.ClientProtocolException;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.bo.MsbServiceInfo;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.MediaType;
36 import org.springframework.stereotype.Component;
37 import org.springframework.web.bind.annotation.RequestMethod;
39 import com.google.gson.Gson;
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonObject;
42 import com.google.gson.JsonParser;
45 public class MsbMgmrImpl implements IMsbMgmr {
46 private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
49 AdaptorEnv adaptorEnv;
52 HttpClientProcessorInf httpClientProcessor;
54 private Gson gson = new Gson();
57 public void register() {
60 String url = adaptorEnv.getMsbApiUriFront() + CommonConstants.MSB_REGISTER_SERVICE_PATH;
61 HashMap<String, String> map = new HashMap<>();
62 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
64 String bodyPostStr = readVfcAdaptorInfoFromJsonFile();
66 logger.info("MSB register content is: " + bodyPostStr);
68 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr);
69 String responseStr = httpResult.getContent();
71 logger.info("MsbMgmrImpl -> register, responseStr is " + responseStr);
73 if(httpResult.getStatusCode() == 201)
75 logger.info("MsbMgmrImpl -> register, Successfully ");
79 logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
82 } catch (IOException e) {
83 logger.error("IOException Failed to register nokia vnfm driver! ", e);
88 private String readVfcAdaptorInfoFromJsonFile() throws IOException {
89 String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
90 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
95 public void unregister() {
97 // String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH);
99 // HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.DELETE, null, null);
100 // String responseStr = httpResult.getContent();
102 // logger.info("MsbMgmrImpl -> unregister, responseStr is " + responseStr);
103 // if(httpResult.getStatusCode() == 204)
105 // logger.info("MsbMgmrImpl -> register, Successfully ");
109 // logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
112 // } catch (Exception e) {
113 // logger.error("IOException Failed to unregister nokia vnfm driver! ", e);
118 public String getServiceUrlInMsbBySeriveNameAndVersion(String serviceName, String version) throws ClientProtocolException, IOException {
119 String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_QUERY_SERVICE_PATH, serviceName, version);
121 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.GET, null, null);
123 String responseStr = httpResult.getContent();
124 logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
125 String serviceUrl = "";
126 if(httpResult.getStatusCode() == 200)
128 MsbServiceInfo serviceInfo = gson.fromJson(responseStr, MsbServiceInfo.class);
129 if (null == serviceInfo) {
130 logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
133 serviceUrl = serviceInfo.getUrl();
134 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
138 logger.error("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion Error, statusCode = " + httpResult.getStatusCode());
145 public void setAdaptorEnv(AdaptorEnv env) {
146 this.adaptorEnv = env;
149 // public static final void main(String[] args) {
150 //// MsbMgmrImpl impl = new MsbMgmrImpl();
151 //// impl.register();
152 // System.setProperty("catalina.base", "D:\\Install\\apache-tomcat-9.0.0.M26");
153 // System.out.println(System.getProperty("catalina.base"));