2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.so;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonSyntaxException;
27 import java.util.HashMap;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.Future;
33 import org.drools.core.WorkingMemory;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
36 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
37 import org.onap.policy.drools.system.PolicyEngine;
38 import org.onap.policy.rest.RestManager;
39 import org.onap.policy.rest.RestManager.Pair;
40 import org.onap.policy.so.util.Serialization;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
46 * This class handles the interface towards SO (Service Orchestrator) for the ONAP Policy
47 * Framework. The SO API is defined at this link:
48 * http://onap.readthedocs.io/en/latest/submodules/so.git/docs/SO_R1_Interface.html#get-orchestration-request
51 public final class SoManager {
52 private static final Logger logger = LoggerFactory.getLogger(SoManager.class);
54 private static ExecutorService executors = Executors.newCachedThreadPool();
56 private static final int SO_RESPONSE_ERROR = 999;
57 private static final String MEDIA_TYPE = "application/json";
58 private static final String LINE_SEPARATOR = System.lineSeparator();
60 // REST get timeout value in milliseconds
61 private static final int GET_REQUESTS_BEFORE_TIMEOUT = 20;
62 private static final long GET_REQUEST_WAIT_INTERVAL = 20000;
64 // The REST manager used for processing REST calls for this VFC manager
65 private RestManager restManager;
67 private long restGetTimeout = GET_REQUEST_WAIT_INTERVAL;
70 * Default constructor.
73 restManager = new RestManager();
77 * Create a service instance in SO.
79 * @param url the SO URL
80 * @param urlBase the base URL
81 * @param username user name on SO
82 * @param password password on SO
83 * @param request the request to issue to SO
84 * @return the SO Response object
86 public SoResponse createModuleInstance(final String url, final String urlBase, final String username,
87 final String password, final SoRequest request) {
88 // Issue the HTTP POST request to SO to create the service instance
89 String requestJson = Serialization.gsonPretty.toJson(request);
90 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|{}|{}|{}|{}|]{}{}", "SO", url, username, password,
91 createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR, requestJson);
92 Pair<Integer, String> httpResponse =
93 restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, requestJson);
95 // Process the response from SO
96 SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
97 if (SO_RESPONSE_ERROR != response.getHttpResponseCode()) {
105 * Works just like SOManager#asyncSORestCall(String, WorkingMemory, String, String, String, SORequest)
106 * except the vfModuleInstanceId is always null.
109 public Future<SoResponse> asyncSoRestCall(final String requestId, final WorkingMemory wm,
110 final String serviceInstanceId, final String vnfInstanceId,
111 final SoRequest request) {
112 return asyncSoRestCall(requestId, wm, serviceInstanceId, vnfInstanceId, null, request);
116 * This method makes an asynchronous Rest call to MSO and inserts the response into
117 * Drools working memory.
119 * @param requestId the request id
120 * @param wm the Drools working memory
121 * @param serviceInstanceId service instance id to construct the request url
122 * @param vnfInstanceId vnf instance id to construct the request url
123 * @param vfModuleInstanceId vfModule instance id to construct the request url (required in case of delete vf
125 * @param request the SO request
126 * @return a concurrent Future for the thread that handles the request
128 public Future<SoResponse> asyncSoRestCall(final String requestId,
129 final WorkingMemory wm,
130 final String serviceInstanceId,
131 final String vnfInstanceId,
132 final String vfModuleInstanceId, final SoRequest request) {
133 return executors.submit(new AsyncSoRestCallThread(requestId, wm, serviceInstanceId, vnfInstanceId,
134 vfModuleInstanceId, request));
138 * This class handles an asynchronous request to SO as a thread.
140 private class AsyncSoRestCallThread implements Callable<SoResponse> {
141 final String requestId;
142 final WorkingMemory wm;
143 final String serviceInstanceId;
144 final String vnfInstanceId;
145 final String vfModuleInstanceId;
146 final SoRequest request;
149 * Constructor, sets the context of the request.
151 * @param requestID The request ID
152 * @param wm reference to the Drools working memory
153 * @param serviceInstanceId the service instance in SO to use
154 * @param vnfInstanceId the VNF instance that is the subject of the request
155 * @param vfModuleInstanceId the vf module instance id (not null in case of delete vf module request)
156 * @param request the request itself
158 private AsyncSoRestCallThread(final String requestId,
159 final WorkingMemory wm, final String serviceInstanceId,
160 final String vnfInstanceId, final String vfModuleInstanceId,
161 final SoRequest request) {
162 this.requestId = requestId;
164 this.serviceInstanceId = serviceInstanceId;
165 this.vnfInstanceId = vnfInstanceId;
166 this.vfModuleInstanceId = vfModuleInstanceId;
167 this.request = request;
171 * Process the asynchronous SO request.
174 public SoResponse call() {
175 String urlBase = PolicyEngine.manager.getEnvironmentProperty("so.url");
176 String username = PolicyEngine.manager.getEnvironmentProperty("so.username");
177 String password = PolicyEngine.manager.getEnvironmentProperty("so.password");
179 // Create a JSON representation of the request
180 String soJson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create().toJson(request);
182 Pair<Integer, String> httpResponse = null;
184 if (request.getOperationType() != null && request.getOperationType()
185 .equals(SoOperationType.SCALE_OUT)) {
186 url = urlBase + "/serviceInstantiation/v7/serviceInstances/" + serviceInstanceId + "/vnfs/"
187 + vnfInstanceId + "/vfModules/scaleOut";
188 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, soJson);
189 httpResponse = restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, soJson);
190 } else if (request.getOperationType() != null && request.getOperationType()
191 .equals(SoOperationType.DELETE_VF_MODULE)) {
192 url = urlBase + "/serviceInstances/v7/" + serviceInstanceId + "/vnfs/" + vnfInstanceId
193 + "/vfModules/" + vfModuleInstanceId;
194 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, soJson);
195 httpResponse = restManager.delete(url, username, password, createSimpleHeaders(), MEDIA_TYPE, soJson);
200 // Process the response from SO
201 SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
203 // Return the response to Drools in its working memory
204 SoResponseWrapper soWrapper = new SoResponseWrapper(response, requestId);
205 wm.insert(soWrapper);
212 * Wait for the SO operation we have ordered to complete.
214 * @param urlBaseSo The base URL for SO
215 * @param username user name on SO
216 * @param password password on SO
217 * @param initialRequestUrl The URL of the initial HTTP request
218 * @param initialHttpResponse The initial HTTP message returned from SO
219 * @return The parsed final response of SO to the request
221 private SoResponse waitForSoOperationCompletion(final String urlBaseSo, final String username,
222 final String password, final String initialRequestUrl,
223 final Pair<Integer, String> initialHttpResponse) {
224 // Process the initial response from SO, the response to a post
225 SoResponse response = processSoResponse(initialRequestUrl, initialHttpResponse);
226 if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
230 // The SO URL to use to get the status of orchestration requests
231 String urlGet = urlBaseSo + "/orchestrationRequests/v5/" + response.getRequestReferences().getRequestId();
233 // The HTTP status code of the latest response
234 Pair<Integer, String> latestHttpResponse = initialHttpResponse;
236 // Wait for the response from SO
237 for (int attemptsLeft = GET_REQUESTS_BEFORE_TIMEOUT; attemptsLeft >= 0; attemptsLeft--) {
238 // The SO request may have completed even on the first request so we check the
241 // issuing any other requests
242 if (isRequestStateFinished(latestHttpResponse, response)) {
246 // Wait for the defined interval before issuing a get
248 Thread.sleep(restGetTimeout);
249 } catch (InterruptedException e) {
250 logger.error("Interrupted exception: ", e);
251 Thread.currentThread().interrupt();
252 response.setHttpResponseCode(SO_RESPONSE_ERROR);
256 // Issue a GET to find the current status of our request
257 NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|{}|{}|{}|{}|]{}", "SO", urlGet, username, password,
258 createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR);
259 Pair<Integer, String> httpResponse = restManager.get(urlGet, username, password, createSimpleHeaders());
262 response = processSoResponse(urlGet, httpResponse);
263 if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
267 // Our latest HTTP response code
268 latestHttpResponse = httpResponse;
271 // We have timed out on the SO request
272 response.setHttpResponseCode(SO_RESPONSE_ERROR);
277 * Parse the response message from SO into a SOResponse object.
279 * @param requestURL The URL of the HTTP request
280 * @param httpResponse The HTTP message returned from SO
281 * @return The parsed response
283 private SoResponse processSoResponse(final String requestUrl, final Pair<Integer, String> httpResponse) {
284 SoResponse response = new SoResponse();
286 // A null httpDetails indicates a HTTP problem, a valid response from SO must be
289 if (!httpResultIsNullFree(httpResponse) || (httpResponse.first != 200 && httpResponse.first != 202)) {
290 logger.error("Invalid HTTP response received from SO");
291 response.setHttpResponseCode(SO_RESPONSE_ERROR);
295 // Parse the JSON of the response into our POJO
297 response = Serialization.gsonPretty.fromJson(httpResponse.second, SoResponse.class);
298 } catch (JsonSyntaxException e) {
299 logger.error("Failed to deserialize HTTP response into SOResponse: ", e);
300 response.setHttpResponseCode(SO_RESPONSE_ERROR);
304 // Set the HTTP response code of the response if needed
305 if (response.getHttpResponseCode() == 0) {
306 response.setHttpResponseCode(httpResponse.first);
309 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, requestUrl, httpResponse.second);
311 if (logger.isDebugEnabled()) {
312 logger.debug("***** Response to SO Request to URL {}:", requestUrl);
313 logger.debug(httpResponse.second);
320 * Method to allow tuning of REST get timeout.
322 * @param restGetTimeout the timeout value
324 protected void setRestGetTimeout(final long restGetTimeout) {
325 this.restGetTimeout = restGetTimeout;
329 * Check that the request state of a response is defined.
331 * @param response The response to check
332 * @return true if the request for the response is defined
334 private boolean isRequestStateDefined(final SoResponse response) {
335 return response != null && response.getRequest() != null && response.getRequest().getRequestStatus() != null
336 && response.getRequest().getRequestStatus().getRequestState() != null;
340 * Check that the request state of a response is finished.
342 * @param latestHttpDetails the HTTP details of the response
343 * @param response The response to check
344 * @return true if the request for the response is finished
346 private boolean isRequestStateFinished(final Pair<Integer, String> latestHttpDetails, final SoResponse response) {
347 if (latestHttpDetails != null && 200 == latestHttpDetails.first && isRequestStateDefined(response)) {
348 String requestState = response.getRequest().getRequestStatus().getRequestState();
349 return "COMPLETE".equalsIgnoreCase(requestState) || "FAILED".equalsIgnoreCase(requestState);
356 * Check that a HTTP operation result has no nulls.
358 * @param httpOperationResult the result to check
359 * @return true if no nulls are found
361 private boolean httpResultIsNullFree(Pair<Integer, String> httpOperationResult) {
362 return httpOperationResult != null && httpOperationResult.first != null && httpOperationResult.second != null;
366 * Create simple HTTP headers for unauthenticated requests to SO.
368 * @return the HTTP headers
370 private Map<String, String> createSimpleHeaders() {
371 Map<String, String> headers = new HashMap<>();
372 headers.put("Accept", MEDIA_TYPE);