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.ERROR_MESSAGE;
24 import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE_ID;
25 import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE;
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.onap.so.aai.simulator.utils.RequestErrorBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
52 * @author waqas.ikram@ericsson.com
56 @RequestMapping(path = CUSTOMER_URL)
57 public class BusinessController {
59 private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
60 private final CustomerCacheServiceProvider cacheServiceProvider;
61 private final NodesCacheServiceProvider nodesCacheServiceProvider;
64 public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
65 final NodesCacheServiceProvider nodesCacheServiceProvider) {
66 this.cacheServiceProvider = cacheServiceProvider;
67 this.nodesCacheServiceProvider = nodesCacheServiceProvider;
70 @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
71 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
72 final HttpServletRequest request) {
73 LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
75 final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
76 if (optional.isPresent()) {
77 final Customer customer = optional.get();
78 LOGGER.info("found customer {} in cache", customer);
79 return ResponseEntity.ok(customer);
82 LOGGER.error("Couldn't find {} in cache", globalCustomerId);
83 return getRequestErrorResponseEntity(request);
86 @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
87 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
88 public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
89 @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
90 LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
92 if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
93 customer.setResourceVersion(getResourceVersion());
96 cacheServiceProvider.putCustomer(globalCustomerId, customer);
97 return ResponseEntity.accepted().build();
101 @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
102 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
103 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
104 @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
105 LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
106 globalCustomerId, serviceType);
108 final Optional<ServiceSubscription> optional =
109 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
110 if (optional.isPresent()) {
111 final ServiceSubscription serviceSubscription = optional.get();
112 LOGGER.info("found service subscription {} in cache", serviceSubscription);
113 return ResponseEntity.ok(serviceSubscription);
116 LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
118 return getRequestErrorResponseEntity(request);
122 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
123 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
124 public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
125 @PathVariable("service-type") final String serviceType,
126 @RequestParam(name = "service-instance-name") final String serviceInstanceName,
127 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
130 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
131 globalCustomerId, serviceType, serviceInstanceName, depth);
133 final Optional<ServiceInstances> optional =
134 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
135 if (optional.isPresent()) {
136 final ServiceInstances serviceInstances = optional.get();
137 LOGGER.info("found service instance {} in cache", serviceInstances);
138 return ResponseEntity.ok(serviceInstances);
141 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
142 globalCustomerId, serviceType, serviceInstanceName, depth);
143 return getRequestErrorResponseEntity(request);
147 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
148 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
149 public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
150 @PathVariable("service-type") final String serviceType,
151 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
152 @RequestParam(name = "depth", required = false) final Integer depth,
153 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
154 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
155 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
158 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
159 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
161 final Optional<ServiceInstance> optional =
162 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
163 if (optional.isPresent()) {
164 final ServiceInstance serviceInstance = optional.get();
165 LOGGER.info("found service instance {} in cache", serviceInstance);
166 return ResponseEntity.ok(serviceInstance);
169 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
170 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
171 return getRequestErrorResponseEntity(request);
175 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
176 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
177 public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
178 @PathVariable("service-type") final String serviceType,
179 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
180 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
183 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
184 globalCustomerId, serviceType, serviceInstanceId);
186 if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
187 serviceInstance.setResourceVersion(getResourceVersion());
190 if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
192 nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
193 globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
194 return ResponseEntity.accepted().build();
197 LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
198 globalCustomerId, serviceType, serviceInstanceId);
199 return getRequestErrorResponseEntity(request);
202 private ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) {
203 return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE)
204 .variables(request.getMethod(), request.getRequestURI(),
205 "Node Not Found:No Node of type service-instance found at: " + request.getRequestURI(),
207 .build(), HttpStatus.NOT_FOUND);