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.BufferedInputStream;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
26 import org.apache.http.client.ClientProtocolException;
27 import org.apache.http.impl.client.HttpClientBuilder;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpRequestProcessor;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.inf.IMsbMgmr;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.http.MediaType;
37 import org.springframework.stereotype.Component;
38 import org.springframework.util.ResourceUtils;
39 import org.springframework.web.bind.annotation.RequestMethod;
42 public class MsbMgmrImpl implements IMsbMgmr {
43 private static final Logger logger = LogManager.getLogger("MsbMgmrImpl");
45 private HttpClientBuilder httpClientBuilder;
48 private AdaptorEnv adaptorEnv;
50 @Value("${serviceName}")
51 private String serviceName;
54 private String version;
60 private String protocol;
62 @Value("${visualRange}")
63 private String visualRange;
75 public void register() {
76 String httpPath = CommonConstants.MSB_REGISTER_SERVICE_PATH;
77 RequestMethod method = RequestMethod.POST;
80 String jsonStr = readVfcAdaptorInfoFromJsonFile();
81 String registerResponse = operateHttpTask(jsonStr, httpPath, method);
82 logger.info("registerResponse is ", registerResponse);
83 } catch (IOException e) {
84 logger.error("Failed to read vfcadaptor info! ", e);
89 public String readVfcAdaptorInfoFromJsonFile() throws IOException {
90 InputStream ins = null;
91 BufferedInputStream bins = null;
92 String fileContent = "";
93 String fileName = getAppRoot() + "/etc/adapterInfo/vnfmadapterinfo.json";
96 ins = new FileInputStream(fileName);
97 bins = new BufferedInputStream(ins);
99 byte[] contentByte = new byte[ins.available()];
100 int num = bins.read(contentByte);
103 fileContent = new String(contentByte);
105 } catch(FileNotFoundException e) {
106 logger.error(fileName + "is not found!", e);
120 public void unregister() {
121 String httpPath = String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH, serviceName, version, ip, port);
122 RequestMethod method = RequestMethod.DELETE;
125 String jsonStr = readVfcAdaptorInfoFromJsonFile();
126 String registerResponse = operateHttpTask(jsonStr, httpPath, method);
127 logger.info("unregisterResponse is ", registerResponse);
128 } catch (IOException e) {
129 logger.error("Failed to unregister! ", e);
134 public String operateHttpTask(String httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
135 String url=adaptorEnv.getMsbApiUriFront() + httpPath;
136 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, method);
137 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
139 processor.addPostEntity(httpBodyObj);
141 String responseStr = processor.process(url);
146 public String getAppRoot() {
147 String appRoot = null;
148 appRoot = System.getProperty("catalina.base");
149 if(appRoot != null) {
150 appRoot = getCanonicalPath(appRoot);
155 private String getCanonicalPath(final String inPath) {
159 final File file = new File(inPath);
160 path = file.getCanonicalPath();
162 } catch(final IOException e) {
163 logger.error("file.getCanonicalPath() IOException:", e);