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.aaisimulator.controller;
22 import static org.onap.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.aaisimulator.utils.Constants.CUSTOMER_TYPE;
24 import static org.onap.aaisimulator.utils.Constants.CUSTOMER_URL;
25 import static org.onap.aaisimulator.utils.Constants.GENERIC_VNF;
26 import static org.onap.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
27 import static org.onap.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
28 import static org.onap.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION;
29 import static org.onap.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
30 import static org.onap.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
31 import static org.onap.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
32 import java.util.Optional;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.core.MediaType;
35 import org.onap.aai.domain.yang.Customer;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.aai.domain.yang.GenericVnfs;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.RelationshipData;
40 import org.onap.aai.domain.yang.ServiceInstance;
41 import org.onap.aai.domain.yang.ServiceInstances;
42 import org.onap.aai.domain.yang.ServiceSubscription;
43 import org.onap.aaisimulator.models.NodeServiceInstance;
44 import org.onap.aaisimulator.service.providers.CustomerCacheServiceProvider;
45 import org.onap.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
46 import org.onap.aaisimulator.service.providers.NodesCacheServiceProvider;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.stereotype.Controller;
53 import org.springframework.web.bind.annotation.DeleteMapping;
54 import org.springframework.web.bind.annotation.GetMapping;
55 import org.springframework.web.bind.annotation.PathVariable;
56 import org.springframework.web.bind.annotation.PostMapping;
57 import org.springframework.web.bind.annotation.PutMapping;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestHeader;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestParam;
64 * @author waqas.ikram@ericsson.com
68 @RequestMapping(path = CUSTOMER_URL)
69 public class BusinessController {
71 private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
72 private final CustomerCacheServiceProvider cacheServiceProvider;
73 private final NodesCacheServiceProvider nodesCacheServiceProvider;
74 private final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
77 public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
78 final NodesCacheServiceProvider nodesCacheServiceProvider,
79 final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider) {
80 this.cacheServiceProvider = cacheServiceProvider;
81 this.nodesCacheServiceProvider = nodesCacheServiceProvider;
82 this.genericVnfCacheServiceProvider = genericVnfCacheServiceProvider;
85 @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
86 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
87 final HttpServletRequest request) {
88 LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
90 final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
91 if (optional.isPresent()) {
92 final Customer customer = optional.get();
93 LOGGER.info("found customer {} in cache", customer);
94 return ResponseEntity.ok(customer);
97 LOGGER.error("Couldn't find {} in cache", globalCustomerId);
98 return getRequestErrorResponseEntity(request, CUSTOMER_TYPE);
101 @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
102 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
103 public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
104 @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
105 LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
107 if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
108 customer.setResourceVersion(getResourceVersion());
111 cacheServiceProvider.putCustomer(globalCustomerId, customer);
112 return ResponseEntity.accepted().build();
116 @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
117 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
118 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
119 @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
120 LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
121 globalCustomerId, serviceType);
123 final Optional<ServiceSubscription> optional =
124 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
125 if (optional.isPresent()) {
126 final ServiceSubscription serviceSubscription = optional.get();
127 LOGGER.info("found service subscription {} in cache", serviceSubscription);
128 return ResponseEntity.ok(serviceSubscription);
131 LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
133 return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
136 @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
137 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
138 public ResponseEntity<?> putServiceSubscription(@PathVariable("global-customer-id") final String globalCustomerId,
139 @PathVariable("service-type") final String serviceType,
140 @RequestBody final ServiceSubscription serviceSubscription, final HttpServletRequest request) {
141 LOGGER.info("Will add service subscription for 'global customer id': {} and 'service type': {} ...",
142 globalCustomerId, serviceType);
144 if (cacheServiceProvider.putServiceSubscription(globalCustomerId, serviceType, serviceSubscription)) {
145 LOGGER.info("Successfully add service subscription in cache ...");
146 return ResponseEntity.accepted().build();
149 LOGGER.error("Couldn't add service subscription using 'global customer id': {} and 'service type': {}",
150 globalCustomerId, serviceType);
151 return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
155 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
156 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
157 public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
158 @PathVariable("service-type") final String serviceType,
159 @RequestParam(name = "service-instance-name") final String serviceInstanceName,
160 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
163 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
164 globalCustomerId, serviceType, serviceInstanceName, depth);
166 final Optional<ServiceInstances> optional =
167 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
168 if (optional.isPresent()) {
169 final ServiceInstances serviceInstances = optional.get();
170 LOGGER.info("found service instance {} in cache", serviceInstances);
171 return ResponseEntity.ok(serviceInstances);
174 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
175 globalCustomerId, serviceType, serviceInstanceName, depth);
176 return getRequestErrorResponseEntity(request);
180 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
181 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
182 public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
183 @PathVariable("service-type") final String serviceType,
184 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
185 @RequestParam(name = "depth", required = false) final Integer depth,
186 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
187 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
188 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
191 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
192 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
194 final Optional<ServiceInstance> optional =
195 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
196 if (optional.isPresent()) {
197 final ServiceInstance serviceInstance = optional.get();
198 LOGGER.info("found service instance {} in cache", serviceInstance);
199 return ResponseEntity.ok(serviceInstance);
202 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
203 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
204 return getRequestErrorResponseEntity(request);
208 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
209 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
210 public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
211 @PathVariable("service-type") final String serviceType,
212 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
213 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String invocationId,
214 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
217 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
218 globalCustomerId, serviceType, serviceInstanceId);
220 if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
221 serviceInstance.setResourceVersion(getResourceVersion());
224 if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
226 nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
227 globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
228 return ResponseEntity.accepted().build();
231 LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
232 globalCustomerId, serviceType, serviceInstanceId);
233 return getRequestErrorResponseEntity(request);
237 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
238 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
239 public ResponseEntity<?> patchSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
240 @PathVariable("service-type") final String serviceType,
241 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
242 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
243 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
246 "Will post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...",
247 globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
249 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
250 cacheServiceProvider.patchServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
252 return ResponseEntity.accepted().build();
254 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
256 return getRequestErrorResponseEntity(request);
261 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/related-to/generic-vnfs",
262 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
263 public ResponseEntity<?> getRelatedToGenericVnf(@PathVariable("global-customer-id") final String globalCustomerId,
264 @PathVariable("service-type") final String serviceType,
265 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
266 @RequestParam(name = "vnf-name", required = true) final String vnfName, final HttpServletRequest request) {
269 "Will retrieve generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
270 globalCustomerId, serviceType, serviceInstanceId, vnfName);
272 final Optional<Relationship> optional =
273 cacheServiceProvider.getRelationship(globalCustomerId, serviceType, serviceInstanceId, vnfName);
275 if (optional.isPresent()) {
277 final Relationship relationship = optional.get();
278 final Optional<RelationshipData> relationshipDataOptional = relationship.getRelationshipData().stream()
279 .filter(existing -> GENERIC_VNF_VNF_ID.equals(existing.getRelationshipKey())).findFirst();
281 if (relationshipDataOptional.isPresent()) {
282 final RelationshipData relationshipData = relationshipDataOptional.get();
283 final String vnfId = relationshipData.getRelationshipValue();
284 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(vnfId);
285 if (genericVnfOptional.isPresent()) {
286 final GenericVnfs genericVnfs = new GenericVnfs();
287 genericVnfs.getGenericVnf().add(genericVnfOptional.get());
288 LOGGER.info("found service instance {} in cache", relationship);
289 return ResponseEntity.ok(genericVnfs);
294 "Couldn't find generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
295 globalCustomerId, serviceType, serviceInstanceId, vnfName);
296 return getRequestErrorResponseEntity(request, GENERIC_VNF);
300 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}"
301 + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
302 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
303 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
304 public ResponseEntity<?> putSericeInstanceRelationShip(
305 @PathVariable("global-customer-id") final String globalCustomerId,
306 @PathVariable("service-type") final String serviceType,
307 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
308 @RequestBody final Relationship relationship, final HttpServletRequest request) {
311 "Will add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
312 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
313 final Optional<Relationship> optional = cacheServiceProvider.addRelationShip(globalCustomerId, serviceType,
314 serviceInstanceId, relationship, request.getRequestURI());
316 if (optional.isPresent()) {
317 final Relationship resultantRelationship = optional.get();
318 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
319 return ResponseEntity.accepted().body(resultantRelationship);
323 "Couldn't add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
324 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
326 return getRequestErrorResponseEntity(request);
330 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
331 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
332 public ResponseEntity<?> deleteSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
333 @PathVariable("service-type") final String serviceType,
334 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
335 @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
338 "Will delete SericeInstance for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
339 globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
341 if (cacheServiceProvider.deleteSericeInstance(globalCustomerId, serviceType, serviceInstanceId,
344 "Successfully deleted SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
345 globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
346 return ResponseEntity.noContent().build();
350 "Unable to delete SericeInstance from cache for 'global-customer-id': {}, 'service-type': {}, 'service-instance-id': {} and 'resource-version': {}",
351 globalCustomerId, serviceType, serviceInstanceId, resourceVersion);
353 return getRequestErrorResponseEntity(request);