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