cd4c7e313ad799ae55b25ad68f1ee2c533aff599
[vfc/nfvo/driver/vnfm/svnfm.git] /
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 IOException {
119                 try{
120                         String url = adaptorEnv.getMsbApiUriFront() + String.format(CommonConstants.MSB_QUERY_SERVICE_PATH, serviceName, version);
121
122                         HttpResult httpResult = httpClientProcessor.process(url, RequestMethod.GET, null, null);
123
124                         String responseStr = httpResult.getContent();
125                         logger.info("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion, responseStr is " + responseStr);
126                         String serviceUrl = "";
127                         if(httpResult.getStatusCode() == 200)
128                         {
129                                 MsbServiceInfo serviceInfo = gson.fromJson(responseStr, MsbServiceInfo.class);
130                                 if (null == serviceInfo) {
131                                         logger.error("There is no service in MSB for serviceName = {} and version = {}", serviceName, version);
132                                 }
133                                 else{
134                                         serviceUrl = serviceInfo.getUrl();
135                                         logger.info("Service Url in MSB for serviceName = {} and version = {} is {}", serviceName, version, serviceUrl);
136                                 }
137                         }
138                         else
139                         {
140                                 logger.error("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion Error, statusCode = " + httpResult.getStatusCode());
141                         }
142
143                         return serviceUrl;
144
145                 }catch(Exception e){
146                         logger.error("MsbMgmrImpl -> getServiceUrlInMsbBySeriveNameAndVersion Error", e);
147                         throw new IOException("getServiceUrlInMsbBySeriveNameAndVersion", e);
148
149                 }               
150                 
151         }
152
153         public void setAdaptorEnv(AdaptorEnv env) {
154                 this.adaptorEnv = env;
155         }
156
157 }