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