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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aai.simulator.controller;
22 import static org.onap.so.aai.simulator.utils.Constants.CUSTOMER_URL;
23 import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE;
24 import static org.onap.so.aai.simulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
25 import static org.onap.so.aai.simulator.utils.Utils.getRequestErrorResponseEntity;
26 import static org.onap.so.aai.simulator.utils.Utils.getResourceVersion;
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.Customer;
31 import org.onap.aai.domain.yang.ServiceInstance;
32 import org.onap.aai.domain.yang.ServiceInstances;
33 import org.onap.aai.domain.yang.ServiceSubscription;
34 import org.onap.so.aai.simulator.models.NodeServiceInstance;
35 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
36 import org.onap.so.aai.simulator.service.providers.NodesCacheServiceProvider;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestHeader;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestParam;
53 * @author waqas.ikram@ericsson.com
57 @RequestMapping(path = CUSTOMER_URL)
58 public class BusinessController {
60 private static final String SERVICE_SUBSCRIPTION = "service-subscription";
61 private static final String CUSTOMER_TYPE = "Customer";
62 private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
63 private final CustomerCacheServiceProvider cacheServiceProvider;
64 private final NodesCacheServiceProvider nodesCacheServiceProvider;
67 public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
68 final NodesCacheServiceProvider nodesCacheServiceProvider) {
69 this.cacheServiceProvider = cacheServiceProvider;
70 this.nodesCacheServiceProvider = nodesCacheServiceProvider;
73 @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
74 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
75 final HttpServletRequest request) {
76 LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
78 final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
79 if (optional.isPresent()) {
80 final Customer customer = optional.get();
81 LOGGER.info("found customer {} in cache", customer);
82 return ResponseEntity.ok(customer);
85 LOGGER.error("Couldn't find {} in cache", globalCustomerId);
86 return getRequestErrorResponseEntity(request, CUSTOMER_TYPE);
89 @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
90 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
91 public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
92 @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
93 LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
95 if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
96 customer.setResourceVersion(getResourceVersion());
99 cacheServiceProvider.putCustomer(globalCustomerId, customer);
100 return ResponseEntity.accepted().build();
104 @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
105 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
106 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
107 @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
108 LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
109 globalCustomerId, serviceType);
111 final Optional<ServiceSubscription> optional =
112 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
113 if (optional.isPresent()) {
114 final ServiceSubscription serviceSubscription = optional.get();
115 LOGGER.info("found service subscription {} in cache", serviceSubscription);
116 return ResponseEntity.ok(serviceSubscription);
119 LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
121 return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
124 @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
125 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
126 public ResponseEntity<?> putServiceSubscription(@PathVariable("global-customer-id") final String globalCustomerId,
127 @PathVariable("service-type") final String serviceType,
128 @RequestBody final ServiceSubscription serviceSubscription, final HttpServletRequest request) {
129 LOGGER.info("Will add service subscription for 'global customer id': {} and 'service type': {} ...",
130 globalCustomerId, serviceType);
132 if (cacheServiceProvider.putServiceSubscription(globalCustomerId, serviceType, serviceSubscription)) {
133 LOGGER.info("Successfully add service subscription in cache ...");
134 return ResponseEntity.accepted().build();
137 LOGGER.error("Couldn't add service subscription using 'global customer id': {} and 'service type': {}",
138 globalCustomerId, serviceType);
139 return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
143 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
144 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
145 public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
146 @PathVariable("service-type") final String serviceType,
147 @RequestParam(name = "service-instance-name") final String serviceInstanceName,
148 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
151 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
152 globalCustomerId, serviceType, serviceInstanceName, depth);
154 final Optional<ServiceInstances> optional =
155 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
156 if (optional.isPresent()) {
157 final ServiceInstances serviceInstances = optional.get();
158 LOGGER.info("found service instance {} in cache", serviceInstances);
159 return ResponseEntity.ok(serviceInstances);
162 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
163 globalCustomerId, serviceType, serviceInstanceName, depth);
164 return getRequestErrorResponseEntity(request);
168 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
169 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
170 public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
171 @PathVariable("service-type") final String serviceType,
172 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
173 @RequestParam(name = "depth", required = false) final Integer depth,
174 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
175 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
176 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
179 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
180 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
182 final Optional<ServiceInstance> optional =
183 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
184 if (optional.isPresent()) {
185 final ServiceInstance serviceInstance = optional.get();
186 LOGGER.info("found service instance {} in cache", serviceInstance);
187 return ResponseEntity.ok(serviceInstance);
190 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
191 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
192 return getRequestErrorResponseEntity(request);
196 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
197 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
198 public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
199 @PathVariable("service-type") final String serviceType,
200 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
201 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String invocationId,
202 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
205 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
206 globalCustomerId, serviceType, serviceInstanceId);
208 if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
209 serviceInstance.setResourceVersion(getResourceVersion());
212 if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
214 nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
215 globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
216 return ResponseEntity.accepted().build();
219 LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
220 globalCustomerId, serviceType, serviceInstanceId);
221 return getRequestErrorResponseEntity(request);
225 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
226 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
227 public ResponseEntity<?> patchSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
228 @PathVariable("service-type") final String serviceType,
229 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
230 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
231 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
234 "Will post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...",
235 globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
237 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
238 cacheServiceProvider.patchServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
240 return ResponseEntity.accepted().build();
242 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
244 return getRequestErrorResponseEntity(request);