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.Constant.BUSINESS_URL;
23 import static org.onap.so.aai.simulator.utils.Constant.ERROR_MESSAGE;
24 import static org.onap.so.aai.simulator.utils.Constant.ERROR_MESSAGE_ID;
25 import java.util.Optional;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.aai.domain.yang.Customer;
29 import org.onap.aai.domain.yang.ServiceInstance;
30 import org.onap.aai.domain.yang.ServiceInstances;
31 import org.onap.aai.domain.yang.ServiceSubscription;
32 import org.onap.so.aai.simulator.service.providers.CacheServiceProvider;
33 import org.onap.so.aai.simulator.utils.RequestErrorBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PutMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
48 * @author waqas.ikram@ericsson.com
52 @RequestMapping(path = BUSINESS_URL)
53 public class BusinessController {
55 private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
56 private final CacheServiceProvider cacheServiceProvider;
59 public BusinessController(final CacheServiceProvider cacheServiceProvider) {
60 this.cacheServiceProvider = cacheServiceProvider;
63 @GetMapping(value = "/customers/customer/{global-customer-id}",
64 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);
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);
76 LOGGER.error("Couldn't find {} in cache", globalCustomerId);
77 return getRequestErrorResponseEntity(request);
80 @PutMapping(value = "/customers/customer/{global-customer-id}",
81 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
82 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
83 public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
84 @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
85 LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
87 if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
88 customer.setResourceVersion(getResourceVersion());
91 cacheServiceProvider.putCustomer(globalCustomerId, customer);
92 return ResponseEntity.accepted().build();
97 value = "/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
98 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
99 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
100 @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
101 LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
102 globalCustomerId, serviceType);
104 final Optional<ServiceSubscription> optional =
105 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
106 if (optional.isPresent()) {
107 final ServiceSubscription serviceSubscription = optional.get();
108 LOGGER.info("found service subscription {} in cache", serviceSubscription);
109 return ResponseEntity.ok(serviceSubscription);
112 LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
114 return getRequestErrorResponseEntity(request);
118 value = "/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
119 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
120 public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
121 @PathVariable("service-type") final String serviceType,
122 @RequestParam(name = "service-instance-name") final String serviceInstanceName,
123 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
126 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
127 globalCustomerId, serviceType, serviceInstanceName, depth);
129 final Optional<ServiceInstances> optional =
130 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
131 if (optional.isPresent()) {
132 final ServiceInstances serviceInstances = optional.get();
133 LOGGER.info("found service instance {} in cache", serviceInstances);
134 return ResponseEntity.ok(serviceInstances);
137 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
138 globalCustomerId, serviceType, serviceInstanceName, depth);
139 return getRequestErrorResponseEntity(request);
143 value = "/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
144 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
145 public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
146 @PathVariable("service-type") final String serviceType,
147 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
148 @RequestParam(name = "depth", required = false) final Integer depth,
149 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
150 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
151 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
154 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
155 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
157 final Optional<ServiceInstance> optional =
158 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
159 if (optional.isPresent()) {
160 final ServiceInstance serviceInstance = optional.get();
161 LOGGER.info("found service instance {} in cache", serviceInstance);
162 return ResponseEntity.ok(serviceInstance);
165 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
166 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
167 return getRequestErrorResponseEntity(request);
171 value = "/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
172 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
173 public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
174 @PathVariable("service-type") final String serviceType,
175 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
176 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
179 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
180 globalCustomerId, serviceType, serviceInstanceId);
182 if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
183 serviceInstance.setResourceVersion(getResourceVersion());
186 if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
188 ResponseEntity.accepted().build();
192 "Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} to cache",
193 globalCustomerId, serviceType, serviceInstanceId);
194 return getRequestErrorResponseEntity(request);
197 private String getResourceVersion() {
198 return System.currentTimeMillis() + "";
201 private ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) {
202 return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE)
203 .variables(request.getMethod(), request.getRequestURI(),
204 "Node Not Found:No Node of type service-instance found at: " + request.getRequestURI(),
206 .build(), HttpStatus.NOT_FOUND);