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
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;
22 import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO;
23 import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST;
24 import static org.onap.so.aaisimulator.utils.Constants.ESR_VNFM;
25 import static org.onap.so.aaisimulator.utils.Constants.EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL;
26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
27 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
28 import java.util.List;
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.EsrSystemInfo;
33 import org.onap.aai.domain.yang.EsrSystemInfoList;
34 import org.onap.aai.domain.yang.EsrVnfm;
35 import org.onap.aai.domain.yang.EsrVnfmList;
36 import org.onap.so.aaisimulator.service.providers.ExternalSystemCacheServiceProvider;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PutMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
49 * @author Waqas Ikram (waqas.ikram@est.tech)
53 @RequestMapping(path = EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL)
54 public class ExternalSystemEsrController {
55 private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemEsrController.class);
57 private final ExternalSystemCacheServiceProvider cacheServiceProvider;
60 public ExternalSystemEsrController(final ExternalSystemCacheServiceProvider cacheServiceProvider) {
61 this.cacheServiceProvider = cacheServiceProvider;
64 @PutMapping(value = "/esr-vnfm/{vnfm-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
65 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
66 public ResponseEntity<?> putEsrVnfm(@RequestBody final EsrVnfm esrVnfm,
67 @PathVariable("vnfm-id") final String vnfmId, final HttpServletRequest request) {
68 LOGGER.info("Will put esr-vnfm to cache for 'vnfm id': {} ...", esrVnfm.getVnfmId());
70 if (esrVnfm.getResourceVersion() == null || esrVnfm.getResourceVersion().isEmpty()) {
71 esrVnfm.setResourceVersion(getResourceVersion());
74 cacheServiceProvider.putEsrVnfm(vnfmId, esrVnfm);
75 return ResponseEntity.accepted().build();
78 @GetMapping(value = "/esr-vnfm/{vnfm-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
79 public ResponseEntity<?> getEsrVnfm(@PathVariable("vnfm-id") final String vnfmId,
80 final HttpServletRequest request) {
81 LOGGER.info("Will retrieve ESR VNFM for 'vnfm id': {} ...", vnfmId);
83 final Optional<EsrVnfm> optional = cacheServiceProvider.getEsrVnfm(vnfmId);
84 if (optional.isPresent()) {
85 final EsrVnfm esrVnfm = optional.get();
86 LOGGER.info("found esrVnfm {} in cache", esrVnfm);
87 return ResponseEntity.ok(esrVnfm);
90 LOGGER.error("Couldn't Esr Vnfm for 'vnfm id': {} ...", vnfmId);
91 return getRequestErrorResponseEntity(request, ESR_VNFM);
94 @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
95 public ResponseEntity<?> getEsrVnfmList(final HttpServletRequest request) {
96 LOGGER.info("Will retrieve a list of all ESR VNFMs");
98 final List<EsrVnfm> esrVnfms = cacheServiceProvider.getAllEsrVnfm();
99 LOGGER.info("found {} Esr Vnfms in cache", esrVnfms.size());
101 final EsrVnfmList esrVnfmList = new EsrVnfmList();
102 esrVnfmList.getEsrVnfm().addAll(esrVnfms);
104 return ResponseEntity.ok(esrVnfmList);
107 @PutMapping(value = "/esr-vnfm/{vnfm-id}/esr-system-info-list/esr-system-info/{esr-system-info-id}",
108 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
109 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
110 public ResponseEntity<?> putEsrSystemInfo(@RequestBody final EsrSystemInfo esrSystemInfo,
111 @PathVariable("vnfm-id") final String vnfmId,
112 @PathVariable("esr-system-info-id") final String esrSystemInfoId, final HttpServletRequest request) {
113 LOGGER.info("Will put esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId, esrSystemInfo);
115 if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) {
116 esrSystemInfo.setResourceVersion(getResourceVersion());
120 if (cacheServiceProvider.putEsrSystemInfo(vnfmId, esrSystemInfoId, esrSystemInfo)) {
121 LOGGER.info("Successfully added EsrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId,
123 return ResponseEntity.accepted().build();
125 LOGGER.error("Unable to add esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId,
127 return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST);
130 @GetMapping(value = "/esr-vnfm/{vnfm-id}/esr-system-info-list",
131 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
132 public ResponseEntity<?> getEsrSystemInfoList(@PathVariable("vnfm-id") final String vnfmId,
133 final HttpServletRequest request) {
134 LOGGER.info("Will retrieve esrSystemInfoList for 'vnfm id': {} ...", vnfmId);
136 final Optional<EsrSystemInfoList> optional = cacheServiceProvider.getEsrSystemInfoList(vnfmId);
137 if (optional.isPresent()) {
138 final EsrSystemInfoList esrSystemInfoList = optional.get();
139 LOGGER.info("found esrSystemInfoList {} in cache", esrSystemInfoList);
140 return ResponseEntity.ok(esrSystemInfoList);
143 LOGGER.error("Couldn't find esrSystemInfoList for 'vnfm id': {} ...", vnfmId);
144 return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO);