2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.so;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonSyntaxException;
26 import java.util.HashMap;
28 import java.util.concurrent.Callable;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.Future;
32 import org.drools.core.WorkingMemory;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
35 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
36 import org.onap.policy.drools.system.PolicyEngine;
37 import org.onap.policy.rest.RestManager;
38 import org.onap.policy.rest.RestManager.Pair;
39 import org.onap.policy.so.util.Serialization;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
45 * This class handles the interface towards SO (Service Orchestrator) for the ONAP Policy
46 * Framework. The SO API is defined at this link:
47 * http://onap.readthedocs.io/en/latest/submodules/so.git/docs/SO_R1_Interface.html#get-orchestration-request
50 public final class SoManager {
51 private static final Logger logger = LoggerFactory.getLogger(SoManager.class);
53 private static ExecutorService executors = Executors.newCachedThreadPool();
55 private static final int SO_RESPONSE_ERROR = 999;
56 private static final String MEDIA_TYPE = "application/json";
57 private static final String LINE_SEPARATOR = System.lineSeparator();
59 // REST get timeout value in milliseconds
60 private static final int GET_REQUESTS_BEFORE_TIMEOUT = 20;
61 private static final long GET_REQUEST_WAIT_INTERVAL = 20000;
63 // The REST manager used for processing REST calls for this VFC manager
64 private RestManager restManager;
66 private long restGetTimeout = GET_REQUEST_WAIT_INTERVAL;
69 * Default constructor.
72 restManager = new RestManager();
76 * Create a service instance in SO.
78 * @param url the SO URL
79 * @param urlBase the base URL
80 * @param username user name on SO
81 * @param password password on SO
82 * @param request the request to issue to SO
83 * @return the SO Response object
85 public SoResponse createModuleInstance(final String url, final String urlBase, final String username,
86 final String password, final SoRequest request) {
87 // Issue the HTTP POST request to SO to create the service instance
88 String requestJson = Serialization.gsonPretty.toJson(request);
89 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|{}|{}|{}|{}|]{}{}", "SO", url, username, password,
90 createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR, requestJson);
91 Pair<Integer, String> httpResponse =
92 restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, requestJson);
94 // Process the response from SO
95 SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
96 if (SO_RESPONSE_ERROR != response.getHttpResponseCode()) {
104 * Works just like SOManager#asyncSORestCall(String, WorkingMemory, String, String, String, SORequest)
105 * except the vfModuleInstanceId is always null.
108 public Future<SoResponse> asyncSoRestCall(final String requestId, final WorkingMemory wm,
109 final String serviceInstanceId, final String vnfInstanceId,
110 final SoRequest request) {
111 return asyncSoRestCall(requestId, wm, serviceInstanceId, vnfInstanceId, null, request);
115 * This method makes an asynchronous Rest call to MSO and inserts the response into
116 * Drools working memory.
118 * @param requestId the request id
119 * @param wm the Drools working memory
120 * @param serviceInstanceId service instance id to construct the request url
121 * @param vnfInstanceId vnf instance id to construct the request url
122 * @param vfModuleInstanceId vfModule instance id to construct the request url (required in case of delete vf
124 * @param request the SO request
125 * @return a concurrent Future for the thread that handles the request
127 public Future<SoResponse> asyncSoRestCall(final String requestId,
128 final WorkingMemory wm,
129 final String serviceInstanceId,
130 final String vnfInstanceId,
131 final String vfModuleInstanceId, final SoRequest request) {
132 return executors.submit(new AsyncSoRestCallThread(requestId, wm, serviceInstanceId, vnfInstanceId,
133 vfModuleInstanceId, request));
137 * This class handles an asynchronous request to SO as a thread.
139 private class AsyncSoRestCallThread implements Callable<SoResponse> {
140 final String requestId;
141 final WorkingMemory wm;
142 final String serviceInstanceId;
143 final String vnfInstanceId;
144 final String vfModuleInstanceId;
145 final SoRequest request;
148 * Constructor, sets the context of the request.
150 * @param requestID The request ID
151 * @param wm reference to the Drools working memory
152 * @param serviceInstanceId the service instance in SO to use
153 * @param vnfInstanceId the VNF instance that is the subject of the request
154 * @param vfModuleInstanceId the vf module instance id (not null in case of delete vf module request)
155 * @param request the request itself
157 private AsyncSoRestCallThread(final String requestId,
158 final WorkingMemory wm, final String serviceInstanceId,
159 final String vnfInstanceId, final String vfModuleInstanceId,
160 final SoRequest request) {
161 this.requestId = requestId;
163 this.serviceInstanceId = serviceInstanceId;
164 this.vnfInstanceId = vnfInstanceId;
165 this.vfModuleInstanceId = vfModuleInstanceId;
166 this.request = request;
170 * Process the asynchronous SO request.
173 public SoResponse call() {
174 String urlBase = PolicyEngine.manager.getEnvironmentProperty("so.url");
175 String username = PolicyEngine.manager.getEnvironmentProperty("so.username");
176 String password = PolicyEngine.manager.getEnvironmentProperty("so.password");
178 // Create a JSON representation of the request
179 String soJson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create().toJson(request);
181 Pair<Integer, String> httpResponse = null;
183 if (request.getOperationType() != null && request.getOperationType()
184 .equals(SoOperationType.SCALE_OUT)) {
185 url = urlBase + "/serviceInstantiation/v7/serviceInstances/" + serviceInstanceId + "/vnfs/"
186 + vnfInstanceId + "/vfModules/scaleOut";
187 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, soJson);
188 httpResponse = restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, soJson);
189 } else if (request.getOperationType() != null && request.getOperationType()
190 .equals(SoOperationType.DELETE_VF_MODULE)) {
191 url = urlBase + "/serviceInstances/v7/" + serviceInstanceId + "/vnfs/" + vnfInstanceId
192 + "/vfModules/" + vfModuleInstanceId;
193 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, soJson);
194 httpResponse = restManager.delete(url, username, password, createSimpleHeaders(), MEDIA_TYPE, soJson);
199 // Process the response from SO
200 SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
202 // Return the response to Drools in its working memory
203 SoResponseWrapper soWrapper = new SoResponseWrapper(response, requestId);
204 wm.insert(soWrapper);
211 * Wait for the SO operation we have ordered to complete.
213 * @param urlBaseSo The base URL for SO
214 * @param username user name on SO
215 * @param password password on SO
216 * @param initialRequestUrl The URL of the initial HTTP request
217 * @param initialHttpResponse The initial HTTP message returned from SO
218 * @return The parsed final response of SO to the request
220 private SoResponse waitForSoOperationCompletion(final String urlBaseSo, final String username,
221 final String password, final String initialRequestUrl,
222 final Pair<Integer, String> initialHttpResponse) {
223 // Process the initial response from SO, the response to a post
224 SoResponse response = processSoResponse(initialRequestUrl, initialHttpResponse);
225 if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
229 // The SO URL to use to get the status of orchestration requests
230 String urlGet = urlBaseSo + "/orchestrationRequests/v5/" + response.getRequestReferences().getRequestId();
232 // The HTTP status code of the latest response
233 Pair<Integer, String> latestHttpResponse = initialHttpResponse;
235 // Wait for the response from SO
236 for (int attemptsLeft = GET_REQUESTS_BEFORE_TIMEOUT; attemptsLeft >= 0; attemptsLeft--) {
237 // The SO request may have completed even on the first request so we check the
240 // issuing any other requests
241 if (isRequestStateFinished(latestHttpResponse, response)) {
245 // Wait for the defined interval before issuing a get
247 Thread.sleep(restGetTimeout);
248 } catch (InterruptedException e) {
249 logger.error("Interrupted exception: ", e);
250 Thread.currentThread().interrupt();
251 response.setHttpResponseCode(SO_RESPONSE_ERROR);
255 // Issue a GET to find the current status of our request
256 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|{}|{}|{}|{}|]{}", "SO", urlGet, username, password,
257 createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR);
258 Pair<Integer, String> httpResponse = restManager.get(urlGet, username, password, createSimpleHeaders());
261 response = processSoResponse(urlGet, httpResponse);
262 if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
266 // Our latest HTTP response code
267 latestHttpResponse = httpResponse;
270 // We have timed out on the SO request
271 response.setHttpResponseCode(SO_RESPONSE_ERROR);
276 * Parse the response message from SO into a SOResponse object.
278 * @param requestURL The URL of the HTTP request
279 * @param httpResponse The HTTP message returned from SO
280 * @return The parsed response
282 private SoResponse processSoResponse(final String requestUrl, final Pair<Integer, String> httpResponse) {
283 SoResponse response = new SoResponse();
285 // A null httpDetails indicates a HTTP problem, a valid response from SO must be
288 if (!httpResultIsNullFree(httpResponse) || (httpResponse.first != 200 && httpResponse.first != 202)) {
289 logger.error("Invalid HTTP response received from SO");
290 response.setHttpResponseCode(SO_RESPONSE_ERROR);
294 // Parse the JSON of the response into our POJO
296 response = Serialization.gsonPretty.fromJson(httpResponse.second, SoResponse.class);
297 } catch (JsonSyntaxException e) {
298 logger.error("Failed to deserialize HTTP response into SOResponse: ", e);
299 response.setHttpResponseCode(SO_RESPONSE_ERROR);
303 // Set the HTTP response code of the response if needed
304 if (response.getHttpResponseCode() == 0) {
305 response.setHttpResponseCode(httpResponse.first);
308 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, requestUrl, httpResponse.second);
310 if (logger.isDebugEnabled()) {
311 logger.debug("***** Response to SO Request to URL {}:", requestUrl);
312 logger.debug(httpResponse.second);
319 * Method to allow tuning of REST get timeout.
321 * @param restGetTimeout the timeout value
323 protected void setRestGetTimeout(final long restGetTimeout) {
324 this.restGetTimeout = restGetTimeout;
328 * Check that the request state of a response is defined.
330 * @param response The response to check
331 * @return true if the request for the response is defined
333 private boolean isRequestStateDefined(final SoResponse response) {
334 return response != null && response.getRequest() != null && response.getRequest().getRequestStatus() != null
335 && response.getRequest().getRequestStatus().getRequestState() != null;
339 * Check that the request state of a response is finished.
341 * @param latestHttpDetails the HTTP details of the response
342 * @param response The response to check
343 * @return true if the request for the response is finished
345 private boolean isRequestStateFinished(final Pair<Integer, String> latestHttpDetails, final SoResponse response) {
346 if (latestHttpDetails != null && 200 == latestHttpDetails.first && isRequestStateDefined(response)) {
347 String requestState = response.getRequest().getRequestStatus().getRequestState();
348 return "COMPLETE".equalsIgnoreCase(requestState) || "FAILED".equalsIgnoreCase(requestState);
355 * Check that a HTTP operation result has no nulls.
357 * @param httpOperationResult the result to check
358 * @return true if no nulls are found
360 private boolean httpResultIsNullFree(Pair<Integer, String> httpOperationResult) {
361 return httpOperationResult != null && httpOperationResult.first != null && httpOperationResult.second != null;
365 * Create simple HTTP headers for unauthenticated requests to SO.
367 * @return the HTTP headers
369 private Map<String, String> createSimpleHeaders() {
370 Map<String, String> headers = new HashMap<>();
371 headers.put("Accept", MEDIA_TYPE);