2eb84f05a16e4ca1619617a7244aa165cedd43e5
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / GenericVnfsController.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.controller;
21
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;
54
55 /**
56  * @author Waqas Ikram (waqas.ikram@est.tech)
57  *
58  */
59 @Controller
60 @RequestMapping(path = GENERIC_VNFS_URL)
61 public class GenericVnfsController {
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
64
65     private final GenericVnfCacheServiceProvider cacheServiceProvider;
66
67     private final HttpRestServiceProvider httpRestServiceProvider;
68
69     @Autowired
70     public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider,
71             final HttpRestServiceProvider httpRestServiceProvider) {
72         this.cacheServiceProvider = cacheServiceProvider;
73         this.httpRestServiceProvider = httpRestServiceProvider;
74     }
75
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);
81
82         if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
83             genericVnf.setResourceVersion(getResourceVersion());
84
85         }
86         cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
87         return ResponseEntity.accepted().build();
88
89     }
90
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);
95
96         final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
97
98         if (optional.isPresent()) {
99             final GenericVnf genericVnf = optional.get();
100             LOGGER.info("found GenericVnf {} in cache", genericVnf);
101             return ResponseEntity.ok(genericVnf);
102         }
103
104         LOGGER.error("Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {} ...", vnfId, depth);
105         return getRequestErrorResponseEntity(request, GENERIC_VNF);
106
107     }
108
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);
115
116         try {
117             if (relationship.getRelatedLink() != null) {
118                 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
119
120                 if (optional.isPresent()) {
121                     final GenericVnf genericVnf = optional.get();
122                     final String url = getRelationShipUrl(request, relationship.getRelatedLink());
123
124                     final Relationship serviceRelationship = getRelationship(request.getRequestURI(), genericVnf);
125                     final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(getHeaders(request),
126                             serviceRelationship, url, Relationship.class);
127
128                     if (optionalRelationship.isPresent()) {
129                         final Relationship resultantRelationship = optionalRelationship.get();
130                         final boolean result = cacheServiceProvider.addRelationShip(vnfId, resultantRelationship);
131                         if (result) {
132                             LOGGER.info("added relationship {} in cache successfully", relationship);
133                             return ResponseEntity.accepted().build();
134                         }
135                         LOGGER.error("Unable to add relationship {} in cache", relationship);
136                     }
137                 }
138             }
139         } catch (final Exception exception) {
140             LOGGER.error("Unable to add two-way relationship ", exception);
141         }
142
143         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
144         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
145
146     }
147
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);
153
154         final RelationshipData relationshipData = new RelationshipData();
155         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
156         relationshipData.setRelationshipValue(genericVnf.getVnfId());
157         relationShip.getRelationshipData().add(relationshipData);
158
159         final RelatedToProperty relatedToProperty = new RelatedToProperty();
160         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
161         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
162         relationShip.getRelatedToProperty().add(relatedToProperty);
163         return relationShip;
164     }
165
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();
169     }
170
171 }