Adding aai platform endpoints
[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.COMPOSED_OF;
23 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_GLOBAL_CUSTOMER_ID;
24 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_TYPE;
25 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_URL;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
27 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
28 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID;
29 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME;
30 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
31 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION;
32 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE;
33 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
34 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
35 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
36 import java.util.List;
37 import java.util.Optional;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.ws.rs.core.MediaType;
40 import org.onap.aai.domain.yang.Customer;
41 import org.onap.aai.domain.yang.GenericVnf;
42 import org.onap.aai.domain.yang.GenericVnfs;
43 import org.onap.aai.domain.yang.RelatedToProperty;
44 import org.onap.aai.domain.yang.Relationship;
45 import org.onap.aai.domain.yang.RelationshipData;
46 import org.onap.aai.domain.yang.ServiceInstance;
47 import org.onap.aai.domain.yang.ServiceInstances;
48 import org.onap.aai.domain.yang.ServiceSubscription;
49 import org.onap.so.aaisimulator.models.NodeServiceInstance;
50 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
51 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
52 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
53 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.http.HttpMethod;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.stereotype.Controller;
60 import org.springframework.web.bind.annotation.GetMapping;
61 import org.springframework.web.bind.annotation.PathVariable;
62 import org.springframework.web.bind.annotation.PostMapping;
63 import org.springframework.web.bind.annotation.PutMapping;
64 import org.springframework.web.bind.annotation.RequestBody;
65 import org.springframework.web.bind.annotation.RequestHeader;
66 import org.springframework.web.bind.annotation.RequestMapping;
67 import org.springframework.web.bind.annotation.RequestParam;
68
69 /**
70  * @author waqas.ikram@ericsson.com
71  *
72  */
73 @Controller
74 @RequestMapping(path = CUSTOMER_URL)
75 public class BusinessController {
76
77     private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
78     private final CustomerCacheServiceProvider cacheServiceProvider;
79     private final NodesCacheServiceProvider nodesCacheServiceProvider;
80     private final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
81
82     @Autowired
83     public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
84             final NodesCacheServiceProvider nodesCacheServiceProvider,
85             final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider) {
86         this.cacheServiceProvider = cacheServiceProvider;
87         this.nodesCacheServiceProvider = nodesCacheServiceProvider;
88         this.genericVnfCacheServiceProvider = genericVnfCacheServiceProvider;
89     }
90
91     @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
92     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
93             final HttpServletRequest request) {
94         LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
95
96         final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
97         if (optional.isPresent()) {
98             final Customer customer = optional.get();
99             LOGGER.info("found customer {} in cache", customer);
100             return ResponseEntity.ok(customer);
101         }
102
103         LOGGER.error("Couldn't find {} in cache", globalCustomerId);
104         return getRequestErrorResponseEntity(request, CUSTOMER_TYPE);
105     }
106
107     @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
108             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
109     public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
110             @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
111         LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
112
113         if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
114             customer.setResourceVersion(getResourceVersion());
115
116         }
117         cacheServiceProvider.putCustomer(globalCustomerId, customer);
118         return ResponseEntity.accepted().build();
119
120     }
121
122     @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
123             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
124     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
125             @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
126         LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
127                 globalCustomerId, serviceType);
128
129         final Optional<ServiceSubscription> optional =
130                 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
131         if (optional.isPresent()) {
132             final ServiceSubscription serviceSubscription = optional.get();
133             LOGGER.info("found service subscription  {} in cache", serviceSubscription);
134             return ResponseEntity.ok(serviceSubscription);
135         }
136
137         LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
138                 serviceType);
139         return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
140     }
141
142     @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
143             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
144     public ResponseEntity<?> putServiceSubscription(@PathVariable("global-customer-id") final String globalCustomerId,
145             @PathVariable("service-type") final String serviceType,
146             @RequestBody final ServiceSubscription serviceSubscription, final HttpServletRequest request) {
147         LOGGER.info("Will add service subscription for 'global customer id': {} and 'service type': {} ...",
148                 globalCustomerId, serviceType);
149
150         if (cacheServiceProvider.putServiceSubscription(globalCustomerId, serviceType, serviceSubscription)) {
151             LOGGER.info("Successfully add service subscription in cache ...");
152             return ResponseEntity.accepted().build();
153         }
154
155         LOGGER.error("Couldn't add service subscription using 'global customer id': {} and 'service type': {}",
156                 globalCustomerId, serviceType);
157         return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
158     }
159
160     @GetMapping(
161             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
162             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
163     public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
164             @PathVariable("service-type") final String serviceType,
165             @RequestParam(name = "service-instance-name") final String serviceInstanceName,
166             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
167
168         LOGGER.info(
169                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
170                 globalCustomerId, serviceType, serviceInstanceName, depth);
171
172         final Optional<ServiceInstances> optional =
173                 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
174         if (optional.isPresent()) {
175             final ServiceInstances serviceInstances = optional.get();
176             LOGGER.info("found service instance  {} in cache", serviceInstances);
177             return ResponseEntity.ok(serviceInstances);
178         }
179         LOGGER.error(
180                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
181                 globalCustomerId, serviceType, serviceInstanceName, depth);
182         return getRequestErrorResponseEntity(request);
183     }
184
185     @GetMapping(
186             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
187             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
188     public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
189             @PathVariable("service-type") final String serviceType,
190             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
191             @RequestParam(name = "depth", required = false) final Integer depth,
192             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
193             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
194             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
195
196         LOGGER.info(
197                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
198                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
199
200         final Optional<ServiceInstance> optional =
201                 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
202         if (optional.isPresent()) {
203             final ServiceInstance serviceInstance = optional.get();
204             LOGGER.info("found service instance  {} in cache", serviceInstance);
205             return ResponseEntity.ok(serviceInstance);
206         }
207         LOGGER.error(
208                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
209                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
210         return getRequestErrorResponseEntity(request);
211     }
212
213     @PutMapping(
214             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
215             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
216     public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
217             @PathVariable("service-type") final String serviceType,
218             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
219             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String invocationId,
220             @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
221
222         LOGGER.info(
223                 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
224                 globalCustomerId, serviceType, serviceInstanceId);
225
226         if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
227             serviceInstance.setResourceVersion(getResourceVersion());
228         }
229
230         if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
231                 serviceInstance)) {
232             nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
233                     globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
234             return ResponseEntity.accepted().build();
235         }
236
237         LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
238                 globalCustomerId, serviceType, serviceInstanceId);
239         return getRequestErrorResponseEntity(request);
240     }
241
242     @PostMapping(
243             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
244             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
245     public ResponseEntity<?> patchSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
246             @PathVariable("service-type") final String serviceType,
247             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
248             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
249             @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
250
251         LOGGER.info(
252                 "Will post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...",
253                 globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
254
255         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
256             cacheServiceProvider.patchServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
257                     serviceInstance);
258             return ResponseEntity.accepted().build();
259         }
260         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
261
262         return getRequestErrorResponseEntity(request);
263     }
264
265
266     @GetMapping(
267             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/related-to/generic-vnfs",
268             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
269     public ResponseEntity<?> getRelatedToGenericVnf(@PathVariable("global-customer-id") final String globalCustomerId,
270             @PathVariable("service-type") final String serviceType,
271             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
272             @RequestParam(name = "vnf-name", required = true) final String vnfName, final HttpServletRequest request) {
273
274         LOGGER.info(
275                 "Will retrieve generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
276                 globalCustomerId, serviceType, serviceInstanceId, vnfName);
277
278         final Optional<Relationship> optional =
279                 cacheServiceProvider.getRelationship(globalCustomerId, serviceType, serviceInstanceId, vnfName);
280
281         if (optional.isPresent()) {
282
283             final Relationship relationship = optional.get();
284             final Optional<RelationshipData> relationshipDataOptional = relationship.getRelationshipData().stream()
285                     .filter(existing -> GENERIC_VNF_VNF_ID.equals(existing.getRelationshipKey())).findFirst();
286
287             if (relationshipDataOptional.isPresent()) {
288                 final RelationshipData relationshipData = relationshipDataOptional.get();
289                 final String vnfId = relationshipData.getRelationshipValue();
290                 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(vnfId);
291                 if (genericVnfOptional.isPresent()) {
292                     final GenericVnfs genericVnfs = new GenericVnfs();
293                     genericVnfs.getGenericVnf().add(genericVnfOptional.get());
294                     LOGGER.info("found service instance  {} in cache", relationship);
295                     return ResponseEntity.ok(genericVnfs);
296                 }
297             }
298         }
299         LOGGER.error(
300                 "Couldn't find  generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
301                 globalCustomerId, serviceType, serviceInstanceId, vnfName);
302         return getRequestErrorResponseEntity(request, GENERIC_VNF);
303     }
304
305
306     @PutMapping(
307             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/relationship-list/relationship",
308             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
309             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
310     public ResponseEntity<?> putSericeInstanceRelationShip(
311             @PathVariable("global-customer-id") final String globalCustomerId,
312             @PathVariable("service-type") final String serviceType,
313             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
314             @RequestBody final Relationship relationship, final HttpServletRequest request) {
315
316         LOGGER.info(
317                 "Will add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
318                 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
319         final Optional<ServiceInstance> optional =
320                 cacheServiceProvider.addRelationShip(globalCustomerId, serviceType, serviceInstanceId, relationship);
321
322         if (optional.isPresent()) {
323             final ServiceInstance serviceInstance = optional.get();
324             final Relationship resultantRelationship = new Relationship();
325             resultantRelationship.setRelatedTo(GENERIC_VNF);
326             resultantRelationship.setRelationshipLabel(COMPOSED_OF);
327             resultantRelationship.setRelatedLink(request.getRequestURI());
328
329             final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
330             relationshipDataList.add(getRelationshipData(CUSTOMER_GLOBAL_CUSTOMER_ID, globalCustomerId));
331             relationshipDataList.add(getRelationshipData(SERVICE_SUBSCRIPTION_SERVICE_TYPE, serviceType));
332             relationshipDataList.add(getRelationshipData(SERVICE_INSTANCE_SERVICE_INSTANCE_ID, serviceInstanceId));
333
334             final List<RelatedToProperty> relatedToProperty = resultantRelationship.getRelatedToProperty();
335             relatedToProperty.add(getRelatedToProperty(SERVICE_INSTANCE_SERVICE_INSTANCE_NAME,
336                     serviceInstance.getServiceInstanceName()));
337
338             return ResponseEntity.accepted().body(resultantRelationship);
339         }
340
341         LOGGER.error(
342                 "Couldn't add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
343                 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
344
345         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request);
346     }
347
348     private RelatedToProperty getRelatedToProperty(final String key, final String value) {
349         final RelatedToProperty relatedToProperty = new RelatedToProperty();
350         relatedToProperty.setPropertyKey(key);
351         relatedToProperty.setPropertyValue(value);
352         return relatedToProperty;
353     }
354
355     private RelationshipData getRelationshipData(final String key, final String value) {
356         final RelationshipData relationshipData = new RelationshipData();
357         relationshipData.setRelationshipKey(key);
358         relationshipData.setRelationshipValue(value);
359         return relationshipData;
360     }
361
362
363 }