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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.controller;
23 import org.onap.aai.domain.yang.v15.Pnf;
24 import org.onap.aai.domain.yang.v15.Pnfs;
25 import org.onap.so.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;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.ws.rs.core.MediaType;
44 import java.util.List;
45 import java.util.Optional;
47 import static org.onap.so.aaisimulator.utils.Constants.APPLICATION_MERGE_PATCH_JSON;
48 import static org.onap.so.aaisimulator.utils.Constants.PNF;
49 import static org.onap.so.aaisimulator.utils.Constants.PNFS_URL;
50 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
51 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
52 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
55 * @author Raj Gumma (raj.gumma@est.tech)
58 @RequestMapping(path = PNFS_URL)
59 public class PnfsController {
61 private static final Logger LOGGER = LoggerFactory.getLogger(PnfsController.class);
63 private final PnfCacheServiceProvider cacheServiceProvider;
67 public PnfsController(final PnfCacheServiceProvider cacheServiceProvider) {
68 this.cacheServiceProvider = cacheServiceProvider;
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);
77 if (pnf.getResourceVersion() == null || pnf.getResourceVersion().isEmpty()) {
78 pnf.setResourceVersion(getResourceVersion());
80 cacheServiceProvider.putPnf(pnfId, pnf);
81 return ResponseEntity.accepted().build();
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);
88 final Optional<Pnf> optional = cacheServiceProvider.getPnf(pnfId);
90 if (optional.isPresent()) {
91 final Pnf pnf = optional.get();
92 LOGGER.info("found Pnf {} in cache", pnf);
93 return ResponseEntity.ok(pnf);
96 LOGGER.error("Unable to find Pnf in cache for 'pnf-id': {}", pnfId);
97 return getRequestErrorResponseEntity(request, "pnf");
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) {
109 LOGGER.info("Will post Pnf to cache with 'pnf-id': {} and '{}': {} ...", pnfId, X_HTTP_METHOD_OVERRIDE,
110 xHttpHeaderOverride);
112 if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
113 if (cacheServiceProvider.patchPnf(pnfId, pnf)) {
114 return ResponseEntity.accepted().build();
116 LOGGER.error("Unable to apply patch to Pnf using 'pnf-id': {} ... ", pnfId);
117 return getRequestErrorResponseEntity(request, PNF);
119 LOGGER.error("{} not supported ... ", xHttpHeaderOverride);
121 return getRequestErrorResponseEntity(request, PNF);
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);
129 final List<Pnf> pnfList = cacheServiceProvider.getPnfs(selflink);
131 if (pnfList.isEmpty()) {
132 LOGGER.error("No matching pnfs found using selflink: {}", selflink);
133 return getRequestErrorResponseEntity(request, PNF);
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);
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);
147 if (cacheServiceProvider.deletePnf(pnfId, resourceVersion)) {
148 LOGGER.info("Successfully delete Pnf from cache for 'pnf-id': {} and 'resource-version': {}", pnfId,
150 return ResponseEntity.noContent().build();
153 LOGGER.error("Unable to delete Pnf for 'pnf-id': {} and 'resource-version': {} ...", pnfId,
155 return getRequestErrorResponseEntity(request, PNF);