Fix sonar issue affecting in drools-applications
[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 import javax.ws.rs.Consumes;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
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 get query.
69      *
70      * @return the result
71      */
72     @GET
73     @Path("/v16/search/nodes-query")
74     @Consumes(MediaType.APPLICATION_JSON)
75     @Produces("application/json")
76     public String aaiGetVserverQuery() {
77         return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
78                 + "cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants"
79                 + "/tenant/3f2aaef74ecb4b19b35e26d0849fe9a2/vservers/vserver/"
80                 + "6c3b3714-e36c-45af-9f16-7d3a73d99497\"}]}}";
81     }
82
83
84     /**
85      * A&AI put query.
86      *
87      * @param req the request
88      * @return the response
89      * @throws IOException if a response file cannot be read
90      */
91     @PUT
92     @Path("/v16/query")
93     @Consumes(MediaType.APPLICATION_JSON)
94     @Produces("application/json")
95     public String aaiPutQuery(final String req) throws IOException {
96         return IOUtils.toString(getClass().getResource("aai/AaiCqResponse.json"), StandardCharsets.UTF_8);
97     }
98
99     /**
100      * A&AI post query.
101      *
102      * @param req the request
103      * @return the response
104      * @throws IOException if a response file cannot be read
105      */
106     @POST
107     @Path("/search/named-query")
108     @Consumes(MediaType.APPLICATION_JSON)
109     @Produces("application/json")
110     public String aaiPostQuery(final String req) throws IOException {
111         final AaiNqRequest request = Serialization.gsonPretty.fromJson(req, AaiNqRequest.class);
112
113         if (request.getInstanceFilters().getInstanceFilter().get(0).containsKey(VSERVER)) {
114             final String vserverName =
115                     request.getInstanceFilters().getInstanceFilter().get(0).get(VSERVER).get("vserver-name");
116             if (ERROR.equals(vserverName)) {
117                 Map<String, String> params = new TreeMap<>();
118                 params.put("type", VSERVER);
119                 return load("aai/AaiNqResponse-Error.json", params);
120             } else {
121                 // vll format - new
122                 // new aai response from Brian 11/13/2017
123                 return load("aai/AaiNqResponse-Vserver.json", new TreeMap<>());
124             }
125         } else {
126             final String vnfId =
127                     request.getInstanceFilters().getInstanceFilter().get(0).get("generic-vnf").get("vnf-id");
128             if (ERROR.equals(vnfId)) {
129                 Map<String, String> params = new TreeMap<>();
130                 params.put("type", "generic-vnf");
131                 return load("aai/AaiNqResponse-Error.json", params);
132             } else {
133                 Map<String, String> params = new TreeMap<>();
134                 params.put("vnfId", "" + vnfId);
135                 params.put("vnfName", getUuidValue(vnfId, "ZRDM2MMEX39"));
136                 params.put("pnfVndName", "pnf-test-" + vnfId);
137                 params.put("pnfVnfId", getUuidValue(params.get("pnfVndName"), "jimmy-test"));
138
139                 params.put("serviceInstanceVnfName", "service-instance-test-" + vnfId);
140                 params.put("serviceInstanceVnfId",
141                         getUuidValue(params.get("serviceInstanceVnfName"), "jimmy-test-vnf2"));
142
143                 return load("aai/AaiNqResponse-GenericVnf.json", params);
144             }
145         }
146     }
147
148     /**
149      * Get by VNF name.
150      *
151      * @param vnfName the VNF name
152      * @return the response
153      */
154     @GET
155     @Path("/v11/network/generic-vnfs/generic-vnf")
156     @Consumes(MediaType.APPLICATION_JSON)
157     @Produces("application/json")
158     public String getByVnfName(@QueryParam("vnf-name") final String vnfName) {
159         if (GETFAIL.equals(vnfName)) {
160             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not"
161                     + " found for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
162                     + "generic-vnf\",\"Node Not Found:No Node of type generic-vnf found at network/generic-vnfs"
163                     + "/generic-vnf\",\"ERR.5.4.6114\"]}}}";
164         }
165         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfName);
166         if (ERROR.equals(vnfName)) {
167             return "{ \"vnf-id\": \"error\", \"vnf-name\": \"" + vnfName
168                     + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
169                     + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \""
170                     + "ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
171                     + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
172                     + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
173                     + "\"is-closed-loop-disabled\": " + isDisabled
174                     + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
175                     + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer/"
176                     + "1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
177                     + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
178                     + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
179                     + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
180                     + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
181                     + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01"
182                     + "\"} ], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name"
183                     + "\"}]},{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/"
184                     + "cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/"
185                     + "USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver/3b2558f4-39d8-40e7-bfc7-30660fb52c45"
186                     + "\", \"relationship-data\":[{ \"relationship-key\": \"cloud-region.cloud-owner\", \""
187                     + "relationship-value\": \"att-aic\"},{ \"relationship-key\": \"cloud-region.cloud-region-id"
188                     + "\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \"tenant.tenant-id"
189                     + "\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \"relationship-key\": \""
190                     + "vserver.vserver-id\", \"relationship-value\": \"3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \""
191                     + "related-to-property\": [ {\"property-key\": \"vserver.vserver-name\",\"property-value\": \""
192                     + "USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
193
194         }
195         final String vnfId = getUuidValue(vnfName, "5e49ca06-2972-4532-9ed4-6d071588d792");
196         return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
197                 + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
198                 + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \"ATT"
199                 + "\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
200                 + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
201                 + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
202                 + "\"is-closed-loop-disabled\": " + isDisabled
203                 + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
204                 + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer"
205                 + "/1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
206                 + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
207                 + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
208                 + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
209                 + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
210                 + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} ], \""
211                 + "related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},{ \""
212                 + "related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
213                 + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
214                 + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
215                 + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
216                 + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
217                 + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
218                 + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
219                 + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
220                 + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
221     }
222
223     /**
224      * Get by VNF Id.
225      *
226      * @param vnfId the VNF Id
227      * @return the response
228      */
229     @GET
230     @Path("/v11/network/generic-vnfs/generic-vnf/{vnfId}")
231     @Consumes(MediaType.APPLICATION_JSON)
232     @Produces("application/json")
233     public String getByVnfId(@PathParam("vnfId") final String vnfId) {
234         if (GETFAIL.equals(vnfId)) {
235             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
236                     + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
237                     + "generic-vnf/getFail\",\"Node Not Found:No Node of type generic-vnf found at network/"
238                     + "generic-vnfs/generic-vnf/getFail\",\"ERR.5.4.6114\"]}}}";
239         }
240         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfId);
241         final String vnfName = getUuidValue(vnfId, "USUCP0PCOIL0110UJRT01");
242         return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
243                 + "\", \"vnf-type\": \"RT\", \"service-id\": \""
244                 + "d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \"equipment-role\": \"UCPE\", \"orchestration-status"
245                 + "\": \"created\", \"management-option\": \"ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \""
246                 + "ipv4-loopback0-address\": \"32.40.64.57\", \"nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345"
247                 + "\", \"management-v6-address\": \"2001:1890:e00e:fffd::36\", \"in-maint\": false, "
248                 + "\"prov-status\":\"ACTIVE\", \"" + "" + "is-closed-loop-disabled\": " + isDisabled
249                 + ", \"resource-version\": \"1493389458092\", \""
250                 + "relationship-list\": {\"relationship\":[{ \"related-to\": \"service-instance\", \"related-link"
251                 + "\": \"/aai/v11/business/customers/customer/1610_Func_Global_20160817084727/service-subscriptions"
252                 + "/service-subscription/uCPE-VMS/service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \""
253                 + "relationship-data\":[{ \"relationship-key\": \"customer.global-customer-id\", \""
254                 + "relationship-value\": \"1610_Func_Global_20160817084727\"},{ \"relationship-key\": \""
255                 + "service-subscription.service-type\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key"
256                 + "\": \"service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} "
257                 + "], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},"
258                 + "{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
259                 + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
260                 + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
261                 + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
262                 + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
263                 + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
264                 + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
265                 + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
266                 + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
267     }
268
269     /**
270      * Get by VServer name.
271      *
272      * @param vserverName the VServer name
273      * @return the response
274      */
275     @GET
276     @Path("/v11/nodes/vservers")
277     @Consumes(MediaType.APPLICATION_JSON)
278     @Produces("application/json")
279     public String getByVserverName(@QueryParam("vserver-name") final String vserverName) {
280         if (GETFAIL.equals(vserverName)) {
281             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
282                     + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"nodes/vservers\",\"Node Not"
283                     + " Found:No Node of type generic-vnf found at nodes/vservers\",\"ERR.5.4.6114\"]}}}";
284         }
285         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vserverName);
286         final String vserverId = getUuidValue(vserverName, "d0668d4f-c25e-4a1b-87c4-83845c01efd8");
287         return "{\"vserver\": [{ \"vserver-id\": \"" + vserverId + "\", \"vserver-name\": \"" + vserverName
288                 + "\", \"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\": "
289                 + isDisabled + ", \"prov-status\":\"ACTIVE\", \"resource-version\": \"1494001931513\", "
290                 + "\"relationship-list\": {\"relationship\":[{ \"related-to"
291                 + "\": \"generic-vnf\", \"related-link\": \"/aai/v11/network/generic-vnfs/generic-vnf/"
292                 + "e1a41e99-4ede-409a-8f9d-b5e12984203a\", \"relationship-data\": [ {\"relationship-key\": \""
293                 + "generic-vnf.vnf-id\",\"relationship-value\": \"e1a41e99-4ede-409a-8f9d-b5e12984203a\" }], \""
294                 + "related-to-property\": [ {\"property-key\": \"generic-vnf.vnf-name\",\"property-value\": \""
295                 + "USMSO1SX7NJ0103UJSW01\" }]},{ \"related-to\": \"pserver\", \"related-link\": \"/aai/v11/"
296                 + "cloud-infrastructure/pservers/pserver/USMSO1SX7NJ0103UJZZ01\", \"relationship-data\": [ {\""
297                 + "relationship-key\": \"pserver.hostname\",\"relationship-value\": \"USMSO1SX7NJ0103UJZZ01\" }], \""
298                 + "related-to-property\": [{\"property-key\": \"pserver.pserver-name2\"}]} ]}}]}";
299     }
300
301     private String getUuidValue(final String value, final String defaultValue) {
302         return value != null ? UUID.nameUUIDFromBytes(value.getBytes()).toString() : defaultValue;
303     }
304
305     /**
306      * Loads a JSON response from a file and then replaces parameters of the form, ${xxx}, with values.
307      *
308      * @param fileName name of the file containing the JSON
309      * @param params parameters to be substituted
310      * @return the JSON response, after parameter substitution
311      * @throws IOException if the file cannot be read
312      */
313     private String load(String fileName, Map<String, String> params) throws IOException {
314         String json = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
315
316         // perform parameter substitution
317         for (Entry<String, String> ent : params.entrySet()) {
318             String name = "${" + ent.getKey() + "}";
319             String value = ent.getValue();
320             json = json.replace(name, value);
321         }
322
323         return json;
324     }
325 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use