[SO] SO release image for CSIT testing
[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.VF_MODULE;
28 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
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.List;
33 import java.util.Optional;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.core.MediaType;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.aai.domain.yang.GenericVnfs;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.VfModule;
40 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
41 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
42 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.HttpHeaders;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.DeleteMapping;
52 import org.springframework.web.bind.annotation.GetMapping;
53 import org.springframework.web.bind.annotation.PathVariable;
54 import org.springframework.web.bind.annotation.PostMapping;
55 import org.springframework.web.bind.annotation.PutMapping;
56 import org.springframework.web.bind.annotation.RequestBody;
57 import org.springframework.web.bind.annotation.RequestHeader;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestParam;
60
61 /**
62  * @author Waqas Ikram (waqas.ikram@est.tech)
63  *
64  */
65 @Controller
66 @RequestMapping(path = GENERIC_VNFS_URL)
67 public class GenericVnfsController {
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfsController.class);
70
71     private final GenericVnfCacheServiceProvider cacheServiceProvider;
72
73
74     @Autowired
75     public GenericVnfsController(final GenericVnfCacheServiceProvider cacheServiceProvider) {
76         this.cacheServiceProvider = cacheServiceProvider;
77     }
78
79     @PutMapping(value = "/generic-vnf/{vnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
80             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
81     public ResponseEntity<?> putGenericVnf(@RequestBody final GenericVnf genericVnf,
82             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
83         LOGGER.info("Will add GenericVnf to cache with 'vnf-id': {} ...", vnfId);
84
85         if (genericVnf.getResourceVersion() == null || genericVnf.getResourceVersion().isEmpty()) {
86             genericVnf.setResourceVersion(getResourceVersion());
87
88         }
89         cacheServiceProvider.putGenericVnf(vnfId, genericVnf);
90         return ResponseEntity.accepted().build();
91
92     }
93
94     @GetMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
95     public ResponseEntity<?> getGenericVnf(@PathVariable("vnf-id") final String vnfId,
96             @RequestParam(name = "depth", required = false) final Integer depth,
97             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
98             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
99             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
100         LOGGER.info(
101                 "Will get GenericVnf for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
102                 vnfId, depth, resultIndex, resultSize, format);
103
104         final Optional<GenericVnf> optional = cacheServiceProvider.getGenericVnf(vnfId);
105
106         if (optional.isPresent()) {
107             final GenericVnf genericVnf = optional.get();
108             LOGGER.info("found GenericVnf {} in cache", genericVnf);
109             return ResponseEntity.ok(genericVnf);
110         }
111
112         LOGGER.error(
113                 "Unable to find GenericVnf in cache for 'vnf-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
114                 vnfId, depth, resultIndex, resultSize, format);
115         return getRequestErrorResponseEntity(request, GENERIC_VNF);
116
117     }
118
119     @PutMapping(value = "/generic-vnf/{vnf-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
120             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
121             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
122     public ResponseEntity<?> putGenericVnfRelationShip(@RequestBody final Relationship relationship,
123             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
124         LOGGER.info("Will add {} relationship for for 'vnf-id': {}...", relationship.getRelatedLink(), vnfId);
125
126         if (relationship.getRelatedLink() != null) {
127             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
128             final HttpHeaders incomingHeader = getHeaders(request);
129             final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
130                     request.getRequestURI(), vnfId, relationship);
131             if (result) {
132                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
133                 return ResponseEntity.accepted().build();
134             }
135         }
136         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
137         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
138     }
139
140     @PutMapping(value = "/generic-vnf/{vnf-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
141             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
142             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
143     public ResponseEntity<?> putBiDirectionalRelationShip(@RequestBody final Relationship relationship,
144             @PathVariable("vnf-id") final String vnfId, final HttpServletRequest request) {
145         LOGGER.info("Will put bi-directional relationship for 'vnf-id': {} ...", vnfId);
146
147         final Optional<Relationship> optional =
148                 cacheServiceProvider.addRelationShip(vnfId, relationship, request.getRequestURI());
149
150         if (optional.isPresent()) {
151             final Relationship resultantRelationship = optional.get();
152             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
153             return ResponseEntity.accepted().body(resultantRelationship);
154         }
155
156         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
157         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
158     }
159
160     @PostMapping(value = "/generic-vnf/{vnf-id}",
161             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
162             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
163     public ResponseEntity<?> patchGenericVnf(@RequestBody final GenericVnf genericVnf,
164             @PathVariable("vnf-id") final String vnfId,
165             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
166             final HttpServletRequest request) {
167
168         LOGGER.info("Will post GenericVnf to cache with 'vnf-id': {} and '{}': {} ...", vnfId, X_HTTP_METHOD_OVERRIDE,
169                 xHttpHeaderOverride);
170
171         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
172             if (cacheServiceProvider.patchGenericVnf(vnfId, genericVnf)) {
173                 return ResponseEntity.accepted().build();
174             }
175             LOGGER.error("Unable to apply patch to GenericVnf using 'vnf-id': {} ... ", vnfId);
176             return getRequestErrorResponseEntity(request, GENERIC_VNF);
177         }
178         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
179
180         return getRequestErrorResponseEntity(request, GENERIC_VNF);
181     }
182
183     @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
184     public ResponseEntity<?> getAllGenericVnfs(final HttpServletRequest request) {
185         LOGGER.info("will retrieve all GenericVnfs");
186         final List<GenericVnf> genericVnfList = cacheServiceProvider.getGenericVnfs();
187
188         LOGGER.info("found {} GenericVnfs in cache", genericVnfList.size());
189         final GenericVnfs genericVnfs = new GenericVnfs();
190         genericVnfs.getGenericVnf().addAll(genericVnfList);
191         return ResponseEntity.ok(genericVnfs);
192     }
193
194     @DeleteMapping(value = "/generic-vnf/{vnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
195     public ResponseEntity<?> deleteGenericVnf(@PathVariable("vnf-id") final String vnfId,
196             @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
197         LOGGER.info("Will delete GenericVnf for 'vnf-id': {} and 'resource-version': {}", vnfId, resourceVersion);
198
199         if (cacheServiceProvider.deleteGenericVnf(vnfId, resourceVersion)) {
200             LOGGER.info("Successfully delete GenericVnf from cache for 'vnf-id': {} and 'resource-version': {}", vnfId,
201                     resourceVersion);
202             return ResponseEntity.noContent().build();
203         }
204
205         LOGGER.error("Unable to delete GenericVnf for 'vnf-id': {} and 'resource-version': {} ...", vnfId,
206                 resourceVersion);
207         return getRequestErrorResponseEntity(request, GENERIC_VNF);
208
209     }
210
211     @GetMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
212             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
213     public ResponseEntity<?> getVfModule(@PathVariable("vnf-id") final String vnfId,
214             @PathVariable("vf-module-id") final String vfModuleId,
215             @RequestParam(name = "depth", required = false) final Integer depth,
216             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
217             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
218             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
219         LOGGER.info(
220                 "Will get VfModule for 'vf-module-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
221                 vnfId, vfModuleId, depth, resultIndex, resultSize, format);
222
223         final Optional<VfModule> optional = cacheServiceProvider.getVfModule(vnfId, vfModuleId);
224
225         if (optional.isPresent()) {
226             final VfModule vfModule = optional.get();
227             LOGGER.info("found VfModule {} in cache", vfModule);
228             return ResponseEntity.ok(vfModule);
229         }
230
231         LOGGER.error(
232                 "Unable to find VfModule in cache for 'vf-module-id': {} with depth: {}, resultIndex: {}, resultSize:{}, format:{} ...",
233                 vnfId, vfModuleId, depth, resultIndex, resultSize, format);
234         return getRequestErrorResponseEntity(request, VF_MODULE);
235
236     }
237
238
239     @PutMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
240             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
241             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
242     public ResponseEntity<?> putVfModule(@RequestBody final VfModule vfModule,
243             @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
244             final HttpServletRequest request) {
245         LOGGER.info("Will add VfModule to cache with 'vf-module-id': {} ...", vfModuleId);
246         if (vfModule.getResourceVersion() == null || vfModule.getResourceVersion().isEmpty()) {
247             vfModule.setResourceVersion(getResourceVersion());
248         }
249         cacheServiceProvider.putVfModule(vnfId, vfModuleId, vfModule);
250         return ResponseEntity.accepted().build();
251     }
252
253     @PostMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
254             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
255             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
256     public ResponseEntity<?> patchVfModule(@RequestBody final VfModule vfModule,
257             @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
258             @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
259             final HttpServletRequest request) {
260
261         LOGGER.info("Will post VfModule to cache with 'vf-module-id': {} and '{}': {} ...", vfModuleId,
262                 X_HTTP_METHOD_OVERRIDE, xHttpHeaderOverride);
263
264         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
265             if (cacheServiceProvider.patchVfModule(vnfId, vfModuleId, vfModule)) {
266                 return ResponseEntity.accepted().build();
267             }
268             LOGGER.error("Unable to apply patch to VmModule using 'vf-module-id': {} ... ", vfModule);
269             return getRequestErrorResponseEntity(request, VF_MODULE);
270         }
271         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
272
273         return getRequestErrorResponseEntity(request, VF_MODULE);
274     }
275
276
277     @PutMapping(
278             value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
279             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
280             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
281     public ResponseEntity<?> putBiDirectionalVfModuleRelationShip(@RequestBody final Relationship relationship,
282             @PathVariable("vnf-id") final String vnfId, @PathVariable("vf-module-id") final String vfModuleId,
283             final HttpServletRequest request) {
284         LOGGER.info("Will add {} bi-directional relationship to : {} ...", relationship.getRelatedTo());
285
286         final Optional<Relationship> optional =
287                 cacheServiceProvider.addRelationShip(vnfId, vfModuleId, relationship, request.getRequestURI());
288
289         if (optional.isPresent()) {
290             final Relationship resultantRelationship = optional.get();
291             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
292             return ResponseEntity.accepted().body(resultantRelationship);
293         }
294
295         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
296         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, GENERIC_VNF);
297     }
298
299     @DeleteMapping(value = "/generic-vnf/{vnf-id}/vf-modules/vf-module/{vf-module-id}",
300         produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
301     public ResponseEntity<?> deleteVfModule(@PathVariable("vnf-id") final String vnfId,
302                                             @PathVariable("vf-module-id") final String vfModuleId,
303                                             @RequestParam(name = "resource-version") final String resourceVersion,
304                                             final HttpServletRequest request) {
305         LOGGER.info("Deleting VfModule from cache with 'vf-module-id': {} ...", vfModuleId);
306
307         boolean response = cacheServiceProvider.deleteVfModule(vnfId, vfModuleId, resourceVersion);
308         if(response){
309             return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
310         } else {
311             return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
312         }
313     }
314 }