summaryrefslogtreecommitdiffstats
path: root/nokia/vnfmdriver/vfcadaptorservice/vfcadaptor/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/cbam/impl/CbamMgmrImpl.java
blob: 79966f63ed1142fe193e35882f5460916563eaff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.impl;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;

@Component
public class CbamMgmrImpl implements CbamMgmrInf {
	private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
	private Gson gson = new Gson();
	
	@Autowired 
	private AdaptorEnv adaptorEnv;
	
	@Autowired
	HttpClientProcessorInf httpClientProcessor;
	
	private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
		String result = null;
		String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
		HashMap<String, String> map = new HashMap<>();
		map.put(CommonConstants.ACCEPT, "*/*");
		map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
		
		String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
		
		String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent();
		
		logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
		
		JSONObject tokenJsonObject = new JSONObject(responseStr);
		
		result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
		
		return result;
	}
	
	public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
		String httpPath = CommonConstants.CbamCreateVnfPath;
		RequestMethod method = RequestMethod.POST;
			
		String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
		
		logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
		
		CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
		
		return response;
	}
	
	/* (non-Javadoc)
	 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
	 */
	public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
		String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
		RequestMethod method = RequestMethod.POST;
			
		String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
		
		logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
		
		CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
		
		return response;
	}
	
	public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
		String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
		RequestMethod method = RequestMethod.POST;
		
		String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
		
		logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
		
		CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
		
		return response;
	}
	
	public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
		String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
		RequestMethod method = RequestMethod.DELETE;
		
		operateCbamHttpTask(null, httpPath, method);
		
		logger.info("CbamMgmrImpl -> deleteVnf.");
	}
	
	public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
		String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
		RequestMethod method = RequestMethod.POST;
			
		String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
		
		CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
		
		return response;
	}

	public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
		String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
		RequestMethod method = RequestMethod.POST;
			
		String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
		
		logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
		
		CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
		
		return response;
	}
	
	public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
		String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
		RequestMethod method = RequestMethod.GET;
		
		String responseStr = operateCbamHttpTask(null, httpPath, method);
		
		logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
		
		CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
		
		return response;
	}

	public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
		String token = null;
		try {
			token = retrieveToken();
		} catch (JSONException e) {
			logger.error("retrieveTokenError ", e);
		}
	
		String url= adaptorEnv.getCbamApiUriFront() + httpPath;
		
		HashMap<String, String> map = new HashMap<>();
		map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
		map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
		
		String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj)).getContent();
		
		return responseStr;
	}

	public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
		String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
		RequestMethod method = RequestMethod.GET;
		
		String responseStr = operateCbamHttpTask(null, httpPath, method);
		
		logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
		
		CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
		
		return response;
	}

	public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
		this.adaptorEnv = adaptorEnv;
	}
	
}