Improve unit-test coverage
[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.IOException;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
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;
38
39 import com.google.gson.Gson;
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonObject;
42 import com.google.gson.JsonParser;
43
44 @Component
45 public class MsbMgmrImpl implements IMsbMgmr {
46         private static final Logger logger = LoggerFactory.getLogger(MsbMgmrImpl.class);
47
48         @Autowired
49         AdaptorEnv adaptorEnv;
50
51         @Autowired
52         HttpClientProcessorInf httpClientProcessor;
53
54         private Gson gson = new Gson();
55
56         @Override
57         public void register() {
58
59                 try {
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);
63
64                         String bodyPostStr = readVfcAdaptorInfoFromJsonFile();
65                         
66                         logger.info("MSB register content is: " + bodyPostStr);
67
68                         HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr);
69                         String responseStr = httpResult.getContent();
70
71                         logger.info("MsbMgmrImpl -> register, responseStr is " + responseStr);
72                         
73                         if(httpResult.getStatusCode() == 201)
74                         {
75                                 logger.info("MsbMgmrImpl -> register, Successfully ");
76                         }
77                         else
78                         {
79                                 logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
80                         }
81
82                 } catch (IOException e) {
83                         logger.error("IOException Failed to register nokia vnfm driver! ", e);
84                 }
85
86         }
87
88         private String readVfcAdaptorInfoFromJsonFile() throws IOException {
89                 String filePath = "/etc/adapterInfo/vnfmadapterinfo.json";
90                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);   
91                 return fileContent;
92         }
93
94         @Override
95         public void unregister() {
96 //              try {
97 //                      String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_UNREGISTER_SERVICE_PATH);
98 //
99 //                      HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.DELETE, null, null);
100 //                      String responseStr = httpResult.getContent();
101 //
102 //                      logger.info("MsbMgmrImpl -> unregister, responseStr is " + responseStr);
103 //                      if(httpResult.getStatusCode() == 204)
104 //                      {
105 //                              logger.info("MsbMgmrImpl -> register, Successfully ");
106 //                      }
107 //                      else
108 //                      {
109 //                              logger.error("MsbMgmrImpl -> register Error, statusCode = " + httpResult.getStatusCode());
110 //                      }
111 //
112 //              } catch (Exception e) {
113 //                      logger.error("IOException Failed to unregister nokia vnfm driver! ", e);
114 //              }
115
116         }
117
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);
120
121                 HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.GET, null, null);
122
123                 String responseStr = httpResult.getContent();
124                 logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
125                 String serviceUrl = "";
126                 if(httpResult.getStatusCode() == 200)
127                 {
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);
131                         }
132                         
133                         serviceUrl = serviceInfo.getUrl();
134                         logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
135                 }
136                 else
137                 {
138                         logger.error("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion Error, statusCode = " + httpResult.getStatusCode());
139                 }
140
141
142                 return serviceUrl;
143         }
144
145         public void setAdaptorEnv(AdaptorEnv env) {
146                 this.adaptorEnv = env;
147         }
148
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"));
154 //      }
155
156 }