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.GENERIC_VNF;
23 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNFS_URL;
24 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
27 import java.util.Optional;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.MediaType;
30 import org.onap.aai.domain.yang.GenericVnf;
31 import org.onap.aai.domain.yang.Relationship;
32 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
33 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
34 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
49 * @author Waqas Ikram (waqas.ikram@est.tech)
53 @RequestMapping(path = GENERIC_VNFS_URL)
54 public class GenericVnfsController {
56 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
58 private final GenericVnfCacheServiceProvider cacheServiceProvider;
62 public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) {
63 this.cacheServiceProvider = cacheServiceProvider;
66 @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
67 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
68 public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
69 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
70 LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
72 if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
73 genericVnf.setResourceVersion(getResourceVersion());
76 cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
77 return ResponseEntity.accepted().build();
81 @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
82 public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
83 @RequestParam(name = "depth", required = false) final Integer depth,
84 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
85 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
86 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
88 "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
89 vnfId, depth, resultIndex, resultSize, format);
91 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
93 if (optional.isPresent()) {
94 final GenericVnf genericVnf = optional.get();
95 LOGGER.info("found GenericVnf {} in cache", genericVnf);
96 return ResponseEntity.ok(genericVnf);
100 "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
101 vnfId, depth, resultIndex, resultSize, format);
102 return getRequestErrorResponseEntity(request, GENERIC_VNF);
106 @PutMapping(value = "/generic-vnf/{vnf-id}/relationship-list/relationship",
107 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
108 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
109 public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
110 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
111 LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
113 if (relationship.getRelatedLink() != null) {
114 final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
115 final HttpHeaders incomingHeader = getHeaders(request);
116 boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
117 request.getRequestURI(), vnfId, relationship);
119 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
120 return ResponseEntity.accepted().build();
123 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
124 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);