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.GENERIC_VNF;
24 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNFS_URL;
25 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
26 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
27 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
28 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
29 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
30 import java.util.Optional;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.core.MediaType;
33 import org.onap.aai.domain.yang.GenericVnf;
34 import org.onap.aai.domain.yang.Relationship;
35 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
36 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
37 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PathVariable;
47 import org.springframework.web.bind.annotation.PostMapping;
48 import org.springframework.web.bind.annotation.PutMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestHeader;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RequestParam;
55 * @author Waqas Ikram (waqas.ikram@est.tech)
59 @RequestMapping(path = GENERIC_VNFS_URL)
60 public class GenericVnfsController {
62 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
64 private final GenericVnfCacheServiceProvider cacheServiceProvider;
68 public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) {
69 this.cacheServiceProvider = cacheServiceProvider;
72 @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
73 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
74 public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
75 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
76 LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
78 if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
79 genericVnf.setResourceVersion(getResourceVersion());
82 cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
83 return ResponseEntity.accepted().build();
87 @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
88 public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
89 @RequestParam(name = "depth", required = false) final Integer depth,
90 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
91 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
92 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
94 "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
95 vnfId, depth, resultIndex, resultSize, format);
97 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
99 if (optional.isPresent()) {
100 final GenericVnf genericVnf = optional.get();
101 LOGGER.info("found GenericVnf {} in cache", genericVnf);
102 return ResponseEntity.ok(genericVnf);
106 "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
107 vnfId, depth, resultIndex, resultSize, format);
108 return getRequestErrorResponseEntity(request, GENERIC_VNF);
112 @PutMapping(value = "/generic-vnf/{vnf-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
113 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
114 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
115 public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
116 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
117 LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
119 if (relationship.getRelatedLink() != null) {
120 final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
121 final HttpHeaders incomingHeader = getHeaders(request);
122 final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
123 request.getRequestURI(), vnfId, relationship);
125 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
126 return ResponseEntity.accepted().build();
129 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
130 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
133 @PutMapping(value = "/generic-vnf/{vnf-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
134 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
135 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
136 public ResponseEntity<?> putBiDirectionalRelationShip(@RequestBody final Relationship relationship,
137 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
138 LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
140 final Optional<Relationship> optional =
141 cacheServiceProvider.addRelationShip(vnfId, relationship, request.getRequestURI());
143 if (optional.isPresent()) {
144 final Relationship resultantRelationship = optional.get();
145 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
146 return ResponseEntity.accepted().body(resultantRelationship);
149 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
150 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
153 @PostMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
154 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
155 public ResponseEntity<?> patchGenericVnf(@RequestBody final GenericVnf genericVnf,
156 @PathVariable("vnf-id") final String vnfId,
157 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
158 final HttpServletRequest request) {
160 LOGGER.info("Will post GenericVnf to cache with 'vnf-id': {} and '{}': {} ...", vnfId, X_HTTP_METHOD_OVERRIDE,
161 xHttpHeaderOverride);
163 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
164 if (cacheServiceProvider.patchGenericVnf(vnfId, genericVnf)) {
165 return ResponseEntity.accepted().build();
167 LOGGER.error("Unable to apply patch to GenericVnf using 'vnf-id': {} ... ", vnfId);
168 return getRequestErrorResponseEntity(request, GENERIC_VNF);
170 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
172 return getRequestErrorResponseEntity(request, GENERIC_VNF);