Updating image version and fixing tests
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / BusinessController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.controller;
21
22 import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_TYPE;
24 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_URL;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
26 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
27 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION;
28 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
29 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
30 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
31 import java.util.List;
32 import java.util.Optional;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.core.MediaType;
35 import org.onap.aai.domain.yang.Customer;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.aai.domain.yang.GenericVnfs;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.ServiceInstance;
40 import org.onap.aai.domain.yang.ServiceInstances;
41 import org.onap.aai.domain.yang.ServiceSubscription;
42 import org.onap.so.aaisimulator.models.NodeServiceInstance;
43 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
44 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
45 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.web.bind.annotation.DeleteMapping;
53 import org.springframework.web.bind.annotation.GetMapping;
54 import org.springframework.web.bind.annotation.PathVariable;
55 import org.springframework.web.bind.annotation.PostMapping;
56 import org.springframework.web.bind.annotation.PutMapping;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestHeader;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestParam;
61
62 /**
63  * @author waqas.ikram@ericsson.com
64  *
65  */
66 @Controller
67 @RequestMapping(path = CUSTOMER_URL)
68 public class BusinessController {
69
70     private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
71     private final CustomerCacheServiceProvider cacheServiceProvider;
72     private final NodesCacheServiceProvider nodesCacheServiceProvider;
73     private final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
74
75     @Autowired
76     public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
77             final NodesCacheServiceProvider nodesCacheServiceProvider,
78             final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider) {
79         this.cacheServiceProvider = cacheServiceProvider;
80         this.nodesCacheServiceProvider = nodesCacheServiceProvider;
81         this.genericVnfCacheServiceProvider = genericVnfCacheServiceProvider;
82     }
83
84     @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
85     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
86             final HttpServletRequest request) {
87         LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
88
89         final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
90         if (optional.isPresent()) {
91             final Customer customer = optional.get();
92             LOGGER.info("found customer {} in cache", customer);
93             return ResponseEntity.ok(customer);
94         }
95
96         LOGGER.error("Couldn't find {} in cache", globalCustomerId);
97         return getRequestErrorResponseEntity(request, CUSTOMER_TYPE);
98     }
99
100     @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
101             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
102     public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
103             @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
104         LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
105
106         if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
107             customer.setResourceVersion(getResourceVersion());
108
109         }
110         cacheServiceProvider.putCustomer(globalCustomerId, customer);
111         return ResponseEntity.accepted().build();
112
113     }
114
115     @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
116             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
117     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
118             @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
119         LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
120                 globalCustomerId, serviceType);
121
122         final Optional<ServiceSubscription> optional =
123                 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
124         if (optional.isPresent()) {
125             final ServiceSubscription serviceSubscription = optional.get();
126             LOGGER.info("found service subscription  {} in cache", serviceSubscription);
127             return ResponseEntity.ok(serviceSubscription);
128         }
129
130         LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
131                 serviceType);
132         return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
133     }
134
135     @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
136             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
137     public ResponseEntity<?> putServiceSubscription(@PathVariable("global-customer-id") final String globalCustomerId,
138             @PathVariable("service-type") final String serviceType,
139             @RequestBody final ServiceSubscription serviceSubscription, final HttpServletRequest request) {
140         LOGGER.info("Will add service subscription for 'global customer id': {} and 'service type': {} ...",
141                 globalCustomerId, serviceType);
142
143         if (cacheServiceProvider.putServiceSubscription(globalCustomerId, serviceType, serviceSubscription)) {
144             LOGGER.info("Successfully add service subscription in cache ...");
145             return ResponseEntity.accepted().build();
146         }
147
148         LOGGER.error("Couldn't add service subscription using 'global customer id': {} and 'service type': {}",
149                 globalCustomerId, serviceType);
150         return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
151     }
152
153     @GetMapping(
154             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
155             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
156     public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
157             @PathVariable("service-type") final String serviceType,
158             @RequestParam(name = "service-instance-name") final String serviceInstanceName,
159             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
160
161         LOGGER.info(
162                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
163                 globalCustomerId, serviceType, serviceInstanceName, depth);
164
165         final Optional<ServiceInstances> optional =
166                 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
167         if (optional.isPresent()) {
168             final ServiceInstances serviceInstances = optional.get();
169             LOGGER.info("found service instance  {} in cache", serviceInstances);
170             return ResponseEntity.ok(serviceInstances);
171         }
172         LOGGER.error(
173                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
174                 globalCustomerId, serviceType, serviceInstanceName, depth);
175         return getRequestErrorResponseEntity(request);
176     }
177
178     @GetMapping(
179             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
180             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
181     public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
182             @PathVariable("service-type") final String serviceType,
183             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
184             @RequestParam(name = "depth", required = false) final Integer depth,
185             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
186             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
187             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
188
189         LOGGER.info(
190                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
191                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
192
193         final Optional<ServiceInstance> optional =
194                 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
195         if (optional.isPresent()) {
196             final ServiceInstance serviceInstance = optional.get();
197             LOGGER.info("found service instance  {} in cache", serviceInstance);
198             return ResponseEntity.ok(serviceInstance);
199         }
200         LOGGER.error(
201                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
202                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
203         return getRequestErrorResponseEntity(request);
204     }
205
206     @PutMapping(
207             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
208             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
209     public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
210             @PathVariable("service-type") final String serviceType,
211             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
212             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String invocationId,
213             @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
214
215         LOGGER.info(
216                 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
217                 globalCustomerId, serviceType, serviceInstanceId);
218
219         if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
220             serviceInstance.setResourceVersion(getResourceVersion());
221         }
222
223         if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
224                 serviceInstance)) {
225             nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
226                     globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
227             return ResponseEntity.accepted().build();
228         }
229
230         LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
231                 globalCustomerId, serviceType, serviceInstanceId);
232         return getRequestErrorResponseEntity(request);
233     }
234
235     @PostMapping(
236             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
237             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
238     public ResponseEntity<?> patchSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
239             @PathVariable("service-type") final String serviceType,
240             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
241             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
242             @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
243
244         LOGGER.info(
245                 "Will post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...",
246                 globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
247
248         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
249             cacheServiceProvider.patchServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
250                     serviceInstance);
251             return ResponseEntity.accepted().build();
252         }
253         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
254
255         return getRequestErrorResponseEntity(request);
256     }
257
258
259     @GetMapping(
260             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/related-to/generic-vnfs",
261             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
262     public ResponseEntity<?> getRelatedToGenericVnf(@PathVariable("global-customer-id") final String globalCustomerId,
263             @PathVariable("service-type") final String serviceType,
264             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
265             @RequestParam(name = "vnf-name", required = false) final String vnfName, final HttpServletRequest request) {
266
267         LOGGER.info(
268                 "Will retrieve generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
269                 globalCustomerId, serviceType, serviceInstanceId, vnfName);
270
271         final List<String> relatedToVnfIds =
272                 getRelatedToVnfIds(globalCustomerId, serviceType, serviceInstanceId, vnfName);
273
274
275         if (!relatedToVnfIds.isEmpty()) {
276             final GenericVnfs genericVnfs = new GenericVnfs();
277             relatedToVnfIds.stream().forEach(vnfId -> {
278                 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(vnfId);
279                 if (genericVnfOptional.isPresent()) {
280                     final GenericVnf genericVnf = genericVnfOptional.get();
281                     LOGGER.info("found related-to generic-vnf {} in cache", genericVnf);
282                     genericVnfs.getGenericVnf().add(genericVnf);
283                 }
284             });
285             if (!genericVnfs.getGenericVnf().isEmpty()) {
286                 LOGGER.info("Found {} related generic-vnfs", genericVnfs.getGenericVnf().size());
287                 return ResponseEntity.ok(genericVnfs);
288             }
289         }
290
291         LOGGER.error(
292                 "Couldn't find  generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
293                 globalCustomerId, serviceType, serviceInstanceId, vnfName);
294         return getRequestErrorResponseEntity(request, GENERIC_VNF);
295     }
296
297     @PutMapping(
298             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}"
299                     + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
300             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
301             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
302     public ResponseEntity<?> putSericeInstanceRelationShip(
303             @PathVariable("global-customer-id") final String globalCustomerId,
304             @PathVariable("service-type") final String serviceType,
305             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
306             @RequestBody final Relationship relationship, final HttpServletRequest request) {
307
308         LOGGER.info(
309                 "Will add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
310                 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
311         final Optional<Relationship> optional = cacheServiceProvider.addRelationShip(globalCustomerId, serviceType,
312                 serviceInstanceId, relationship, request.getRequestURI());
313
314         if (optional.isPresent()) {
315             final Relationship resultantRelationship = optional.get();
316             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
317             return ResponseEntity.accepted().body(resultantRelationship);
318         }
319
320         LOGGER.error(
321                 "Couldn't add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
322                 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
323
324         return getRequestErrorResponseEntity(request);
325     }
326
327     @DeleteMapping(
328             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
329             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
330     public ResponseEntity<?> deleteSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
331             @PathVariable("service-type") final String serviceType,
332             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
333             @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
334
335         LOGGER.info(
336                 "Will delete SericeInstance for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
337                 globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
338
339         if (cacheServiceProvider.deleteSericeInstance(globalCustomerId, serviceType, serviceInstanceId,
340                 resourceVersion)) {
341             LOGGER.info(
342                     "Successfully deleted SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
343                     globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
344             return ResponseEntity.noContent().build();
345         }
346
347         LOGGER.error(
348                 "Unable to delete SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
349                 globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
350
351         return getRequestErrorResponseEntity(request);
352
353     }
354
355     private List<String> getRelatedToVnfIds(final String globalCustomerId, final String serviceType,
356             final String serviceInstanceId, final String vnfName) {
357         if (vnfName != null) {
358             return cacheServiceProvider.getRelatedToVnfIds(globalCustomerId, serviceType, serviceInstanceId, vnfName);
359         }
360         return cacheServiceProvider.getRelatedToVnfIds(globalCustomerId, serviceType, serviceInstanceId);
361     }
362 }