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.VF_MODULE;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNFS_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
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.aai.domain.yang.VfModules;
41 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
42 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
43 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpMethod;
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.PatchMapping;
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 (waqas.ikram@est.tech)
67 @RequestMapping(path = GENERIC_VNFS_URL)
68 public class GenericVnfsController {
70 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
72 private final GenericVnfCacheServiceProvider cacheServiceProvider;
76 public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) {
77 this.cacheServiceProvider = cacheServiceProvider;
80 @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
81 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
82 public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
83 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
84 LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
86 if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
87 genericVnf.setResourceVersion(getResourceVersion());
90 cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
91 return ResponseEntity.accepted().build();
95 @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
96 public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
97 @RequestParam(name = "depth", required = false) final Integer depth,
98 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
99 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
100 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
102 "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
103 vnfId, depth, resultIndex, resultSize, format);
105 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
107 if (optional.isPresent()) {
108 final GenericVnf genericVnf = optional.get();
109 LOGGER.info("found GenericVnf {} in cache", genericVnf);
110 return ResponseEntity.ok(genericVnf);
114 "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
115 vnfId, depth, resultIndex, resultSize, format);
116 return getRequestErrorResponseEntity(request, GENERIC_VNF);
120 @PutMapping(value = "/generic-vnf/{vnf-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
121 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
122 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
123 public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
124 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
125 LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
127 if (relationship.getRelatedLink() != null) {
128 final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
129 final HttpHeaders incomingHeader = getHeaders(request);
130 final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
131 request.getRequestURI(), vnfId, relationship);
133 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
134 return ResponseEntity.accepted().build();
137 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
138 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
141 @PutMapping(value = "/generic-vnf/{vnf-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
142 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
143 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
144 public ResponseEntity<?> putBiDirectionalRelationShip(@RequestBody final Relationship relationship,
145 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
146 LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
148 final Optional<Relationship> optional =
149 cacheServiceProvider.addRelationShip(vnfId, relationship, request.getRequestURI());
151 if (optional.isPresent()) {
152 final Relationship resultantRelationship = optional.get();
153 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
154 return ResponseEntity.accepted().body(resultantRelationship);
157 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
158 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
161 @PostMapping(value = "/generic-vnf/{vnf-id}",
162 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
163 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
164 public ResponseEntity<?> patchGenericVnf(@RequestBody final GenericVnf genericVnf,
165 @PathVariable("vnf-id") final String vnfId,
166 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
167 final HttpServletRequest request) {
169 LOGGER.info("Will post GenericVnf to cache with 'vnf-id': {} and '{}': {} ...", vnfId, X_HTTP_METHOD_OVERRIDE,
170 xHttpHeaderOverride);
172 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
173 if (cacheServiceProvider.patchGenericVnf(vnfId, genericVnf)) {
174 return ResponseEntity.accepted().build();
176 LOGGER.error("Unable to apply patch to GenericVnf using 'vnf-id': {} ... ", vnfId);
177 return getRequestErrorResponseEntity(request, GENERIC_VNF);
179 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
181 return getRequestErrorResponseEntity(request, GENERIC_VNF);
184 @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
185 public ResponseEntity<?> getGenericVnfs(@RequestParam(name = "selflink") final String selflink,
186 final HttpServletRequest request) {
187 LOGGER.info("will retrieve GenericVnfs using selflink: {}", selflink);
189 final List<GenericVnf> genericVnfList = cacheServiceProvider.getGenericVnfs(selflink);
191 if (genericVnfList.isEmpty()) {
192 LOGGER.error("No matching generic vnfs found using selflink: {}", selflink);
193 return getRequestErrorResponseEntity(request, GENERIC_VNF);
196 LOGGER.info("found {} GenericVnfs in cache", genericVnfList.size());
197 final GenericVnfs genericVnfs = new GenericVnfs();
198 genericVnfs.getGenericVnf().addAll(genericVnfList);
199 return ResponseEntity.ok(genericVnfs);
202 @DeleteMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
203 public ResponseEntity<?> deleteGenericVnf(@PathVariable("vnf-id") final String vnfId,
204 @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
205 LOGGER.info("Will delete GenericVnf for 'vnf-id': {} and 'resource-version': {}", vnfId, resourceVersion);
207 if (cacheServiceProvider.deleteGenericVnf(vnfId, resourceVersion)) {
208 LOGGER.info("Successfully delete GenericVnf from cache for 'vnf-id': {} and 'resource-version': {}", vnfId,
210 return ResponseEntity.noContent().build();
213 LOGGER.error("Unable to delete GenericVnf for 'vnf-id': {} and 'resource-version': {} ...", vnfId,
215 return getRequestErrorResponseEntity(request, GENERIC_VNF);
220 @GetMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
221 public ResponseEntity<?> getVfModule(@PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
222 @RequestParam(name = "depth", required = false) final Integer depth,
223 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
224 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
225 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
227 "Will get VfModule for 'vf-module-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
228 vnfId, vfModuleId, depth, resultIndex, resultSize, format);
230 final Optional<VfModule> optional = cacheServiceProvider.getVfModule(vnfId, vfModuleId);
232 if (optional.isPresent()) {
233 final VfModule vfModule = optional.get();
234 LOGGER.info("found VfModule {} in cache", vfModule);
235 return ResponseEntity.ok(vfModule);
239 "Unable to find VfModule in cache for 'vf-module-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
240 vnfId, vfModuleId, depth, resultIndex, resultSize, format);
241 return getRequestErrorResponseEntity(request, VF_MODULE);
246 @PutMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
247 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
248 public ResponseEntity<?> putVfModule(@RequestBody final VfModule vfModule,
249 @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId, final HttpServletRequest request) {
250 LOGGER.info("Will add VfModule to cache with 'vf-module-id': {} ...", vfModuleId);
252 cacheServiceProvider.putVfModule(vnfId, vfModuleId, vfModule);
253 return ResponseEntity.accepted().build();
256 @PostMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
257 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
258 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
259 public ResponseEntity<?> patchVfModule(@RequestBody final VfModule vfModule,
260 @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
261 @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
262 final HttpServletRequest request) {
264 LOGGER.info("Will post VfModule to cache with 'vf-module-id': {} and '{}': {} ...", vfModuleId, X_HTTP_METHOD_OVERRIDE,
265 xHttpHeaderOverride);
267 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
268 if (cacheServiceProvider.patchVfModule(vnfId, vfModuleId, vfModule)) {
269 return ResponseEntity.accepted().build();
271 LOGGER.error("Unable to apply patch to VmModule using 'vf-module-id': {} ... ", vfModule);
272 return getRequestErrorResponseEntity(request, VF_MODULE);
274 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
276 return getRequestErrorResponseEntity(request, VF_MODULE);