Merge "Update monitoring policies to match DCAE"
[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, 2020 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.UUID;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.QueryParam;
34 import javax.ws.rs.core.MediaType;
35 import org.apache.commons.io.IOUtils;
36
37 @Path("/aai")
38 public class AaiSimulatorJaxRs {
39
40     private static final String DISABLE_CLOSEDLOOP = "disableClosedLoop";
41     private static final String ERROR = "error";
42     private static final String GETFAIL = "getFail";
43
44     /**
45      * A&AI get query.
46      *
47      * @param vnfId the VNF Id
48      * @return the result
49      */
50     @GET
51     @Path("/v8/network/generic-vnfs/generic-vnf/{vnfId}")
52     @Consumes(MediaType.APPLICATION_JSON)
53     @Produces("application/json")
54     public String aaiGetQuery(@PathParam("vnfId") final String vnfId) {
55         return "{\"relationship-list\": {\"relationship\":[{\"related-to-property\": [{\"property-key\": "
56             + "\"service-instance.service-instance-name\"}]},{\"related-to-property\": [ {\"property-key\": "
57             + "\"vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
58     }
59
60     /**
61      * A&AI get query.
62      *
63      * @return the result
64      */
65     @GET
66     @Path("/v16/search/nodes-query")
67     @Consumes(MediaType.APPLICATION_JSON)
68     @Produces("application/json")
69     public String aaiGetVserverQuery(@QueryParam("filter") final String filter) {
70         if (filter.equals("vserver-name:EQUALS:f953c499-4b1e-426b-8c6d-e9e9f1fc730f")
71             || filter.equals("vserver-name:EQUALS:OzVServer")
72             || filter.equals("vserver-name:EQUALS:testVserverName")) {
73             return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
74                 + "cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants"
75                 + "/tenant/3f2aaef74ecb4b19b35e26d0849fe9a2/vservers/vserver/"
76                 + "6c3b3714-e36c-45af-9f16-7d3a73d99497\"}]}";
77         } else {
78             return null;
79         }
80     }
81
82     /**
83      * A&AI put query.
84      *
85      * @param req the request
86      * @return the response
87      * @throws IOException if a response file cannot be read
88      */
89     @PUT
90     @Path("/v16/query")
91     @Consumes(MediaType.APPLICATION_JSON)
92     @Produces("application/json")
93     public String aaiPutQuery(final String req) throws IOException {
94         return IOUtils.toString(getClass().getResource("aai/AaiCqResponse.json"),
95             StandardCharsets.UTF_8);
96     }
97
98     /**
99      * A&AI get PNF query.
100      *
101      * @return the result
102      * @throws IOException if a response file cannot be read
103      */
104     @GET
105     @Path("/v16/network/pnfs/pnf/{pnfName}")
106     @Consumes(MediaType.APPLICATION_JSON)
107     @Produces("application/json")
108     public String aaiGetPnfQuery(@PathParam("pnfName") final String pnfName) throws IOException {
109         if (GETFAIL.equals(pnfName)) {
110             throw new IllegalArgumentException("query failed, as requested");
111         }
112
113         return IOUtils.toString(getClass().getResource("aai/AaiGetPnfResponse.json"),
114                         StandardCharsets.UTF_8);
115     }
116
117     /**
118      * Get by VNF name.
119      *
120      * @param vnfName the VNF name
121      * @return the response
122      */
123     @GET
124     @Path("/v11/network/generic-vnfs/generic-vnf")
125     @Consumes(MediaType.APPLICATION_JSON)
126     @Produces("application/json")
127     public String getByVnfName(@QueryParam("vnf-name") final String vnfName) {
128         if (GETFAIL.equals(vnfName)) {
129             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not"
130                 + " found for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
131                 + "generic-vnf\",\"Node Not Found:No Node of type generic-vnf found at network/generic-vnfs"
132                 + "/generic-vnf\",\"ERR.5.4.6114\"]}}}";
133         }
134         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfName);
135         if (ERROR.equals(vnfName)) {
136             return "{ \"vnf-id\": \"error\", \"vnf-name\": \"" + vnfName
137                 + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
138                 + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \""
139                 + "ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
140                 + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
141                 + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
142                 + "\"is-closed-loop-disabled\": " + isDisabled
143                 + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
144                 + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer/"
145                 + "1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
146                 + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
147                 + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
148                 + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
149                 + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
150                 + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01"
151                 + "\"} ], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name"
152                 + "\"}]},{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/"
153                 + "cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/"
154                 + "USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver/3b2558f4-39d8-40e7-bfc7-30660fb52c45"
155                 + "\", \"relationship-data\":[{ \"relationship-key\": \"cloud-region.cloud-owner\", \""
156                 + "relationship-value\": \"att-aic\"},{ \"relationship-key\": \"cloud-region.cloud-region-id"
157                 + "\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \"tenant.tenant-id"
158                 + "\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \"relationship-key\": \""
159                 + "vserver.vserver-id\", \"relationship-value\": \"3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \""
160                 + "related-to-property\": [ {\"property-key\": \"vserver.vserver-name\",\"property-value\": \""
161                 + "USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
162
163         }
164         final String vnfId = getUuidValue(vnfName, "5e49ca06-2972-4532-9ed4-6d071588d792");
165         return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
166             + "\", \"vnf-type\": \"RT\", \"service-id\": \"d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \""
167             + "equipment-role\": \"UCPE\", \"orchestration-status\": \"created\", \"management-option\": \"ATT"
168             + "\", \"ipv4-oam-address\": \"32.40.68.35\", \"ipv4-loopback0-address\": \"32.40.64.57\", \""
169             + "nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345\", \"management-v6-address\": \""
170             + "2001:1890:e00e:fffd::36\", \"in-maint\": false, \"prov-status\":\"ACTIVE\", "
171             + "\"is-closed-loop-disabled\": " + isDisabled
172             + ", \"resource-version\": \"1493389458092\", \"relationship-list\": {\"relationship\":[{ \""
173             + "related-to\": \"service-instance\", \"related-link\": \"/aai/v11/business/customers/customer"
174             + "/1610_Func_Global_20160817084727/service-subscriptions/service-subscription/uCPE-VMS/"
175             + "service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \"relationship-data\":[{ \""
176             + "relationship-key\": \"customer.global-customer-id\", \"relationship-value\": \""
177             + "1610_Func_Global_20160817084727\"},{ \"relationship-key\": \"service-subscription.service-type"
178             + "\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key\": \""
179             + "service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} ], \""
180             + "related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},{ \""
181             + "related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
182             + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
183             + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
184             + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
185             + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
186             + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
187             + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
188             + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
189             + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
190     }
191
192     /**
193      * Get by VNF Id.
194      *
195      * @param vnfId the VNF Id
196      * @return the response
197      */
198     @GET
199     @Path("/v11/network/generic-vnfs/generic-vnf/{vnfId}")
200     @Consumes(MediaType.APPLICATION_JSON)
201     @Produces("application/json")
202     public String getByVnfId(@PathParam("vnfId") final String vnfId) {
203         if (GETFAIL.equals(vnfId)) {
204             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
205                 + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"network/generic-vnfs/"
206                 + "generic-vnf/getFail\",\"Node Not Found:No Node of type generic-vnf found at network/"
207                 + "generic-vnfs/generic-vnf/getFail\",\"ERR.5.4.6114\"]}}}";
208         }
209         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vnfId);
210         final String vnfName = getUuidValue(vnfId, "USUCP0PCOIL0110UJRT01");
211         return "{ \"vnf-id\": \"" + vnfId + "\", \"vnf-name\": \"" + vnfName
212             + "\", \"vnf-type\": \"RT\", \"service-id\": \""
213             + "d7bb0a21-66f2-4e6d-87d9-9ef3ced63ae4\", \"equipment-role\": \"UCPE\", \"orchestration-status"
214             + "\": \"created\", \"management-option\": \"ATT\", \"ipv4-oam-address\": \"32.40.68.35\", \""
215             + "ipv4-loopback0-address\": \"32.40.64.57\", \"nm-lan-v6-address\": \"2001:1890:e00e:fffe::1345"
216             + "\", \"management-v6-address\": \"2001:1890:e00e:fffd::36\", \"in-maint\": false, "
217             + "\"prov-status\":\"ACTIVE\", \"" + "" + "is-closed-loop-disabled\": " + isDisabled
218             + ", \"resource-version\": \"1493389458092\", \""
219             + "relationship-list\": {\"relationship\":[{ \"related-to\": \"service-instance\", \"related-link"
220             + "\": \"/aai/v11/business/customers/customer/1610_Func_Global_20160817084727/service-subscriptions"
221             + "/service-subscription/uCPE-VMS/service-instances/service-instance/USUCP0PCOIL0110UJZZ01\", \""
222             + "relationship-data\":[{ \"relationship-key\": \"customer.global-customer-id\", \""
223             + "relationship-value\": \"1610_Func_Global_20160817084727\"},{ \"relationship-key\": \""
224             + "service-subscription.service-type\", \"relationship-value\": \"uCPE-VMS\"},{ \"relationship-key"
225             + "\": \"service-instance.service-instance-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01\"} "
226             + "], \"related-to-property\": [{\"property-key\": \"service-instance.service-instance-name\"}]},"
227             + "{ \"related-to\": \"vserver\", \"related-link\": \"/aai/v11/cloud-infrastructure/cloud-regions/"
228             + "cloud-region/att-aic/AAIAIC25/tenants/tenant/USUCP0PCOIL0110UJZZ01%3A%3AuCPE-VMS/vservers/vserver"
229             + "/3b2558f4-39d8-40e7-bfc7-30660fb52c45\", \"relationship-data\":[{ \"relationship-key\": \""
230             + "cloud-region.cloud-owner\", \"relationship-value\": \"att-aic\"},{ \"relationship-key\": \""
231             + "cloud-region.cloud-region-id\", \"relationship-value\": \"AAIAIC25\"},{ \"relationship-key\": \""
232             + "tenant.tenant-id\", \"relationship-value\": \"USUCP0PCOIL0110UJZZ01::uCPE-VMS\"},{ \""
233             + "relationship-key\": \"vserver.vserver-id\", \"relationship-value\": \""
234             + "3b2558f4-39d8-40e7-bfc7-30660fb52c45\"} ], \"related-to-property\": [ {\"property-key\": \""
235             + "vserver.vserver-name\",\"property-value\": \"USUCP0PCOIL0110UJZZ01-vsrx\" }]} ]}}";
236     }
237
238     /**
239      * Get by VServer name.
240      *
241      * @param vserverName the VServer name
242      * @return the response
243      */
244     @GET
245     @Path("/v11/nodes/vservers")
246     @Consumes(MediaType.APPLICATION_JSON)
247     @Produces("application/json")
248     public String getByVserverName(@QueryParam("vserver-name") final String vserverName) {
249         if (GETFAIL.equals(vserverName)) {
250             return "{\"requestError\":{\"serviceException\":{\"messageId\":\"SVC3001\",\"text\":\"Resource not found"
251                 + " for %1 using id %2 (msg=%3) (ec=%4)\",\"variables\":[\"GET\",\"nodes/vservers\",\"Node Not"
252                 + " Found:No Node of type generic-vnf found at nodes/vservers\",\"ERR.5.4.6114\"]}}}";
253         }
254         final boolean isDisabled = DISABLE_CLOSEDLOOP.equals(vserverName);
255         final String vserverId = getUuidValue(vserverName, "d0668d4f-c25e-4a1b-87c4-83845c01efd8");
256         return "{\"vserver\": [{ \"vserver-id\": \"" + vserverId + "\", \"vserver-name\": \""
257             + vserverName
258             + "\", \"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\": "
259             + isDisabled + ", \"prov-status\":\"ACTIVE\", \"resource-version\": \"1494001931513\", "
260             + "\"relationship-list\": {\"relationship\":[{ \"related-to"
261             + "\": \"generic-vnf\", \"related-link\": \"/aai/v11/network/generic-vnfs/generic-vnf/"
262             + "e1a41e99-4ede-409a-8f9d-b5e12984203a\", \"relationship-data\": [ {\"relationship-key\": \""
263             + "generic-vnf.vnf-id\",\"relationship-value\": \"e1a41e99-4ede-409a-8f9d-b5e12984203a\" }], \""
264             + "related-to-property\": [ {\"property-key\": \"generic-vnf.vnf-name\",\"property-value\": \""
265             + "USMSO1SX7NJ0103UJSW01\" }]},{ \"related-to\": \"pserver\", \"related-link\": \"/aai/v11/"
266             + "cloud-infrastructure/pservers/pserver/USMSO1SX7NJ0103UJZZ01\", \"relationship-data\": [ {\""
267             + "relationship-key\": \"pserver.hostname\",\"relationship-value\": \"USMSO1SX7NJ0103UJZZ01\" }], \""
268             + "related-to-property\": [{\"property-key\": \"pserver.pserver-name2\"}]} ]}}]}";
269     }
270
271     private String getUuidValue(final String value, final String defaultValue) {
272         return value != null ? UUID.nameUUIDFromBytes(value.getBytes()).toString() : defaultValue;
273     }
274
275 }