c6ec3a5ed6eaa1f71cc0860159bd7d2770b38191
[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.util.UriComponentsBuilder;
53
54 /**
55  * @author Waqas Ikram (waqas.ikram@est.tech)
56  *
57  */
58 @Controller
59 @RequestMapping(path = GENERIC_VNFS_URL)
60 public class GenericVnfsController {
61
62     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
63
64     private final GenericVnfCacheServiceProvider cacheServiceProvider;
65
66     private final HttpRestServiceProvider httpRestServiceProvider;
67
68     @Autowired
69     public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider,
70             final HttpRestServiceProvider httpRestServiceProvider) {
71         this.cacheServiceProvider = cacheServiceProvider;
72         this.httpRestServiceProvider = httpRestServiceProvider;
73     }
74
75     @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
76             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
77     public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
78             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
79         LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
80
81         if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
82             genericVnf.setResourceVersion(getResourceVersion());
83
84         }
85         cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
86         return ResponseEntity.accepted().build();
87
88     }
89
90     @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
91     public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
92             final HttpServletRequest request) {
93         LOGGER.info("Will get GenericVnf for 'vnf-id': {} ...", vnfId);
94
95         final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
96
97         if (optional.isPresent()) {
98             final GenericVnf genericVnf = optional.get();
99             LOGGER.info("found GenericVnf {} in cache", genericVnf);
100             return ResponseEntity.ok(genericVnf);
101         }
102
103         LOGGER.error("Unable to find GenericVnf in cache for 'vnf-id': {} ...", vnfId);
104         return getRequestErrorResponseEntity(request, GENERIC_VNF);
105
106     }
107
108     @PutMapping(value = "/generic-vnf/{vnf-id}/relationship-list/relationship",
109             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
110             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
111     public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
112             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
113         LOGGER.info("Will put customer for 'global customer id': {} ...", vnfId);
114
115         try {
116             if (relationship.getRelatedLink() != null) {
117                 final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
118
119                 if (optional.isPresent()) {
120                     final GenericVnf genericVnf = optional.get();
121                     final String url = getRelationShipUrl(request, relationship.getRelatedLink());
122
123                     final Relationship serviceRelationship = getRelationship(request.getRequestURI(), genericVnf);
124                     final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(getHeaders(request),
125                             serviceRelationship, url, Relationship.class);
126
127                     if (optionalRelationship.isPresent()) {
128                         final Relationship resultantRelationship = optionalRelationship.get();
129                         final boolean result = cacheServiceProvider.addRelationShip(vnfId, resultantRelationship);
130                         if (result) {
131                             LOGGER.info("added relationship {} in cache successfully", relationship);
132                             return ResponseEntity.accepted().build();
133                         }
134                         LOGGER.error("Unable to add relationship {} in cache", relationship);
135                     }
136                 }
137             }
138         } catch (final Exception exception) {
139             LOGGER.error("Unable to add two-way relationship ", exception);
140         }
141
142         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
143         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
144
145     }
146
147     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
148         final Relationship relationShip = new Relationship();
149         relationShip.setRelatedTo(GENERIC_VNF);
150         relationShip.setRelationshipLabel(COMPOSED_OF);
151         relationShip.setRelatedLink(relatedLink);
152
153         final RelationshipData relationshipData = new RelationshipData();
154         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
155         relationshipData.setRelationshipValue(genericVnf.getVnfId());
156         relationShip.getRelationshipData().add(relationshipData);
157
158         final RelatedToProperty relatedToProperty = new RelatedToProperty();
159         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
160         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
161         relationShip.getRelatedToProperty().add(relatedToProperty);
162         return relationShip;
163     }
164
165     private String getRelationShipUrl(final HttpServletRequest request, final String relatedLink) {
166         return UriComponentsBuilder.fromUri(getBaseUrl(request)).path(relatedLink)
167                 .path(RELATIONSHIP_LIST_RELATIONSHIP_URL).toUriString();
168     }
169
170 }