2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.so;
23 import java.util.Base64;
24 import java.util.HashMap;
27 import org.onap.policy.so.util.Serialization;
28 import org.onap.policy.drools.system.PolicyEngine;
29 import org.onap.policy.rest.RESTManager;
30 import org.onap.policy.rest.RESTManager.Pair;
31 import org.drools.core.WorkingMemory;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.Executors;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.gson.Gson;
40 import com.google.gson.GsonBuilder;
41 import com.google.gson.JsonSyntaxException;
43 public final class SOManager {
45 private static final Logger logger = LoggerFactory.getLogger(SOManager.class);
46 private static final Logger netLogger = LoggerFactory.getLogger(org.onap.policy.drools.event.comm.Topic.NETWORK_LOGGER);
47 private static ExecutorService executors = Executors.newCachedThreadPool();
49 public static SOResponse createModuleInstance(String url, String urlBase, String username, String password, SORequest request) {
54 Map<String, String> headers = new HashMap<String, String>();
55 //headers.put("X-FromAppId", "POLICY");
56 //headers.put("X-TransactionId", requestID.toString());
57 headers.put("Accept", "application/json");
60 // 201 - CREATED - you are done just return
62 String requestJson = Serialization.gsonPretty.toJson(request);
63 netLogger.info("[OUT|{}|{}|]{}{}", "SO", url, System.lineSeparator(), requestJson);
64 Pair<Integer, String> httpDetails = RESTManager.post(url, username, password, headers, "application/json", requestJson);
66 if (httpDetails == null) {
70 if (httpDetails.a == 202) {
72 SOResponse response = Serialization.gsonPretty.fromJson(httpDetails.b, SOResponse.class);
74 String body = Serialization.gsonPretty.toJson(response);
75 logger.debug("***** Response to post:");
78 String requestId = response.requestReferences.requestId;
79 int attemptsLeft = 20;
81 //String getUrl = "/orchestrationRequests/v2/"+requestId;
82 String urlGet = urlBase + "/orchestrationRequests/v2/"+requestId;
83 SOResponse responseGet = null;
85 while(attemptsLeft-- > 0){
87 Pair<Integer, String> httpDetailsGet = RESTManager.get(urlGet, username, password, headers);
88 responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.b, SOResponse.class);
89 netLogger.info("[IN|{}|{}|]{}{}", "SO", urlGet, System.lineSeparator(), httpDetailsGet.b);
91 body = Serialization.gsonPretty.toJson(responseGet);
92 logger.debug("***** Response to get:");
95 if(httpDetailsGet.a == 200){
96 if(responseGet.request.requestStatus.requestState.equalsIgnoreCase("COMPLETE") ||
97 responseGet.request.requestStatus.requestState.equalsIgnoreCase("FAILED")){
98 logger.debug("***** ######## VF Module Creation "+responseGet.request.requestStatus.requestState);
105 if (responseGet != null
106 && responseGet.request != null
107 && responseGet.request.requestStatus != null
108 && responseGet.request.requestStatus.requestState != null) {
109 logger.warn("***** ######## VF Module Creation timeout. Status: ( {})", responseGet.request.requestStatus.requestState);
113 } catch (JsonSyntaxException e) {
114 logger.error("Failed to deserialize into SOResponse: ", e);
115 } catch (InterruptedException e) {
116 logger.error("Interrupted exception: ", e);
117 Thread.currentThread().interrupt();
136 * This method makes an asynchronous Rest call to MSO and inserts the response into the Drools working memory
138 public void asyncSORestCall(WorkingMemory wm, String serviceInstanceId, String vnfInstanceId, SORequest request) {
139 executors.submit(new Runnable()
145 String serverRoot = PolicyEngine.manager.getEnvironmentProperty("so.url");
146 String username = PolicyEngine.manager.getEnvironmentProperty("so.username");
147 String password = PolicyEngine.manager.getEnvironmentProperty("so.password");
149 String url = serverRoot + "/serviceInstances/v5/" + serviceInstanceId + "/vnfs/" + vnfInstanceId + "/vfModulesHTTPS/1.1";
151 String auth = username + ":" + password;
153 Map<String, String> headers = new HashMap<String, String>();
154 byte[] encodedBytes = Base64.getEncoder().encode(auth.getBytes());
155 headers.put("Accept", "application/json");
156 headers.put("Authorization", "Basic " + new String(encodedBytes));
158 Gson gsonPretty = new GsonBuilder().disableHtmlEscaping()
162 String soJson = gsonPretty.toJson(request);
164 SOResponse so = new SOResponse();
165 netLogger.info("[OUT|{}|{}|]{}{}", "SO", url, System.lineSeparator(), soJson);
166 Pair<Integer, String> httpResponse = RESTManager.post(url, "policy", "policy", headers, "application/json", soJson);
168 if (httpResponse != null) {
169 netLogger.info("[IN|{}|{}|]{}{}", url, "SO", System.lineSeparator(), httpResponse.b);
171 Gson gson = new Gson();
172 so = gson.fromJson(httpResponse.b, SOResponse.class);
173 so.httpResponseCode = httpResponse.a;
175 logger.error("SO Response returned null.");
176 so.httpResponseCode = 999;
180 logger.info("SOResponse inserted " + gsonPretty.toJson(so));
181 } catch (Exception e) {
182 logger.error("Error while performing asyncSORestCall: "+ e.getMessage(),e);
184 // create dummy SO object to trigger cleanup
185 SOResponse so = new SOResponse();
186 so.httpResponseCode = 999;