347743bd213dc716f274e70a4acbd06d67e2ae8e
[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.GENERIC_VNF_VNF_ID;
27 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
28 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION;
29 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
30 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
31 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
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.RelationshipData;
40 import org.onap.aai.domain.yang.ServiceInstance;
41 import org.onap.aai.domain.yang.ServiceInstances;
42 import org.onap.aai.domain.yang.ServiceSubscription;
43 import org.onap.so.aaisimulator.models.NodeServiceInstance;
44 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
45 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
46 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.stereotype.Controller;
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 = true) 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 Optional<Relationship> optional =
272                 cacheServiceProvider.getRelationship(globalCustomerId, serviceType, serviceInstanceId, vnfName);
273
274         if (optional.isPresent()) {
275
276             final Relationship relationship = optional.get();
277             final Optional<RelationshipData> relationshipDataOptional = relationship.getRelationshipData().stream()
278                     .filter(existing -> GENERIC_VNF_VNF_ID.equals(existing.getRelationshipKey())).findFirst();
279
280             if (relationshipDataOptional.isPresent()) {
281                 final RelationshipData relationshipData = relationshipDataOptional.get();
282                 final String vnfId = relationshipData.getRelationshipValue();
283                 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(vnfId);
284                 if (genericVnfOptional.isPresent()) {
285                     final GenericVnfs genericVnfs = new GenericVnfs();
286                     genericVnfs.getGenericVnf().add(genericVnfOptional.get());
287                     LOGGER.info("found service instance  {} in cache", relationship);
288                     return ResponseEntity.ok(genericVnfs);
289                 }
290             }
291         }
292         LOGGER.error(
293                 "Couldn't find  generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
294                 globalCustomerId, serviceType, serviceInstanceId, vnfName);
295         return getRequestErrorResponseEntity(request, GENERIC_VNF);
296     }
297
298     @PutMapping(
299             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}"
300                     + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
301             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
302             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
303     public ResponseEntity<?> putSericeInstanceRelationShip(
304             @PathVariable("global-customer-id") final String globalCustomerId,
305             @PathVariable("service-type") final String serviceType,
306             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
307             @RequestBody final Relationship relationship, final HttpServletRequest request) {
308
309         LOGGER.info(
310                 "Will add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
311                 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
312         final Optional<Relationship> optional = cacheServiceProvider.addRelationShip(globalCustomerId, serviceType,
313                 serviceInstanceId, relationship, request.getRequestURI());
314
315         if (optional.isPresent()) {
316             final Relationship resultantRelationship = optional.get();
317             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
318             return ResponseEntity.accepted().body(resultantRelationship);
319         }
320
321         LOGGER.error(
322                 "Couldn't add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
323                 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
324
325         return getRequestErrorResponseEntity(request);
326     }
327 }