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