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