[POLICY-11] Sample Query with variable arguments
[policy/drools-applications.git] / controlloop / common / model-impl / mso / src / main / java / org / onap / policy / mso / MSOManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * mso
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.mso;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.onap.policy.mso.util.Serialization;
27 import org.onap.policy.rest.RESTManager;
28 import org.onap.policy.rest.RESTManager.Pair;
29
30 import com.google.gson.JsonSyntaxException;
31
32 public final class MSOManager {
33
34         public static MSOResponse createModuleInstance(String url, String urlBase, String username, String password, MSORequest request) {
35                 
36                 //
37                 // Call REST
38                 //
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");
43                 
44                 //
45                 // 201 - CREATED - you are done just return 
46                 //
47                 
48                 Pair<Integer, String> httpDetails = RESTManager.post(url, username, password, headers, "application/json", Serialization.gsonPretty.toJson(request));
49                 
50                 if (httpDetails == null) {
51                         return null;
52                 }
53                 
54                 if (httpDetails.a == 202) {
55                         try {
56                                 MSOResponse response = Serialization.gsonPretty.fromJson(httpDetails.b, MSOResponse.class);
57                                 
58                                 String body = Serialization.gsonPretty.toJson(response);
59                                 System.out.println("***** Response to post:");
60                                 System.out.println(body);
61                                 
62                                 String requestId = response.requestReferences.requestId;
63                                 int attemptsLeft = 20;
64                                 
65                                 //String getUrl = "/orchestrationRequests/v2/"+requestId;
66                                 String urlGet = urlBase + "/orchestrationRequests/v2/"+requestId;
67                                 MSOResponse responseGet = null;
68                                 
69                                 while(attemptsLeft-- > 0){
70                                         
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);
76                                         
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);
81                                                         return responseGet;
82                                                 }
83                                         }
84                                         Thread.sleep(20000);
85                                 }
86                                 
87                                 System.out.println("***** ########  VF Module Creation timeout. Status: ("+responseGet.request.requestStatus.requestState+")");
88                                 return responseGet;
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());
93                         }
94                 }
95                 
96                 
97                 
98                 
99                 return null;
100         }
101
102 }