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.APPLICATION_MERGE_PATCH_JSON;
23 import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
24 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNFS_URL;
26 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.VF_MODULE;
28 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
29 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
30 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
31 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
32 import java.util.List;
33 import java.util.Optional;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.core.MediaType;
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.VfModule;
40 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
41 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
42 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.HttpHeaders;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.DeleteMapping;
52 import org.springframework.web.bind.annotation.GetMapping;
53 import org.springframework.web.bind.annotation.PathVariable;
54 import org.springframework.web.bind.annotation.PostMapping;
55 import org.springframework.web.bind.annotation.PutMapping;
56 import org.springframework.web.bind.annotation.RequestBody;
57 import org.springframework.web.bind.annotation.RequestHeader;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestParam;
62 * @author Waqas Ikram (waqas.ikram@est.tech)
66 @RequestMapping(path = GENERIC_VNFS_URL)
67 public class GenericVnfsController {
69 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
71 private final GenericVnfCacheServiceProvider cacheServiceProvider;
75 public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) {
76 this.cacheServiceProvider = cacheServiceProvider;
79 @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
80 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
81 public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
82 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
83 LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
85 if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
86 genericVnf.setResourceVersion(getResourceVersion());
89 cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
90 return ResponseEntity.accepted().build();
94 @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
95 public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
96 @RequestParam(name = "depth", required = false) final Integer depth,
97 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
98 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
99 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
101 "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
102 vnfId, depth, resultIndex, resultSize, format);
104 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
106 if (optional.isPresent()) {
107 final GenericVnf genericVnf = optional.get();
108 LOGGER.info("found GenericVnf {} in cache", genericVnf);
109 return ResponseEntity.ok(genericVnf);
113 "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
114 vnfId, depth, resultIndex, resultSize, format);
115 return getRequestErrorResponseEntity(request, GENERIC_VNF);
119 @PutMapping(value = "/generic-vnf/{vnf-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
120 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
121 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
122 public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
123 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
124 LOGGER.info("Will add {} relationship for for 'vnf-id': {}...", relationship.getRelatedLink(), vnfId);
126 if (relationship.getRelatedLink() != null) {
127 final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
128 final HttpHeaders incomingHeader = getHeaders(request);
129 final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
130 request.getRequestURI(), vnfId, relationship);
132 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
133 return ResponseEntity.accepted().build();
136 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
137 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
140 @PutMapping(value = "/generic-vnf/{vnf-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
141 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
142 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
143 public ResponseEntity<?> putBiDirectionalRelationShip(@RequestBody final Relationship relationship,
144 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
145 LOGGER.info("Will put bi-directional relationship for 'vnf-id': {} ...", vnfId);
147 final Optional<Relationship> optional =
148 cacheServiceProvider.addRelationShip(vnfId, relationship, request.getRequestURI());
150 if (optional.isPresent()) {
151 final Relationship resultantRelationship = optional.get();
152 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
153 return ResponseEntity.accepted().body(resultantRelationship);
156 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
157 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
160 @PostMapping(value = "/generic-vnf/{vnf-id}",
161 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
162 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
163 public ResponseEntity<?> patchGenericVnf(@RequestBody final GenericVnf genericVnf,
164 @PathVariable("vnf-id") final String vnfId,
165 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
166 final HttpServletRequest request) {
168 LOGGER.info("Will post GenericVnf to cache with 'vnf-id': {} and '{}': {} ...", vnfId, X_HTTP_METHOD_OVERRIDE,
169 xHttpHeaderOverride);
171 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
172 if (cacheServiceProvider.patchGenericVnf(vnfId, genericVnf)) {
173 return ResponseEntity.accepted().build();
175 LOGGER.error("Unable to apply patch to GenericVnf using 'vnf-id': {} ... ", vnfId);
176 return getRequestErrorResponseEntity(request, GENERIC_VNF);
178 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
180 return getRequestErrorResponseEntity(request, GENERIC_VNF);
183 @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
184 public ResponseEntity<?> getAllGenericVnfs(final HttpServletRequest request) {
185 LOGGER.info("will retrieve all GenericVnfs");
186 final List<GenericVnf> genericVnfList = cacheServiceProvider.getGenericVnfs();
188 LOGGER.info("found {} GenericVnfs in cache", genericVnfList.size());
189 final GenericVnfs genericVnfs = new GenericVnfs();
190 genericVnfs.getGenericVnf().addAll(genericVnfList);
191 return ResponseEntity.ok(genericVnfs);
194 @DeleteMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
195 public ResponseEntity<?> deleteGenericVnf(@PathVariable("vnf-id") final String vnfId,
196 @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
197 LOGGER.info("Will delete GenericVnf for 'vnf-id': {} and 'resource-version': {}", vnfId, resourceVersion);
199 if (cacheServiceProvider.deleteGenericVnf(vnfId, resourceVersion)) {
200 LOGGER.info("Successfully delete GenericVnf from cache for 'vnf-id': {} and 'resource-version': {}", vnfId,
202 return ResponseEntity.noContent().build();
205 LOGGER.error("Unable to delete GenericVnf for 'vnf-id': {} and 'resource-version': {} ...", vnfId,
207 return getRequestErrorResponseEntity(request, GENERIC_VNF);
211 @GetMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
212 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
213 public ResponseEntity<?> getVfModule(@PathVariable("vnf-id") final String vnfId,
214 @PathVariable("vf-module-id") final String vfModuleId,
215 @RequestParam(name = "depth", required = false) final Integer depth,
216 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
217 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
218 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
220 "Will get VfModule for 'vf-module-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
221 vnfId, vfModuleId, depth, resultIndex, resultSize, format);
223 final Optional<VfModule> optional = cacheServiceProvider.getVfModule(vnfId, vfModuleId);
225 if (optional.isPresent()) {
226 final VfModule vfModule = optional.get();
227 LOGGER.info("found VfModule {} in cache", vfModule);
228 return ResponseEntity.ok(vfModule);
232 "Unable to find VfModule in cache for 'vf-module-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
233 vnfId, vfModuleId, depth, resultIndex, resultSize, format);
234 return getRequestErrorResponseEntity(request, VF_MODULE);
239 @PutMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
240 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
241 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
242 public ResponseEntity<?> putVfModule(@RequestBody final VfModule vfModule,
243 @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
244 final HttpServletRequest request) {
245 LOGGER.info("Will add VfModule to cache with 'vf-module-id': {} ...", vfModuleId);
246 if (vfModule.getResourceVersion() == null || vfModule.getResourceVersion().isEmpty()) {
247 vfModule.setResourceVersion(getResourceVersion());
249 cacheServiceProvider.putVfModule(vnfId, vfModuleId, vfModule);
250 return ResponseEntity.accepted().build();
253 @PostMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
254 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
255 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
256 public ResponseEntity<?> patchVfModule(@RequestBody final VfModule vfModule,
257 @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
258 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
259 final HttpServletRequest request) {
261 LOGGER.info("Will post VfModule to cache with 'vf-module-id': {} and '{}': {} ...", vfModuleId,
262 X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
264 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
265 if (cacheServiceProvider.patchVfModule(vnfId, vfModuleId, vfModule)) {
266 return ResponseEntity.accepted().build();
268 LOGGER.error("Unable to apply patch to VmModule using 'vf-module-id': {} ... ", vfModule);
269 return getRequestErrorResponseEntity(request, VF_MODULE);
271 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
273 return getRequestErrorResponseEntity(request, VF_MODULE);
278 value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
279 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
280 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
281 public ResponseEntity<?> putBiDirectionalVfModuleRelationShip(@RequestBody final Relationship relationship,
282 @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
283 final HttpServletRequest request) {
284 LOGGER.info("Will add {} bi-directional relationship to : {} ...", relationship.getRelatedTo());
286 final Optional<Relationship> optional =
287 cacheServiceProvider.addRelationShip(vnfId, vfModuleId, relationship, request.getRequestURI());
289 if (optional.isPresent()) {
290 final Relationship resultantRelationship = optional.get();
291 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
292 return ResponseEntity.accepted().body(resultantRelationship);
295 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
296 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
299 @DeleteMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
300 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
301 public ResponseEntity<?> deleteVfModule(@PathVariable("vnf-id") final String vnfId,
302 @PathVariable("vf-module-id") final String vfModuleId,
303 @RequestParam(name = "resource-version") final String resourceVersion,
304 final HttpServletRequest request) {
305 LOGGER.info("Deleting VfModule from cache with 'vf-module-id': {} ...", vfModuleId);
307 boolean response = cacheServiceProvider.deleteVfModule(vnfId, vfModuleId, resourceVersion);
309 return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
311 return ResponseEntity.status(HttpStatus.NOT_FOUND).build();