Clean up of Pair classes - models
[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-2020 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.apache.commons.lang3.tuple.Pair;
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.rest.RestManager;
38 import org.onap.policy.so.util.Serialization;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * This class handles the interface towards SO (Service Orchestrator) for the ONAP Policy Framework. The SO API is
44  * defined at this link:
45  * http://onap.readthedocs.io/en/latest/submodules/so.git/docs/SO_R1_Interface.html#get-orchestration-request
46  *
47  */
48 public final class SoManager {
49
50     // TODO remove this class
51
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     private String url;
70     private String user;
71     private String password;
72
73     @FunctionalInterface
74     public interface SoCallback {
75         public void onSoResponseWrapper(SoResponseWrapper wrapper);
76     }
77
78     /**
79      * Default constructor.
80      */
81     public SoManager(String url, String user, String password) {
82         this.url = url;
83         this.user = user;
84         this.password = password;
85         restManager = new RestManager();
86     }
87
88     /**
89      * Create a service instance in SO.
90      *
91      * @param url the SO URL
92      * @param urlBase the base URL
93      * @param username user name on SO
94      * @param password password on SO
95      * @param request the request to issue to SO
96      * @return the SO Response object
97      */
98     public SoResponse createModuleInstance(final String url, final String urlBase, final String username,
99             final String password, final SoRequest request) {
100         // Issue the HTTP POST request to SO to create the service instance
101         String requestJson = Serialization.gsonPretty.toJson(request);
102         NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|{}|{}|{}|{}|]{}{}", "SO", url, username, password,
103                 createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR, requestJson);
104         Pair<Integer, String> httpResponse =
105                 restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, requestJson);
106
107         // Process the response from SO
108         SoResponse response = waitForSoOperationCompletion(urlBase, username, password, url, httpResponse);
109         if (SO_RESPONSE_ERROR != response.getHttpResponseCode()) {
110             return response;
111         } else {
112             return null;
113         }
114     }
115
116     /**
117      * Works just like SOManager#asyncSORestCall(String, WorkingMemory, String, String, String, SORequest) except the
118      * vfModuleInstanceId is always null.
119      *
120      */
121     public Future<SoResponse> asyncSoRestCall(final String requestId, final SoCallback callback,
122             final String serviceInstanceId, final String vnfInstanceId, final SoRequest request) {
123         return asyncSoRestCall(requestId, callback, serviceInstanceId, vnfInstanceId, null, request);
124     }
125
126     /**
127      * This method makes an asynchronous Rest call to MSO and inserts the response into Drools working memory.
128      *
129      * @param requestId the request id
130      * @param callback callback method
131      * @param serviceInstanceId service instance id to construct the request url
132      * @param vnfInstanceId vnf instance id to construct the request url
133      * @param vfModuleInstanceId vfModule instance id to construct the request url (required in case of delete vf
134      *        module)
135      * @param request the SO request
136      * @return a concurrent Future for the thread that handles the request
137      */
138     public Future<SoResponse> asyncSoRestCall(final String requestId, final SoCallback callback,
139             final String serviceInstanceId, final String vnfInstanceId, final String vfModuleInstanceId,
140             final SoRequest request) {
141         return executors.submit(new AsyncSoRestCallThread(requestId, callback, serviceInstanceId, vnfInstanceId,
142                 vfModuleInstanceId, request, this));
143     }
144
145     /**
146      * This class handles an asynchronous request to SO as a thread.
147      */
148     private class AsyncSoRestCallThread implements Callable<SoResponse> {
149         final String requestId;
150         final SoCallback callback;
151         final String serviceInstanceId;
152         final String vnfInstanceId;
153         final String vfModuleInstanceId;
154         final SoRequest request;
155         final String baseUrl;
156         final String user;
157         final String password;
158
159         /**
160          * Constructor, sets the context of the request.
161          *
162          * @param requestID The request ID
163          * @param wm reference to the Drools working memory
164          * @param serviceInstanceId the service instance in SO to use
165          * @param vnfInstanceId the VNF instance that is the subject of the request
166          * @param vfModuleInstanceId the vf module instance id (not null in case of delete vf module request)
167          * @param request the request itself
168          */
169         private AsyncSoRestCallThread(final String requestId, final SoCallback callback, final String serviceInstanceId,
170                 final String vnfInstanceId, final String vfModuleInstanceId, final SoRequest request,
171                 final SoManager callingSoManager) {
172             this.requestId = requestId;
173             this.callback = callback;
174             this.serviceInstanceId = serviceInstanceId;
175             this.vnfInstanceId = vnfInstanceId;
176             this.vfModuleInstanceId = vfModuleInstanceId;
177             this.request = request;
178             this.baseUrl = callingSoManager.url;
179             this.user = callingSoManager.user;
180             this.password = callingSoManager.password;
181         }
182
183         /**
184          * Process the asynchronous SO request.
185          */
186         @Override
187         public SoResponse call() {
188
189             // Create a JSON representation of the request
190             String soJson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create().toJson(request);
191             String initialUrl = null;
192             Pair<Integer, String> httpResponse = null;
193
194             if (request.getOperationType() != null && request.getOperationType().equals(SoOperationType.SCALE_OUT)) {
195                 initialUrl = this.baseUrl + "/serviceInstantiation/v7/serviceInstances/" + serviceInstanceId + "/vnfs/"
196                         + vnfInstanceId + "/vfModules/scaleOut";
197                 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, initialUrl, soJson);
198                 httpResponse = restManager.post(initialUrl, this.user, this.password, createSimpleHeaders(), MEDIA_TYPE,
199                         soJson);
200             } else if (request.getOperationType() != null
201                     && request.getOperationType().equals(SoOperationType.DELETE_VF_MODULE)) {
202                 initialUrl = this.baseUrl + "/serviceInstances/v7/" + serviceInstanceId + "/vnfs/" + vnfInstanceId
203                         + "/vfModules/" + vfModuleInstanceId;
204                 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, initialUrl, soJson);
205                 httpResponse = restManager.delete(initialUrl, this.user, this.password, createSimpleHeaders(),
206                         MEDIA_TYPE, soJson);
207             } else {
208                 return null;
209             }
210
211             // Process the response from SO
212             SoResponse response =
213                     waitForSoOperationCompletion(this.baseUrl, this.user, this.password, initialUrl, httpResponse);
214
215             // Return the response to Drools in its working memory
216             SoResponseWrapper soWrapper = new SoResponseWrapper(response, requestId);
217             if (this.callback != null) {
218                 this.callback.onSoResponseWrapper(soWrapper);
219             }
220
221             return response;
222         }
223     }
224
225     /**
226      * Wait for the SO operation we have ordered to complete.
227      *
228      * @param urlBaseSo The base URL for SO
229      * @param username user name on SO
230      * @param password password on SO
231      * @param initialRequestUrl The URL of the initial HTTP request
232      * @param initialHttpResponse The initial HTTP message returned from SO
233      * @return The parsed final response of SO to the request
234      */
235     private SoResponse waitForSoOperationCompletion(final String urlBaseSo, final String username,
236             final String password, final String initialRequestUrl, final Pair<Integer, String> initialHttpResponse) {
237         // Process the initial response from SO, the response to a post
238         SoResponse response = processSoResponse(initialRequestUrl, initialHttpResponse);
239         if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
240             return response;
241         }
242
243         // The SO URL to use to get the status of orchestration requests
244         String urlGet = urlBaseSo + "/orchestrationRequests/v5/" + response.getRequestReferences().getRequestId();
245
246         // The HTTP status code of the latest response
247         Pair<Integer, String> latestHttpResponse = initialHttpResponse;
248
249         // Wait for the response from SO
250         for (int attemptsLeft = GET_REQUESTS_BEFORE_TIMEOUT; attemptsLeft >= 0; attemptsLeft--) {
251             // The SO request may have completed even on the first request so we check the
252             // response
253             // here before
254             // issuing any other requests
255             if (isRequestStateFinished(latestHttpResponse, response)) {
256                 return response;
257             }
258
259             // Wait for the defined interval before issuing a get
260             try {
261                 Thread.sleep(restGetTimeout);
262             } catch (InterruptedException e) {
263                 logger.error("Interrupted exception: ", e);
264                 Thread.currentThread().interrupt();
265                 response.setHttpResponseCode(SO_RESPONSE_ERROR);
266                 return response;
267             }
268
269             // Issue a GET to find the current status of our request
270             NetLoggerUtil.getNetworkLogger().info("[OUT|{}|{}|{}|{}|{}|{}|]{}", "SO", urlGet, username, password,
271                     createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR);
272             Pair<Integer, String> httpResponse = restManager.get(urlGet, username, password, createSimpleHeaders());
273
274             // Get our response
275             response = processSoResponse(urlGet, httpResponse);
276             if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) {
277                 return response;
278             }
279
280             // Our latest HTTP response code
281             latestHttpResponse = httpResponse;
282         }
283
284         // We have timed out on the SO request
285         response.setHttpResponseCode(SO_RESPONSE_ERROR);
286         return response;
287     }
288
289     /**
290      * Parse the response message from SO into a SOResponse object.
291      *
292      * @param requestURL The URL of the HTTP request
293      * @param httpResponse The HTTP message returned from SO
294      * @return The parsed response
295      */
296     private SoResponse processSoResponse(final String requestUrl, final Pair<Integer, String> httpResponse) {
297         SoResponse response = new SoResponse();
298
299         // A null httpDetails indicates a HTTP problem, a valid response from SO must be
300         // either 200
301         // or 202
302         if (!httpResultIsNullFree(httpResponse) || (httpResponse.getLeft() != 200 && httpResponse.getLeft() != 202)) {
303             logger.error("Invalid HTTP response received from SO");
304             response.setHttpResponseCode(SO_RESPONSE_ERROR);
305             return response;
306         }
307
308         // Parse the JSON of the response into our POJO
309         try {
310             response = Serialization.gsonPretty.fromJson(httpResponse.getRight(), SoResponse.class);
311         } catch (JsonSyntaxException e) {
312             logger.error("Failed to deserialize HTTP response into SOResponse: ", e);
313             response.setHttpResponseCode(SO_RESPONSE_ERROR);
314             return response;
315         }
316
317         // Set the HTTP response code of the response if needed
318         if (response.getHttpResponseCode() == 0) {
319             response.setHttpResponseCode(httpResponse.getLeft());
320         }
321
322         NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, requestUrl, httpResponse.getRight());
323
324         if (logger.isDebugEnabled()) {
325             logger.debug("***** Response to SO Request to URL {}:", requestUrl);
326             logger.debug(httpResponse.getRight());
327         }
328
329         return response;
330     }
331
332     /**
333      * Method to allow tuning of REST get timeout.
334      *
335      * @param restGetTimeout the timeout value
336      */
337     protected void setRestGetTimeout(final long restGetTimeout) {
338         this.restGetTimeout = restGetTimeout;
339     }
340
341     /**
342      * Check that the request state of a response is defined.
343      *
344      * @param response The response to check
345      * @return true if the request for the response is defined
346      */
347     private boolean isRequestStateDefined(final SoResponse response) {
348         return response != null && response.getRequest() != null && response.getRequest().getRequestStatus() != null
349                 && response.getRequest().getRequestStatus().getRequestState() != null;
350     }
351
352     /**
353      * Check that the request state of a response is finished.
354      *
355      * @param latestHttpDetails the HTTP details of the response
356      * @param response The response to check
357      * @return true if the request for the response is finished
358      */
359     private boolean isRequestStateFinished(final Pair<Integer, String> latestHttpDetails, final SoResponse response) {
360         if (latestHttpDetails != null && 200 == latestHttpDetails.getLeft() && isRequestStateDefined(response)) {
361             String requestState = response.getRequest().getRequestStatus().getRequestState();
362             return "COMPLETE".equalsIgnoreCase(requestState) || "FAILED".equalsIgnoreCase(requestState);
363         } else {
364             return false;
365         }
366     }
367
368     /**
369      * Check that a HTTP operation result has no nulls.
370      *
371      * @param httpOperationResult the result to check
372      * @return true if no nulls are found
373      */
374     private boolean httpResultIsNullFree(Pair<Integer, String> httpOperationResult) {
375         return httpOperationResult != null && httpOperationResult.getLeft() != null
376                 && httpOperationResult.getRight() != null;
377     }
378
379     /**
380      * Create simple HTTP headers for unauthenticated requests to SO.
381      *
382      * @return the HTTP headers
383      */
384     private Map<String, String> createSimpleHeaders() {
385         Map<String, String> headers = new HashMap<>();
386         headers.put("Accept", MEDIA_TYPE);
387         return headers;
388     }
389 }