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.aaisimulator.controller;
22 import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_TYPE;
24 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_URL;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
27 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
28 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION;
29 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
30 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
31 import static org.onap.so.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.so.aaisimulator.models.NodeServiceInstance;
44 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
45 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
46 import org.onap.so.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.GetMapping;
54 import org.springframework.web.bind.annotation.PathVariable;
55 import org.springframework.web.bind.annotation.PostMapping;
56 import org.springframework.web.bind.annotation.PutMapping;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestHeader;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestParam;
63 * @author waqas.ikram@ericsson.com
67 @RequestMapping(path = CUSTOMER_URL)
68 public class BusinessController {
70 private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
71 private final CustomerCacheServiceProvider cacheServiceProvider;
72 private final NodesCacheServiceProvider nodesCacheServiceProvider;
73 private final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
76 public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
77 final NodesCacheServiceProvider nodesCacheServiceProvider,
78 final GenericVnfCacheServiceProvider genericVnfCacheServiceProvider) {
79 this.cacheServiceProvider = cacheServiceProvider;
80 this.nodesCacheServiceProvider = nodesCacheServiceProvider;
81 this.genericVnfCacheServiceProvider = genericVnfCacheServiceProvider;
84 @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
85 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
86 final HttpServletRequest request) {
87 LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
89 final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
90 if (optional.isPresent()) {
91 final Customer customer = optional.get();
92 LOGGER.info("found customer {} in cache", customer);
93 return ResponseEntity.ok(customer);
96 LOGGER.error("Couldn't find {} in cache", globalCustomerId);
97 return getRequestErrorResponseEntity(request, CUSTOMER_TYPE);
100 @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
101 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
102 public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
103 @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
104 LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
106 if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
107 customer.setResourceVersion(getResourceVersion());
110 cacheServiceProvider.putCustomer(globalCustomerId, customer);
111 return ResponseEntity.accepted().build();
115 @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
116 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
117 public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
118 @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
119 LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
120 globalCustomerId, serviceType);
122 final Optional<ServiceSubscription> optional =
123 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
124 if (optional.isPresent()) {
125 final ServiceSubscription serviceSubscription = optional.get();
126 LOGGER.info("found service subscription {} in cache", serviceSubscription);
127 return ResponseEntity.ok(serviceSubscription);
130 LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
132 return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
135 @PutMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
136 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
137 public ResponseEntity<?> putServiceSubscription(@PathVariable("global-customer-id") final String globalCustomerId,
138 @PathVariable("service-type") final String serviceType,
139 @RequestBody final ServiceSubscription serviceSubscription, final HttpServletRequest request) {
140 LOGGER.info("Will add service subscription for 'global customer id': {} and 'service type': {} ...",
141 globalCustomerId, serviceType);
143 if (cacheServiceProvider.putServiceSubscription(globalCustomerId, serviceType, serviceSubscription)) {
144 LOGGER.info("Successfully add service subscription in cache ...");
145 return ResponseEntity.accepted().build();
148 LOGGER.error("Couldn't add service subscription using 'global customer id': {} and 'service type': {}",
149 globalCustomerId, serviceType);
150 return getRequestErrorResponseEntity(request, SERVICE_SUBSCRIPTION);
154 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
155 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
156 public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
157 @PathVariable("service-type") final String serviceType,
158 @RequestParam(name = "service-instance-name") final String serviceInstanceName,
159 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
162 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
163 globalCustomerId, serviceType, serviceInstanceName, depth);
165 final Optional<ServiceInstances> optional =
166 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
167 if (optional.isPresent()) {
168 final ServiceInstances serviceInstances = optional.get();
169 LOGGER.info("found service instance {} in cache", serviceInstances);
170 return ResponseEntity.ok(serviceInstances);
173 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
174 globalCustomerId, serviceType, serviceInstanceName, depth);
175 return getRequestErrorResponseEntity(request);
179 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
180 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
181 public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
182 @PathVariable("service-type") final String serviceType,
183 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
184 @RequestParam(name = "depth", required = false) final Integer depth,
185 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
186 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
187 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
190 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
191 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
193 final Optional<ServiceInstance> optional =
194 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
195 if (optional.isPresent()) {
196 final ServiceInstance serviceInstance = optional.get();
197 LOGGER.info("found service instance {} in cache", serviceInstance);
198 return ResponseEntity.ok(serviceInstance);
201 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
202 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
203 return getRequestErrorResponseEntity(request);
207 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
208 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
209 public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
210 @PathVariable("service-type") final String serviceType,
211 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
212 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String invocationId,
213 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
216 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
217 globalCustomerId, serviceType, serviceInstanceId);
219 if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
220 serviceInstance.setResourceVersion(getResourceVersion());
223 if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
225 nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
226 globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
227 return ResponseEntity.accepted().build();
230 LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
231 globalCustomerId, serviceType, serviceInstanceId);
232 return getRequestErrorResponseEntity(request);
236 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
237 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
238 public ResponseEntity<?> patchSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
239 @PathVariable("service-type") final String serviceType,
240 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
241 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
242 @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
245 "Will post service instance for 'global customer id': {}, 'service type': {}, 'service instance id: '{} and '{}': {}...",
246 globalCustomerId, serviceType, serviceInstanceId, X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
248 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
249 cacheServiceProvider.patchServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
251 return ResponseEntity.accepted().build();
253 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
255 return getRequestErrorResponseEntity(request);
260 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/related-to/generic-vnfs",
261 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
262 public ResponseEntity<?> getRelatedToGenericVnf(@PathVariable("global-customer-id") final String globalCustomerId,
263 @PathVariable("service-type") final String serviceType,
264 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
265 @RequestParam(name = "vnf-name", required = true) final String vnfName, final HttpServletRequest request) {
268 "Will retrieve generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
269 globalCustomerId, serviceType, serviceInstanceId, vnfName);
271 final Optional<Relationship> optional =
272 cacheServiceProvider.getRelationship(globalCustomerId, serviceType, serviceInstanceId, vnfName);
274 if (optional.isPresent()) {
276 final Relationship relationship = optional.get();
277 final Optional<RelationshipData> relationshipDataOptional = relationship.getRelationshipData().stream()
278 .filter(existing -> GENERIC_VNF_VNF_ID.equals(existing.getRelationshipKey())).findFirst();
280 if (relationshipDataOptional.isPresent()) {
281 final RelationshipData relationshipData = relationshipDataOptional.get();
282 final String vnfId = relationshipData.getRelationshipValue();
283 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(vnfId);
284 if (genericVnfOptional.isPresent()) {
285 final GenericVnfs genericVnfs = new GenericVnfs();
286 genericVnfs.getGenericVnf().add(genericVnfOptional.get());
287 LOGGER.info("found service instance {} in cache", relationship);
288 return ResponseEntity.ok(genericVnfs);
293 "Couldn't find generic vnf related to information for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with vnfname: {}...",
294 globalCustomerId, serviceType, serviceInstanceId, vnfName);
295 return getRequestErrorResponseEntity(request, GENERIC_VNF);
299 value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}"
300 + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
301 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
302 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
303 public ResponseEntity<?> putSericeInstanceRelationShip(
304 @PathVariable("global-customer-id") final String globalCustomerId,
305 @PathVariable("service-type") final String serviceType,
306 @PathVariable(name = "service-instance-id") final String serviceInstanceId,
307 @RequestBody final Relationship relationship, final HttpServletRequest request) {
310 "Will add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
311 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
312 final Optional<Relationship> optional = cacheServiceProvider.addRelationShip(globalCustomerId, serviceType,
313 serviceInstanceId, relationship, request.getRequestURI());
315 if (optional.isPresent()) {
316 final Relationship resultantRelationship = optional.get();
317 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
318 return ResponseEntity.accepted().body(resultantRelationship);
322 "Couldn't add {} relationship for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
323 relationship.getRelatedTo(), globalCustomerId, serviceType, serviceInstanceId);
325 return getRequestErrorResponseEntity(request);