Https to Http from vnfm-adapter to simulator
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / main / java / org / onap / aaisimulator / controller / PnfsController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.aaisimulator.controller;
21
22
23 import org.onap.aai.domain.yang.v15.Pnf;
24 import org.onap.aai.domain.yang.v15.Pnfs;
25 import org.onap.aaisimulator.service.providers.PnfCacheServiceProvider;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.http.HttpMethod;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Controller;
32 import org.springframework.web.bind.annotation.DeleteMapping;
33 import org.springframework.web.bind.annotation.GetMapping;
34 import org.springframework.web.bind.annotation.PathVariable;
35 import org.springframework.web.bind.annotation.PostMapping;
36 import org.springframework.web.bind.annotation.PutMapping;
37 import org.springframework.web.bind.annotation.RequestBody;
38 import org.springframework.web.bind.annotation.RequestHeader;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestParam;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.ws.rs.core.MediaType;
44 import java.util.List;
45 import java.util.Optional;
46
47 import static org.onap.aaisimulator.utils.Constants.APPLICATION_MERGE_PATCH_JSON;
48 import static org.onap.aaisimulator.utils.Constants.PNF;
49 import static org.onap.aaisimulator.utils.Constants.PNFS_URL;
50 import static org.onap.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
51 import static org.onap.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
52 import static org.onap.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
53
54 /**
55  * @author Raj Gumma (raj.gumma@est.tech)
56  */
57 @Controller
58 @RequestMapping(path = PNFS_URL)
59 public class PnfsController {
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(PnfsController.class);
62
63     private final PnfCacheServiceProvider cacheServiceProvider;
64
65
66     @Autowired
67     public PnfsController(final PnfCacheServiceProvider cacheServiceProvider) {
68         this.cacheServiceProvider = cacheServiceProvider;
69     }
70
71     @PutMapping(value = "/pnf/{pnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
72             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
73     public ResponseEntity<?> putPnf(@RequestBody final Pnf pnf,
74                                     @PathVariable("pnf-id") final String pnfId, final HttpServletRequest request) {
75         LOGGER.info("Will add Pnf to cache with 'pnf-id': {} ...", pnfId);
76
77         if (pnf.getResourceVersion() == null || pnf.getResourceVersion().isEmpty()) {
78             pnf.setResourceVersion(getResourceVersion());
79         }
80         cacheServiceProvider.putPnf(pnfId, pnf);
81         return ResponseEntity.accepted().build();
82     }
83
84     @GetMapping(value = "/pnf/{pnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
85     public ResponseEntity<?> getPnf(@PathVariable("pnf-id") final String pnfId, final HttpServletRequest request) {
86         LOGGER.info("Will get Pnf for 'pnf-id': {} ", pnfId);
87
88         final Optional<Pnf> optional = cacheServiceProvider.getPnf(pnfId);
89
90         if (optional.isPresent()) {
91             final Pnf pnf = optional.get();
92             LOGGER.info("found Pnf {} in cache", pnf);
93             return ResponseEntity.ok(pnf);
94         }
95
96         LOGGER.error("Unable to find Pnf in cache for 'pnf-id': {}", pnfId);
97         return getRequestErrorResponseEntity(request, "pnf");
98
99     }
100
101     @PostMapping(value = "/pnf/{pnf-id}",
102             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
103             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
104     public ResponseEntity<?> patchPnf(@RequestBody final Pnf pnf,
105                                       @PathVariable("pnf-id") final String pnfId,
106                                       @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
107                                       final HttpServletRequest request) {
108
109         LOGGER.info("Will post Pnf to cache with 'pnf-id': {} and '{}': {} ...", pnfId, X_HTTP_METHOD_OVERRIDE,
110                 xHttpHeaderOverride);
111
112         if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
113             if (cacheServiceProvider.patchPnf(pnfId, pnf)) {
114                 return ResponseEntity.accepted().build();
115             }
116             LOGGER.error("Unable to apply patch to Pnf using 'pnf-id': {} ... ", pnfId);
117             return getRequestErrorResponseEntity(request, PNF);
118         }
119         LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
120
121         return getRequestErrorResponseEntity(request, PNF);
122     }
123
124     @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
125     public ResponseEntity<?> getPnfs(@RequestParam(name = "selflink") final String selflink,
126                                      final HttpServletRequest request) {
127         LOGGER.info("will retrieve Pnfs using selflink: {}", selflink);
128
129         final List<Pnf> pnfList = cacheServiceProvider.getPnfs(selflink);
130
131         if (pnfList.isEmpty()) {
132             LOGGER.error("No matching pnfs found using selflink: {}", selflink);
133             return getRequestErrorResponseEntity(request, PNF);
134         }
135
136         LOGGER.info("found {} Pnfs in cache", pnfList.size());
137         final Pnfs pnfs = new Pnfs();
138         pnfs.getPnf().addAll(pnfList);
139         return ResponseEntity.ok(pnfs);
140     }
141
142     @DeleteMapping(value = "/pnf/{pnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
143     public ResponseEntity<?> deletePnf(@PathVariable("pnf-id") final String pnfId,
144                                        @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
145         LOGGER.info("Will delete Pnf for 'pnf-id': {} and 'resource-version': {}", pnfId, resourceVersion);
146
147         if (cacheServiceProvider.deletePnf(pnfId, resourceVersion)) {
148             LOGGER.info("Successfully delete Pnf from cache for 'pnf-id': {} and 'resource-version': {}", pnfId,
149                     resourceVersion);
150             return ResponseEntity.noContent().build();
151         }
152
153         LOGGER.error("Unable to delete Pnf for 'pnf-id': {} and 'resource-version': {} ...", pnfId,
154                 resourceVersion);
155         return getRequestErrorResponseEntity(request, PNF);
156
157     }
158
159 }