836ebc85b91c68ef43f0ab64df12a99774e7be5a
[policy/drools-applications.git] / controlloop / common / model-impl / so / src / main / java / org / onap / policy / so / SoManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * so
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.so;
23
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonSyntaxException;
26 import java.util.HashMap;
27 import java.util.Map;
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;
42
43
44 /**
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
48  *
49  */
50 public final class SoManager {
51     private static final Logger logger = LoggerFactory.getLogger(SoManager.class);
52
53     private static ExecutorService executors = Executors.newCachedThreadPool();
54
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();
58
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;
62
63     // The REST manager used for processing REST calls for this VFC manager
64     private RestManager restManager;
65
66     private long restGetTimeout = GET_REQUEST_WAIT_INTERVAL;
67
68     /**
69      * Default constructor.
70      */
71     public SoManager() {
72         restManager = new RestManager();
73     }
74
75     /**
76      * Create a service instance in SO.
77      *
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
84      */
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);
93
94         // Process the response from SO
95         SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
96         if (SO_RESPONSE_ERROR != response.getHttpResponseCode()) {
97             return response;
98         } else {
99             return null;
100         }
101     }
102
103     /**
104      * Works just like SOManager#asyncSORestCall(String, WorkingMemory, String, String, String, SORequest)
105      * except the vfModuleInstanceId is always null.
106      *
107      */
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);
112     }
113
114     /**
115      * This method makes an asynchronous Rest call to MSO and inserts the response into
116      * Drools working memory.
117      *
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
123      *                           module)
124      * @param request            the SO request
125      * @return a concurrent Future for the thread that handles the request
126      */
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));
134     }
135
136     /**
137      * This class handles an asynchronous request to SO as a thread.
138      */
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;
146
147         /**
148          * Constructor, sets the context of the request.
149          *
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
156          */
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;
162             this.wm = wm;
163             this.serviceInstanceId = serviceInstanceId;
164             this.vnfInstanceId = vnfInstanceId;
165             this.vfModuleInstanceId = vfModuleInstanceId;
166             this.request = request;
167         }
168
169         /**
170          * Process the asynchronous SO request.
171          */
172         @Override
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");
177
178             // Create a JSON representation of the request
179             String soJson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create().toJson(request);
180             String url = null;
181             Pair<Integer, String> httpResponse = null;
182
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);
195             } else {
196                 return null;
197             }
198
199             // Process the response from SO
200             SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
201
202             // Return the response to Drools in its working memory
203             SoResponseWrapper soWrapper = new SoResponseWrapper(response, requestId);
204             wm.insert(soWrapper);
205
206             return response;
207         }
208     }
209
210     /**
211      * Wait for the SO operation we have ordered to complete.
212      *
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
219      */
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()) {
226             return response;
227         }
228
229         // The SO URL to use to get the status of orchestration requests
230         String urlGet = urlBaseSo + "/orchestrationRequests/v5/" + response.getRequestReferences().getRequestId();
231
232         // The HTTP status code of the latest response
233         Pair<Integer, String> latestHttpResponse = initialHttpResponse;
234
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
238             // response
239             // here before
240             // issuing any other requests
241             if (isRequestStateFinished(latestHttpResponse, response)) {
242                 return response;
243             }
244
245             // Wait for the defined interval before issuing a get
246             try {
247                 Thread.sleep(restGetTimeout);
248             } catch (InterruptedException e) {
249                 logger.error("Interrupted exception: ", e);
250                 Thread.currentThread().interrupt();
251                 response.setHttpResponseCode(SO_RESPONSE_ERROR);
252                 return response;
253             }
254
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());
259
260             // Get our response
261             response = processSoResponse(urlGet, httpResponse);
262             if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
263                 return response;
264             }
265
266             // Our latest HTTP response code
267             latestHttpResponse = httpResponse;
268         }
269
270         // We have timed out on the SO request
271         response.setHttpResponseCode(SO_RESPONSE_ERROR);
272         return response;
273     }
274
275     /**
276      * Parse the response message from SO into a SOResponse object.
277      *
278      * @param requestURL The URL of the HTTP request
279      * @param httpResponse The HTTP message returned from SO
280      * @return The parsed response
281      */
282     private SoResponse processSoResponse(final String requestUrl, final Pair<Integer, String> httpResponse) {
283         SoResponse response = new SoResponse();
284
285         // A null httpDetails indicates a HTTP problem, a valid response from SO must be
286         // either 200
287         // or 202
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);
291             return response;
292         }
293
294         // Parse the JSON of the response into our POJO
295         try {
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);
300             return response;
301         }
302
303         // Set the HTTP response code of the response if needed
304         if (response.getHttpResponseCode() == 0) {
305             response.setHttpResponseCode(httpResponse.first);
306         }
307
308         NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, requestUrl, httpResponse.second);
309
310         if (logger.isDebugEnabled()) {
311             logger.debug("***** Response to SO Request to URL {}:", requestUrl);
312             logger.debug(httpResponse.second);
313         }
314
315         return response;
316     }
317
318     /**
319      * Method to allow tuning of REST get timeout.
320      *
321      * @param restGetTimeout the timeout value
322      */
323     protected void setRestGetTimeout(final long restGetTimeout) {
324         this.restGetTimeout = restGetTimeout;
325     }
326
327     /**
328      * Check that the request state of a response is defined.
329      *
330      * @param response The response to check
331      * @return true if the request for the response is defined
332      */
333     private boolean isRequestStateDefined(final SoResponse response) {
334         return response != null && response.getRequest() != null && response.getRequest().getRequestStatus() != null
335                         && response.getRequest().getRequestStatus().getRequestState() != null;
336     }
337
338     /**
339      * Check that the request state of a response is finished.
340      *
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
344      */
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);
349         } else {
350             return false;
351         }
352     }
353
354     /**
355      * Check that a HTTP operation result has no nulls.
356      *
357      * @param httpOperationResult the result to check
358      * @return true if no nulls are found
359      */
360     private boolean httpResultIsNullFree(Pair<Integer, String> httpOperationResult) {
361         return httpOperationResult != null && httpOperationResult.first != null && httpOperationResult.second != null;
362     }
363
364     /**
365      * Create simple HTTP headers for unauthenticated requests to SO.
366      *
367      * @return the HTTP headers
368      */
369     private Map<String, String> createSimpleHeaders() {
370         Map<String, String> headers = new HashMap<>();
371         headers.put("Accept", MEDIA_TYPE);
372         return headers;
373     }
374 }