Create seed code of svnfm vnfmdriver
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / com / nokia / vfcadaptor / 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 com.nokia.vfcadaptor.nslcm.impl;
18
19 import java.io.IOException;
20
21 import org.apache.http.client.ClientProtocolException;
22 import org.apache.http.impl.client.HttpClientBuilder;
23 import org.apache.log4j.Logger;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.http.MediaType;
26 import org.springframework.stereotype.Component;
27 import org.springframework.web.bind.annotation.RequestMethod;
28
29 import com.google.gson.Gson;
30 import com.nokia.vfcadaptor.common.bo.AdaptorEnv;
31 import com.nokia.vfcadaptor.constant.CommonConstants;
32 import com.nokia.vfcadaptor.http.client.HttpRequestProcessor;
33 import com.nokia.vfcadaptor.nslcm.bo.NslcmGrantVnfRequest;
34 import com.nokia.vfcadaptor.nslcm.bo.NslcmGrantVnfResponse;
35 import com.nokia.vfcadaptor.nslcm.bo.NslcmNotifyLCMEventsRequest;
36 import com.nokia.vfcadaptor.nslcm.bo.VnfmInfo;
37 import com.nokia.vfcadaptor.nslcm.inf.NslcmMgmrInf;
38
39 @Component
40 public class NslcmMgmrImpl implements NslcmMgmrInf{
41         private Logger logger = Logger.getLogger(NslcmMgmrImpl.class);
42         
43         @Autowired 
44         private AdaptorEnv adaptorEnv;
45         
46         @Autowired 
47         private HttpClientBuilder httpClientBuilder;
48         
49         private Gson gson = new Gson();
50         
51         public VnfmInfo queryVnfm(String vnfmId) throws ClientProtocolException, IOException
52         {
53                 String httpPath = String.format(CommonConstants.RetrieveNvfmListPath, 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 ClientProtocolException, 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 vnfInstanceId) throws ClientProtocolException, IOException {
79                 String httpPath = String.format(CommonConstants.NslcmNotifyPath, vnfInstanceId);
80                 RequestMethod method = RequestMethod.POST;
81                         
82                 operateNslcmHttpTask(driverRequest, httpPath, method);
83         }
84         
85         public String operateNslcmHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
86                 String url="http://" + adaptorEnv.getNslcmIp() + ":" + adaptorEnv.getNslcmPort() + httpPath;
87                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, method);
88                 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
89                 
90                 processor.addPostEntity(gson.toJson(httpBodyObj));
91                 
92                 String responseStr = processor.process(url);
93                 
94                 return responseStr;
95         }
96
97 }