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.COMPOSED_OF;
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.GENERIC_VNF_VNF_ID;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
27 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
28 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBaseUrl;
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.Optional;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.core.MediaType;
35 import org.onap.aai.domain.yang.GenericVnf;
36 import org.onap.aai.domain.yang.RelatedToProperty;
37 import org.onap.aai.domain.yang.Relationship;
38 import org.onap.aai.domain.yang.RelationshipData;
39 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
40 import org.onap.so.aaisimulator.service.providers.HttpRestServiceProvider;
41 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.stereotype.Controller;
47 import org.springframework.web.bind.annotation.GetMapping;
48 import org.springframework.web.bind.annotation.PathVariable;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RequestParam;
53 import org.springframework.web.util.UriComponentsBuilder;
56 * @author Waqas Ikram (waqas.ikram@est.tech)
60 @RequestMapping(path = GENERIC_VNFS_URL)
61 public class GenericVnfsController {
63 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
65 private final GenericVnfCacheServiceProvider cacheServiceProvider;
67 private final HttpRestServiceProvider httpRestServiceProvider;
70 public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider,
71 final HttpRestServiceProvider httpRestServiceProvider) {
72 this.cacheServiceProvider = cacheServiceProvider;
73 this.httpRestServiceProvider = httpRestServiceProvider;
76 @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
77 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
78 public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
79 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
80 LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
82 if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
83 genericVnf.setResourceVersion(getResourceVersion());
86 cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
87 return ResponseEntity.accepted().build();
91 @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
92 public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
93 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
94 LOGGER.info("Will get GenericVnf for 'vnf-id': {} with depth: {}...", vnfId, depth);
96 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
98 if (optional.isPresent()) {
99 final GenericVnf genericVnf = optional.get();
100 LOGGER.info("found GenericVnf {} in cache", genericVnf);
101 return ResponseEntity.ok(genericVnf);
104 LOGGER.error("Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {} ...", vnfId, depth);
105 return getRequestErrorResponseEntity(request, GENERIC_VNF);
109 @PutMapping(value = "/generic-vnf/{vnf-id}/relationship-list/relationship",
110 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
111 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
112 public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
113 @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
114 LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
117 if (relationship.getRelatedLink() != null) {
118 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
120 if (optional.isPresent()) {
121 final GenericVnf genericVnf = optional.get();
122 final String url = getRelationShipUrl(request, relationship.getRelatedLink());
124 final Relationship serviceRelationship = getRelationship(request.getRequestURI(), genericVnf);
125 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(getHeaders(request),
126 serviceRelationship, url, Relationship.class);
128 if (optionalRelationship.isPresent()) {
129 final Relationship resultantRelationship = optionalRelationship.get();
130 final boolean result = cacheServiceProvider.addRelationShip(vnfId, resultantRelationship);
132 LOGGER.info("added relationship {} in cache successfully", relationship);
133 return ResponseEntity.accepted().build();
135 LOGGER.error("Unable to add relationship {} in cache", relationship);
139 } catch (final Exception exception) {
140 LOGGER.error("Unable to add two-way relationship ", exception);
143 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
144 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
148 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
149 final Relationship relationShip = new Relationship();
150 relationShip.setRelatedTo(GENERIC_VNF);
151 relationShip.setRelationshipLabel(COMPOSED_OF);
152 relationShip.setRelatedLink(relatedLink);
154 final RelationshipData relationshipData = new RelationshipData();
155 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
156 relationshipData.setRelationshipValue(genericVnf.getVnfId());
157 relationShip.getRelationshipData().add(relationshipData);
159 final RelatedToProperty relatedToProperty = new RelatedToProperty();
160 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
161 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
162 relationShip.getRelatedToProperty().add(relatedToProperty);
166 private String getRelationShipUrl(final HttpServletRequest request, final String relatedLink) {
167 return UriComponentsBuilder.fromUri(getBaseUrl(request)).path(relatedLink)
168 .path(RELATIONSHIP_LIST_RELATIONSHIP_URL).toUriString();