Merge "move two modules from drools-application to models"
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / AaiSimulatorJaxRs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.TreeMap;
29 import java.util.UUID;
30
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.core.MediaType;
39 import org.apache.commons.io.IOUtils;
40 import org.onap.policy.aai.AaiNqRequest;
41 import org.onap.policy.aai.util.Serialization;
42
43 @Path("/aai")
44 public class AaiSimulatorJaxRs {
45
46     private static final String VSERVER = "vserver";
47     private static final String DISABLE_CLOSEDLOOP = "disableClosedLoop";
48     private static final String ERROR = "error";
49     private static final String GETFAIL = "getFail";
50
51     /**
52      * A&AI get query.
53      * 
54      * @param vnfId the VNF Id
55      * @return the result
56      */
57     @GET
58     @Path("/v8/network/generic-vnfs/generic-vnf/{vnfId}")
59     @Consumes(MediaType.APPLICATION_JSON)
60     @Produces("application/json")
61     public String aaiGetQuery(@PathParam("vnfID") final String vnfId) {
62         return "{\"relationship-list\": {\"relationship\":[{\"related-to-property\": [{\"property-key\": "
63                 + "\"service-instance.service-instance-name\"}]},{\"related-to-property\": [ {\"property-key\": "
64                 + "\"vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
65     }
66
67     /**
68      * A&AI post query.
69      * 
70      * @param req the request
71      * @return the response
72      * @throws IOException if a response file cannot be read
73      */
74     @POST
75     @Path("/search/named-query")
76     @Consumes(MediaType.APPLICATION_JSON)
77     @Produces("application/json")
78     public String aaiPostQuery(final String req) throws IOException {
79         final AaiNqRequest request = Serialization.gsonPretty.fromJson(req, AaiNqRequest.class);
80
81         if (request.getInstanceFilters().getInstanceFilter().get(0).containsKey(VSERVER)) {
82             final String vserverName =
83                     request.getInstanceFilters().getInstanceFilter().get(0).get(VSERVER).get("vserver-name");
84             if (ERROR.equals(vserverName)) {
85                 Map<String,String> params = new TreeMap<>();
86                 params.put("type", VSERVER);
87                 return load("aai/AaiNqResponse-Error.json", params);
88             } else {
89                 // vll format - new
90                 // new aai response from Brian 11/13/2017
91                 return load("aai/AaiNqResponse-Vserver.json", new TreeMap<>());
92             }
93         } else {
94             final String vnfId =
95                     request.getInstanceFilters().getInstanceFilter().get(0).get("generic-vnf").get("vnf-id");
96             if (ERROR.equals(vnfId)) {
97                 Map<String,String> params = new TreeMap<>();
98                 params.put("type", "generic-vnf");
99                 return load("aai/AaiNqResponse-Error.json", params);
100             } else {
101                 Map<String, String> params = new TreeMap<>();
102                 params.put("vnfId", "" + vnfId);
103                 params.put("vnfName", getUuidValue(vnfId, "ZRDM2MMEX39"));
104                 params.put("pnfVndName", "pnf-test-" + vnfId);
105                 params.put("pnfVnfId", getUuidValue(params.get("pnfVndName"), "jimmy-test"));
106
107                 params.put("serviceInstanceVnfName", "service-instance-test-" + vnfId);
108                 params.put("serviceInstanceVnfId", 
109                         getUuidValue(params.get("serviceInstanceVnfName"), "jimmy-test-vnf2"));
110
111                 return load("aai/AaiNqResponse-GenericVnf.json", params);
112             }
113         }
114     }
115
116     /**
117      * Get by VNF name.
118      * 
119      * @param vnfName the VNF name
120      * @return the response
121      */
122     @GET
123     @Path("/v11/network/generic-vnfs/generic-vnf")
124     @Consumes(MediaType.APPLICATION_JSON)
125     @Produces("application/json")
126     public String getByVnfName(@QueryParam("vnf-name") final String vnfName) {
127         if (GETFAIL.equals(vnfName)) {
128             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
129                     + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
130                     + "generic-vnf\",\"Node Not Found:No Node of type generic-vnf found at network/generic-vnfs"
131                     + "/generic-vnf\",\"ERR.5.4.6114\"]}}}";
132         }
133         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfName);
134         if (ERROR.equals(vnfName)) {
135             return "{ \"vnf-id\": \"error\", \"vnf-name\": \"" + vnfName
136                     + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
137                     + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \""
138                     + "ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
139                     + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
140                     + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
141                     + "\"is-closed-loop-disabled\": " + isDisabled
142                     + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
143                     + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer/"
144                     + "1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
145                     + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
146                     + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
147                     + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
148                     + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
149                     + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01"
150                     + "\"} ], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name"
151                     + "\"}]},{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/"
152                     + "cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/"
153                     + "USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver/3b2558f4-39d8-40e7-bfc7-30660fb52c45"
154                     + "\", \"relationship-data\":[{ \"relationship-key\": \"cloud-region.cloud-owner\", \""
155                     + "relationship-value\": \"att-aic\"},{ \"relationship-key\": \"cloud-region.cloud-region-id"
156                     + "\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \"tenant.tenant-id"
157                     + "\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \"relationship-key\": \""
158                     + "vserver.vserver-id\", \"relationship-value\": \"3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \""
159                     + "related-to-property\": [ {\"property-key\": \"vserver.vserver-name\",\"property-value\": \""
160                     + "USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
161
162         }
163         final String vnfId = getUuidValue(vnfName, "5e49ca06-2972-4532-9ed4-6d071588d792");
164         return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
165                 + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
166                 + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \"ATT"
167                 + "\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
168                 + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
169                 + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
170                 + "\"is-closed-loop-disabled\": " + isDisabled
171                 + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
172                 + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer"
173                 + "/1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
174                 + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
175                 + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
176                 + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
177                 + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
178                 + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} ], \""
179                 + "related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},{ \""
180                 + "related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
181                 + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
182                 + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
183                 + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
184                 + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
185                 + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
186                 + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
187                 + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
188                 + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
189     }
190
191     /**
192      * Get by VNF Id.
193      * 
194      * @param vnfId the VNF Id
195      * @return the response
196      */
197     @GET
198     @Path("/v11/network/generic-vnfs/generic-vnf/{vnfId}")
199     @Consumes(MediaType.APPLICATION_JSON)
200     @Produces("application/json")
201     public String getByVnfId(@PathParam("vnfId") final String vnfId) {
202         if (GETFAIL.equals(vnfId)) {
203             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
204                     + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
205                     + "generic-vnf/getFail\",\"Node Not Found:No Node of type generic-vnf found at network/"
206                     + "generic-vnfs/generic-vnf/getFail\",\"ERR.5.4.6114\"]}}}";
207         }
208         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfId);
209         final String vnfName = getUuidValue(vnfId, "USUCP0PCOIL0110UJRT01");
210         return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
211                 + "\", \"vnf-type\": \"RT\", \"service-id\": \""
212                 + "d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \"equipment-role\": \"UCPE\", \"orchestration-status"
213                 + "\": \"created\", \"management-option\": \"ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \""
214                 + "ipv4-loopback0-address\": \"32.40.64.57\", \"nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345"
215                 + "\", \"management-v6-address\": \"2001:1890:e00e:fffd::36\", \"in-maint\": false, " 
216                 + "\"prov-status\":\"ACTIVE\", \"" + ""
217                 + "is-closed-loop-disabled\": " + isDisabled + ", \"resource-version\": \"1493389458092\", \""
218                 + "relationship-list\": {\"relationship\":[{ \"related-to\": \"service-instance\", \"related-link"
219                 + "\": \"/aai/v11/business/customers/customer/1610_Func_Global_20160817084727/service-subscriptions"
220                 + "/service-subscription/uCPE-VMS/service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \""
221                 + "relationship-data\":[{ \"relationship-key\": \"customer.global-customer-id\", \""
222                 + "relationship-value\": \"1610_Func_Global_20160817084727\"},{ \"relationship-key\": \""
223                 + "service-subscription.service-type\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key"
224                 + "\": \"service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} "
225                 + "], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},"
226                 + "{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
227                 + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
228                 + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
229                 + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
230                 + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
231                 + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
232                 + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
233                 + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
234                 + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
235     }
236
237     /**
238      * Get by VServer name.
239      * 
240      * @param vserverName the VServer name
241      * @return the response
242      */
243     @GET
244     @Path("/v11/nodes/vservers")
245     @Consumes(MediaType.APPLICATION_JSON)
246     @Produces("application/json")
247     public String getByVserverName(@QueryParam("vserver-name") final String vserverName) {
248         if (GETFAIL.equals(vserverName)) {
249             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
250                     + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"nodes/vservers\",\"Node Not"
251                     + " Found:No Node of type generic-vnf found at nodes/vservers\",\"ERR.5.4.6114\"]}}}";
252         }
253         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vserverName);
254         final String vserverId = getUuidValue(vserverName, "d0668d4f-c25e-4a1b-87c4-83845c01efd8");
255         return "{\"vserver\": [{ \"vserver-id\": \"" + vserverId + "\", \"vserver-name\": \"" + vserverName
256                 + "\", \"vserver-name2\": \"vjunos0\", \"vserver-selflink\": \"https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8\", \"in-maint\": false, \"is-closed-loop-disabled\": "
257                 + isDisabled
258                 + ", \"prov-status\":\"ACTIVE\", \"resource-version\": \"1494001931513\", " 
259                 + "\"relationship-list\": {\"relationship\":[{ \"related-to"
260                 + "\": \"generic-vnf\", \"related-link\": \"/aai/v11/network/generic-vnfs/generic-vnf/"
261                 + "e1a41e99-4ede-409a-8f9d-b5e12984203a\", \"relationship-data\": [ {\"relationship-key\": \""
262                 + "generic-vnf.vnf-id\",\"relationship-value\": \"e1a41e99-4ede-409a-8f9d-b5e12984203a\" }], \""
263                 + "related-to-property\": [ {\"property-key\": \"generic-vnf.vnf-name\",\"property-value\": \""
264                 + "USMSO1SX7NJ0103UJSW01\" }]},{ \"related-to\": \"pserver\", \"related-link\": \"/aai/v11/"
265                 + "cloud-infrastructure/pservers/pserver/USMSO1SX7NJ0103UJZZ01\", \"relationship-data\": [ {\""
266                 + "relationship-key\": \"pserver.hostname\",\"relationship-value\": \"USMSO1SX7NJ0103UJZZ01\" }], \""
267                 + "related-to-property\": [{\"property-key\": \"pserver.pserver-name2\"}]} ]}}]}";
268     }
269
270     private String getUuidValue(final String value, final String defaultValue) {
271         return value != null ? UUID.nameUUIDFromBytes(value.getBytes()).toString() : defaultValue;
272     }
273
274     /**
275      * Loads a JSON response from a file and then replaces parameters of the form, ${xxx},
276      * with values.
277      * 
278      * @param fileName name of the file containing the JSON
279      * @param params parameters to be substituted
280      * @return the JSON response, after parameter substitution
281      * @throws IOException if the file cannot be read
282      */
283     private String load(String fileName, Map<String, String> params) throws IOException {
284         String json = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
285
286         // perform parameter substitution
287         for (Entry<String, String> ent : params.entrySet()) {
288             String name = "${" + ent.getKey() + "}";
289             String value = ent.getValue();
290             json = json.replace(name, value);
291         }
292
293         return json;
294     }
295 }