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.FileNotFoundException;
20 import java.io.IOException;
21 import java.util.HashMap;
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;
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 String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
63 logger.info("MsbMgmrImpl -> register, responseStr is " + responseStr);
65 } catch (IOException e) {
66 logger.error("IOException Failed to register nokia vnfm driver! ", e);
71 private String readVfcAdaptorInfoFromJsonFile() throws IOException {
72 String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
73 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
79 public void unregister() {
81 String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH);
83 String responseStr = httpClientProcessor.process(url, RequestMethod.DELETE, null, null).getContent();
85 logger.info("MsbMgmrImpl -> unregister, responseStr is " + responseStr);
87 } catch (Exception e) {
88 logger.error("IOException Failed to unregister nokia vnfm driver! ", e);
93 public String getServiceUrlInMsbBySeriveNameAndVersion(String serviceName, String version) throws ClientProtocolException, IOException
95 String url=adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_QUERY_SERVICE_PATH, serviceName, version);
97 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, null, null).getContent();
98 logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
100 MsbServiceInfo serviceInfo = gson.fromJson(responseStr, MsbServiceInfo.class);
101 if(null == serviceInfo)
103 logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
106 String serviceUrl = serviceInfo.getUrl();
107 logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
112 public void setAdaptorEnv(AdaptorEnv env) {
113 this.adaptorEnv = env;
116 public static final void main(String[] args) {
117 MsbMgmrImpl impl = new MsbMgmrImpl();