Add unit test cases
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nslcm / impl / NslcmMgmrImpl.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.nslcm.impl;
18
19 import java.io.IOException;
20 import java.util.HashMap;
21
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.MediaType;
33 import org.springframework.stereotype.Component;
34 import org.springframework.web.bind.annotation.RequestMethod;
35
36 import com.google.gson.Gson;
37
38 @Component
39 public class NslcmMgmrImpl implements NslcmMgmrInf{
40         private static final Logger logger = LoggerFactory.getLogger(NslcmMgmrImpl.class);
41         
42         @Autowired 
43         private AdaptorEnv adaptorEnv;
44         
45         @Autowired
46         HttpClientProcessorInf httpClientProcessor;
47         
48         private Gson gson = new Gson();
49         
50 //      @Deprecated
51 //      public VnfmInfo queryVnfm(String vnfmId) throws IOException
52 //      {
53 //              String httpPath = String.format(CommonConstants.RetrieveVnfmListPath, vnfmId);
54 //              RequestMethod method = RequestMethod.GET;
55 //              
56 //              String responseStr = operateNslcmHttpTask(null, httpPath, method);
57 //              
58 //              logger.info("NslcmMgmrImpl->queryVnfm, the vnfmInfo is {}", responseStr);
59 //              
60 //              VnfmInfo response = gson.fromJson(responseStr, VnfmInfo.class);
61 //              
62 //              return response;
63 //      }
64
65         public NslcmGrantVnfResponse grantVnf(NslcmGrantVnfRequest driverRequest) throws IOException {
66                 String httpPath = CommonConstants.NslcmGrantPath;
67                 RequestMethod method = RequestMethod.POST;
68                         
69                 String responseStr = operateNslcmHttpTask(driverRequest, httpPath, method);
70                 
71                 logger.info("NslcmMgmrImpl->grantVnf, the NslcmGrantVnfResponse is {}", responseStr);
72                 
73                 NslcmGrantVnfResponse response = gson.fromJson(responseStr, NslcmGrantVnfResponse.class);
74                 
75                 return response;
76         }
77
78         public void notifyVnf(NslcmNotifyLCMEventsRequest driverRequest, String vnfmId, String vnfInstanceId) throws IOException {
79                 String httpPath = String.format(CommonConstants.NslcmNotifyPath, vnfmId, vnfInstanceId);
80                 RequestMethod method = RequestMethod.POST;
81                         
82                 operateNslcmHttpTask(driverRequest, httpPath, method);
83         }
84         
85         private String operateNslcmHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws IOException {
86                 String url=adaptorEnv.getLcmApiUriFront() + httpPath;
87                 
88                 HashMap<String, String> map = new HashMap<>();
89                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
90                 
91                 String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj)).getContent();
92                 
93                 return responseStr;
94         }
95
96         public void setAdaptorEnv(AdaptorEnv env) {
97                 this.adaptorEnv = env;
98         }
99
100 }