43fe47da68f34351bedc82633cbec92c2cd8e166
[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.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.GENERIC_VNFS_URL;
26 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
28 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
29 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
30 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
31 import java.util.List;
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.GenericVnfs;
37 import org.onap.aai.domain.yang.Relationship;
38 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
39 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
40 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.stereotype.Controller;
48 import org.springframework.web.bind.annotation.DeleteMapping;
49 import org.springframework.web.bind.annotation.GetMapping;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.PostMapping;
52 import org.springframework.web.bind.annotation.PutMapping;
53 import org.springframework.web.bind.annotation.RequestBody;
54 import org.springframework.web.bind.annotation.RequestHeader;
55 import org.springframework.web.bind.annotation.RequestMapping;
56 import org.springframework.web.bind.annotation.RequestParam;
57
58 /**
59  * @author Waqas Ikram (waqas.ikram@est.tech)
60  *
61  */
62 @Controller
63 @RequestMapping(path = GENERIC_VNFS_URL)
64 public class GenericVnfsController {
65
66     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
67
68     private final GenericVnfCacheServiceProvider cacheServiceProvider;
69
70
71     @Autowired
72     public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) {
73         this.cacheServiceProvider = cacheServiceProvider;
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,
94             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
95             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
96             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
97         LOGGER.info(
98                 "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
99                 vnfId, depth, resultIndex, resultSize, format);
100
101         final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
102
103         if (optional.isPresent()) {
104             final GenericVnf genericVnf = optional.get();
105             LOGGER.info("found GenericVnf {} in cache", genericVnf);
106             return ResponseEntity.ok(genericVnf);
107         }
108
109         LOGGER.error(
110                 "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
111                 vnfId, depth, resultIndex, resultSize, format);
112         return getRequestErrorResponseEntity(request, GENERIC_VNF);
113
114     }
115
116     @PutMapping(value = "/generic-vnf/{vnf-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
117             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
118             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
119     public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
120             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
121         LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
122
123         if (relationship.getRelatedLink() != null) {
124             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
125             final HttpHeaders incomingHeader = getHeaders(request);
126             final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
127                     request.getRequestURI(), vnfId, relationship);
128             if (result) {
129                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
130                 return ResponseEntity.accepted().build();
131             }
132         }
133         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
134         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
135     }
136
137     @PutMapping(value = "/generic-vnf/{vnf-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
138             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
139             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
140     public ResponseEntity<?> putBiDirectionalRelationShip(@RequestBody final Relationship relationship,
141             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
142         LOGGER.info("Will put RelationShip for 'vnf-id': {} ...", vnfId);
143
144         final Optional<Relationship> optional =
145                 cacheServiceProvider.addRelationShip(vnfId, relationship, request.getRequestURI());
146
147         if (optional.isPresent()) {
148             final Relationship resultantRelationship = optional.get();
149             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
150             return ResponseEntity.accepted().body(resultantRelationship);
151         }
152
153         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
154         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
155     }
156
157     @PostMapping(value = "/generic-vnf/{vnf-id}",
158             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
159             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
160     public ResponseEntity<?> patchGenericVnf(@RequestBody final GenericVnf genericVnf,
161             @PathVariable("vnf-id") final String vnfId,
162             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
163             final HttpServletRequest request) {
164
165         LOGGER.info("Will post GenericVnf to cache with 'vnf-id': {} and '{}': {} ...", vnfId, X_HTTP_METHOD_OVERRIDE,
166                 xHttpHeaderOverride);
167
168         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
169             if (cacheServiceProvider.patchGenericVnf(vnfId, genericVnf)) {
170                 return ResponseEntity.accepted().build();
171             }
172             LOGGER.error("Unable to apply patch to GenericVnf using 'vnf-id': {} ... ", vnfId);
173             return getRequestErrorResponseEntity(request, GENERIC_VNF);
174         }
175         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
176
177         return getRequestErrorResponseEntity(request, GENERIC_VNF);
178     }
179
180     @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
181     public ResponseEntity<?> getGenericVnfs(@RequestParam(name = "selflink") final String selflink,
182             final HttpServletRequest request) {
183         LOGGER.info("will retrieve GenericVnfs using selflink: {}", selflink);
184
185         final List<GenericVnf> genericVnfList = cacheServiceProvider.getGenericVnfs(selflink);
186
187         if (genericVnfList.isEmpty()) {
188             LOGGER.error("No matching generic vnfs found using selflink: {}", selflink);
189             return getRequestErrorResponseEntity(request, GENERIC_VNF);
190         }
191
192         LOGGER.info("found {} GenericVnfs in cache", genericVnfList.size());
193         final GenericVnfs genericVnfs = new GenericVnfs();
194         genericVnfs.getGenericVnf().addAll(genericVnfList);
195         return ResponseEntity.ok(genericVnfs);
196     }
197
198     @DeleteMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
199     public ResponseEntity<?> deleteGenericVnf(@PathVariable("vnf-id") final String vnfId,
200             @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
201         LOGGER.info("Will delete GenericVnf for 'vnf-id': {} and 'resource-version': {}", vnfId, resourceVersion);
202
203         if (cacheServiceProvider.deleteGenericVnf(vnfId, resourceVersion)) {
204             LOGGER.info("Successfully delete GenericVnf from cache for 'vnf-id': {} and 'resource-version': {}", vnfId,
205                     resourceVersion);
206             return ResponseEntity.noContent().build();
207         }
208
209         LOGGER.error("Unable to delete GenericVnf for 'vnf-id': {} and 'resource-version': {} ...", vnfId,
210                 resourceVersion);
211         return getRequestErrorResponseEntity(request, GENERIC_VNF);
212
213     }
214
215 }