Merge "[ADMIN] Add Illia Halych in INFO.yaml "
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / main / java / org / onap / aaisimulator / controller / BusinessController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.aaisimulator.controller;
21
22 import static org.onap.aaisimulator.utils.Constants.CUSTOMER_URL;
23 import static org.onap.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
24 import static org.onap.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
25 import static org.onap.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
26
27 import java.util.Optional;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.MediaType;
30 import org.onap.aai.domain.yang.ServiceInstance;
31 import org.onap.aaisimulator.service.providers.ServiceInstanceCacheProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PostMapping;
41 import org.springframework.web.bind.annotation.PutMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestHeader;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46
47 @Controller
48 @RequestMapping(path = CUSTOMER_URL)
49 public class BusinessController {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
52
53     private final ServiceInstanceCacheProvider svcInstanceCacheSvcProvider;
54
55     @Autowired
56     public BusinessController(final ServiceInstanceCacheProvider serviceInstanceCacheProvider) {
57         this.svcInstanceCacheSvcProvider = serviceInstanceCacheProvider;
58     }
59
60     @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
61         produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
62     public ResponseEntity<?> putServiceInstance(@PathVariable("global-customer-id") final String globalCustomerId,
63         @PathVariable("service-type") final String serviceType,
64         @PathVariable(name = "service-instance-id") final String serviceInstanceId,
65         @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
66
67         LOGGER.info("Add service instance to cache for 'global customer id': {}, 'service type': {} and "
68             + "'service instance id: '{}..", globalCustomerId, serviceType, serviceInstanceId);
69
70         if (svcInstanceCacheSvcProvider.putServiceInstance(globalCustomerId, serviceInstance)) {
71             LOGGER.info("Successfully added service instance to cache ...");
72             return ResponseEntity.accepted().build();
73         }
74
75         LOGGER.error(
76             "Couldn't add service instance for 'global customer id': {},'service type': {} and 'service instance id: '{} ...",
77             globalCustomerId, serviceType, serviceInstanceId);
78         return getRequestErrorResponseEntity(request, SERVICE_RESOURCE_TYPE);
79     }
80
81     @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
82         produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
83     public ResponseEntity<?> getServiceInstance(@PathVariable("global-customer-id") final String globalCustomerId,
84         @PathVariable("service-type") final String serviceType,
85         @PathVariable(name = "service-instance-id") final String serviceInstanceId,
86         @RequestParam(name = "depth", required = false) final Integer depth,
87         @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
88         @RequestParam(name = "resultSize", required = false) final Integer resultSize,
89         @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
90
91         LOGGER.info(
92             "Retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
93             globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
94
95         final Optional<ServiceInstance> svcInstance =
96             svcInstanceCacheSvcProvider.getServiceInstance(globalCustomerId);
97
98         if (svcInstance.isPresent()) {
99             final ServiceInstance serviceInstance = svcInstance.get();
100             LOGGER.info("Found service instance  {} in cache", serviceInstance);
101             return ResponseEntity.ok(serviceInstance);
102         }
103
104         LOGGER.error(
105             "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
106             globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
107         return getRequestErrorResponseEntity(request);
108     }
109
110     @PostMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
111         produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
112     public ResponseEntity<?> patchServiceInstance(@PathVariable("global-customer-id") final String globalCustomerId,
113         @PathVariable("service-type") final String serviceType,
114         @PathVariable(name = "service-instance-id") final String serviceInstanceId,
115         @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
116         @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
117
118         LOGGER.info(
119             "Post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...",
120             globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
121
122         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
123             svcInstanceCacheSvcProvider.patchServiceInstance(globalCustomerId, serviceInstance);
124             return ResponseEntity.accepted().build();
125         }
126         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
127
128         return getRequestErrorResponseEntity(request);
129     }
130 }