b5682f02614bf389c667d73f5b11028e06996787
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aai / simulator / 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.aai.simulator.controller;
21
22 import static org.onap.so.aai.simulator.utils.Constants.CUSTOMER_URL;
23 import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE;
24 import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE_ID;
25 import static org.onap.so.aai.simulator.utils.Utils.getResourceVersion;
26 import java.util.Optional;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.MediaType;
29 import org.onap.aai.domain.yang.Customer;
30 import org.onap.aai.domain.yang.ServiceInstance;
31 import org.onap.aai.domain.yang.ServiceInstances;
32 import org.onap.aai.domain.yang.ServiceSubscription;
33 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
34 import org.onap.so.aai.simulator.utils.RequestErrorBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
47
48 /**
49  * @author waqas.ikram@ericsson.com
50  *
51  */
52 @Controller
53 @RequestMapping(path = CUSTOMER_URL)
54 public class BusinessController {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
57     private final CustomerCacheServiceProvider cacheServiceProvider;
58
59     @Autowired
60     public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider) {
61         this.cacheServiceProvider = cacheServiceProvider;
62     }
63
64     @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
65     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
66             final HttpServletRequest request) {
67         LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
68
69         final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
70         if (optional.isPresent()) {
71             final Customer customer = optional.get();
72             LOGGER.info("found customer {} in cache", customer);
73             return ResponseEntity.ok(customer);
74         }
75
76         LOGGER.error("Couldn't find {} in cache", globalCustomerId);
77         return getRequestErrorResponseEntity(request);
78     }
79
80     @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
81             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
82     public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
83             @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
84         LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
85
86         if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
87             customer.setResourceVersion(getResourceVersion());
88
89         }
90         cacheServiceProvider.putCustomer(globalCustomerId, customer);
91         return ResponseEntity.accepted().build();
92
93     }
94
95     @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
96             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
97     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
98             @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
99         LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
100                 globalCustomerId, serviceType);
101
102         final Optional<ServiceSubscription> optional =
103                 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
104         if (optional.isPresent()) {
105             final ServiceSubscription serviceSubscription = optional.get();
106             LOGGER.info("found service subscription  {} in cache", serviceSubscription);
107             return ResponseEntity.ok(serviceSubscription);
108         }
109
110         LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
111                 serviceType);
112         return getRequestErrorResponseEntity(request);
113     }
114
115     @GetMapping(
116             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
117             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
118     public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
119             @PathVariable("service-type") final String serviceType,
120             @RequestParam(name = "service-instance-name") final String serviceInstanceName,
121             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
122
123         LOGGER.info(
124                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
125                 globalCustomerId, serviceType, serviceInstanceName, depth);
126
127         final Optional<ServiceInstances> optional =
128                 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
129         if (optional.isPresent()) {
130             final ServiceInstances serviceInstances = optional.get();
131             LOGGER.info("found service instance  {} in cache", serviceInstances);
132             return ResponseEntity.ok(serviceInstances);
133         }
134         LOGGER.error(
135                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
136                 globalCustomerId, serviceType, serviceInstanceName, depth);
137         return getRequestErrorResponseEntity(request);
138     }
139
140     @GetMapping(
141             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
142             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
143     public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
144             @PathVariable("service-type") final String serviceType,
145             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
146             @RequestParam(name = "depth", required = false) final Integer depth,
147             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
148             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
149             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
150
151         LOGGER.info(
152                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
153                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
154
155         final Optional<ServiceInstance> optional =
156                 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
157         if (optional.isPresent()) {
158             final ServiceInstance serviceInstance = optional.get();
159             LOGGER.info("found service instance  {} in cache", serviceInstance);
160             return ResponseEntity.ok(serviceInstance);
161         }
162         LOGGER.error(
163                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
164                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
165         return getRequestErrorResponseEntity(request);
166     }
167
168     @PutMapping(
169             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
170             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
171     public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
172             @PathVariable("service-type") final String serviceType,
173             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
174             @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
175
176         LOGGER.info(
177                 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
178                 globalCustomerId, serviceType, serviceInstanceId);
179
180         if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
181             serviceInstance.setResourceVersion(getResourceVersion());
182         }
183
184         if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
185                 serviceInstance)) {
186             return ResponseEntity.accepted().build();
187         }
188
189         LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
190                 globalCustomerId, serviceType, serviceInstanceId);
191         return getRequestErrorResponseEntity(request);
192     }
193
194     private ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) {
195         return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE)
196                 .variables(request.getMethod(), request.getRequestURI(),
197                         "Node Not Found:No Node of type service-instance found at: " + request.getRequestURI(),
198                         "ERR.5.4.6114")
199                 .build(), HttpStatus.NOT_FOUND);
200     }
201
202 }