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.mso;
23 import java.util.HashMap;
26 import org.onap.policy.mso.util.Serialization;
27 import org.onap.policy.rest.RESTManager;
28 import org.onap.policy.rest.RESTManager.Pair;
30 import com.google.gson.JsonSyntaxException;
32 public final class MSOManager {
34 public static MSOResponse createModuleInstance(String url, String urlBase, String username, String password, MSORequest request) {
39 Map<String, String> headers = new HashMap<String, String>();
40 //headers.put("X-FromAppId", "POLICY");
41 //headers.put("X-TransactionId", requestID.toString());
42 headers.put("Accept", "application/json");
45 // 201 - CREATED - you are done just return
48 Pair<Integer, String> httpDetails = RESTManager.post(url, username, password, headers, "application/json", Serialization.gsonPretty.toJson(request));
50 if (httpDetails == null) {
54 if (httpDetails.a == 202) {
56 MSOResponse response = Serialization.gsonPretty.fromJson(httpDetails.b, MSOResponse.class);
58 String body = Serialization.gsonPretty.toJson(response);
59 System.out.println("***** Response to post:");
60 System.out.println(body);
62 String requestId = response.requestReferences.requestId;
63 int attemptsLeft = 20;
65 //String getUrl = "/orchestrationRequests/v2/"+requestId;
66 String urlGet = urlBase + "/orchestrationRequests/v2/"+requestId;
67 MSOResponse responseGet = null;
69 while(attemptsLeft-- > 0){
71 Pair<Integer, String> httpDetailsGet = RESTManager.get(urlGet, username, password, headers);
72 responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.b, MSOResponse.class);
73 body = Serialization.gsonPretty.toJson(responseGet);
74 System.out.println("***** Response to get:");
75 System.out.println(body);
77 if(httpDetailsGet.a == 200){
78 if(responseGet.request.requestStatus.requestState.equalsIgnoreCase("COMPLETE") ||
79 responseGet.request.requestStatus.requestState.equalsIgnoreCase("FAILED")){
80 System.out.println("***** ######## VF Module Creation "+responseGet.request.requestStatus.requestState);
87 System.out.println("***** ######## VF Module Creation timeout. Status: ("+responseGet.request.requestStatus.requestState+")");
89 } catch (JsonSyntaxException e) {
90 System.err.println("Failed to deserialize into MSOResponse" + e.getLocalizedMessage());
91 } catch (InterruptedException e) {
92 System.err.println("Interrupted exception: " + e.getLocalizedMessage());