ed85fcfc53e339815ae6a8f46e2f136ca2e8e852
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / msb / impl / MsbMgmrImpl.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.msb.impl;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25
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;
40
41 @Component
42 public class MsbMgmrImpl implements IMsbMgmr {
43         private static final Logger logger = LogManager.getLogger("MsbMgmrImpl");
44         @Autowired 
45         private HttpClientBuilder httpClientBuilder;
46         
47         @Autowired 
48         private AdaptorEnv adaptorEnv;
49         
50         @Value("${serviceName}")
51         private String serviceName;
52         
53         @Value("${version}")
54         private String version;
55         
56         @Value("${url}")
57         private String url;
58         
59         @Value("${protocol}")
60         private String protocol;
61         
62         @Value("${visualRange}")
63         private String visualRange;
64         
65         @Value("${ip}")
66         private String ip;
67         
68         @Value("${port}")
69         private String port;
70         
71         @Value("${ttl}")
72         private String ttl;
73
74         @Override
75         public void register() {
76                 String httpPath = CommonConstants.MSB_REGISTER_SERVICE_PATH;
77                 RequestMethod method = RequestMethod.POST;
78                 
79                 try {
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);
85                 }
86                         
87         }
88         
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";
94
95         try {
96             ins = new FileInputStream(fileName);
97             bins = new BufferedInputStream(ins);
98
99             byte[] contentByte = new byte[ins.available()];
100             int num = bins.read(contentByte);
101
102             if(num > 0) {
103                 fileContent = new String(contentByte);
104             }
105         } catch(FileNotFoundException e) {
106                 logger.error(fileName + "is not found!", e);
107         } finally {
108             if(ins != null) {
109                 ins.close();
110             }
111             if(bins != null) {
112                 bins.close();
113             }
114         }
115
116         return fileContent;
117     }
118
119         @Override
120         public void unregister() {
121                 String httpPath = String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH, serviceName, version, ip, port);
122                 RequestMethod method = RequestMethod.DELETE;
123                 
124                 try {
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);
130                 }
131
132         }
133         
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);
138                 
139                 processor.addPostEntity(httpBodyObj);
140                 
141                 String responseStr = processor.process(url);
142                 
143                 return responseStr;
144         }
145         
146     public String getAppRoot() {
147         String appRoot = null;
148         appRoot = System.getProperty("catalina.base");
149         if(appRoot != null) {
150             appRoot = getCanonicalPath(appRoot);
151         }
152         return appRoot;
153     }
154
155     private String getCanonicalPath(final String inPath) {
156         String path = null;
157         try {
158             if(inPath != null) {
159                 final File file = new File(inPath);
160                 path = file.getCanonicalPath();
161             }
162         } catch(final IOException e) {
163             logger.error("file.getCanonicalPath() IOException:", e);
164         }
165         return path;
166     }
167
168 }