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