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