[POLICY-11] Sample Query with variable arguments
[policy/drools-applications.git] / controlloop / common / model-impl / aai / src / main / java / org / onap / policy / aai / AAINQF199 / AAINQF199Manager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * aai
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.aai.AAINQF199;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.onap.policy.aai.AAIGETResponse;
28 import org.onap.policy.aai.util.Serialization;
29 import org.onap.policy.rest.RESTManager;
30 import org.onap.policy.rest.RESTManager.Pair;
31
32 import com.google.gson.JsonSyntaxException;
33
34 public final class AAINQF199Manager {
35         
36         public static AAINQF199Response postQuery(String url, String username, String password, AAINQF199Request request, UUID requestID) {
37                 
38                 Map<String, String> headers = new HashMap<String, String>();
39                 headers.put("X-FromAppId", "POLICY");
40                 headers.put("X-TransactionId", requestID.toString());
41                 headers.put("Accept", "application/json");
42                 
43                 url = url + "/aai/search/named-query";
44
45                 Pair<Integer, String> httpDetails = RESTManager.post(url, username, password, headers, "application/json", Serialization.gsonPretty.toJson(request));
46
47                 if (httpDetails == null) {
48                         System.out.println("AAI POST Null Response to " + url);
49                         return null;
50                 }
51                 
52                 System.out.println(url);
53                 System.out.println(httpDetails.a);
54                 System.out.println(httpDetails.b);
55                 if (httpDetails.a == 200) {
56                         try {
57                                 AAINQF199Response response = Serialization.gsonPretty.fromJson(httpDetails.b, AAINQF199Response.class);
58                                 return response;
59                         } catch (JsonSyntaxException e) {
60                                 System.err.println("Failed to deserialize into AAIResponse" + e.getLocalizedMessage());
61                         }
62                 }
63
64                 return null;
65         }
66         
67         public static AAIGETResponse getQuery(String urlGet, String username, String password, UUID requestID, String vnfId) {
68                 
69                 Map<String, String> headers = new HashMap<String, String>();
70                 headers.put("X-FromAppId", "POLICY");
71                 headers.put("X-TransactionId", requestID.toString());
72                 headers.put("Accept", "application/json");
73                 
74                 urlGet = urlGet + "/aai/v8/network/generic-vnfs/generic-vnf/" + vnfId;
75                 
76                 int attemptsLeft = 3;
77                 AAIGETResponse responseGet = null;
78                 
79                 while(attemptsLeft-- > 0){
80                 
81                         Pair<Integer, String> httpDetailsGet = RESTManager.get(urlGet, username, password, headers);
82                         if (httpDetailsGet == null) {
83                                 System.out.println("AAI GET Null Response to " + urlGet);
84                                 return null;
85                         }
86                         
87                         System.out.println(urlGet);
88                         System.out.println(httpDetailsGet.a);
89                         System.out.println(httpDetailsGet.b);
90                         
91                         if (httpDetailsGet.a == 200) {
92                                 try {
93                                         responseGet = Serialization.gsonPretty.fromJson(httpDetailsGet.b, AAIGETResponse.class);
94                                         return responseGet;
95                                 } catch (JsonSyntaxException e) {
96                                         System.err.println("Failed to deserialize into AAIResponse" + e.getLocalizedMessage());
97                                 }
98                         }
99                         try {
100                                 Thread.sleep(1000);
101                         } catch (InterruptedException e) {}
102
103                 }
104                 
105                 return null;
106         }
107
108 }