Adding AAI ESR endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / ExternalSystemEsrController.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.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;
47
48 /**
49  * @author Waqas Ikram (waqas.ikram@est.tech)
50  *
51  */
52 @Controller
53 @RequestMapping(path = EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL)
54 public class ExternalSystemEsrController {
55     private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemEsrController.class);
56
57     private final ExternalSystemCacheServiceProvider cacheServiceProvider;
58
59     @Autowired
60     public ExternalSystemEsrController(final ExternalSystemCacheServiceProvider cacheServiceProvider) {
61         this.cacheServiceProvider = cacheServiceProvider;
62     }
63
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());
69
70         if (esrVnfm.getResourceVersion() == null || esrVnfm.getResourceVersion().isEmpty()) {
71             esrVnfm.setResourceVersion(getResourceVersion());
72
73         }
74         cacheServiceProvider.putEsrVnfm(vnfmId, esrVnfm);
75         return ResponseEntity.accepted().build();
76     }
77
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);
82
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);
88         }
89
90         LOGGER.error("Couldn't Esr Vnfm for 'vnfm id': {} ...", vnfmId);
91         return getRequestErrorResponseEntity(request, ESR_VNFM);
92     }
93
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");
97
98         final List<EsrVnfm> esrVnfms = cacheServiceProvider.getAllEsrVnfm();
99         LOGGER.info("found {} Esr Vnfms in cache", esrVnfms.size());
100
101         final EsrVnfmList esrVnfmList = new EsrVnfmList();
102         esrVnfmList.getEsrVnfm().addAll(esrVnfms);
103
104         return ResponseEntity.ok(esrVnfmList);
105     }
106
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);
114
115         if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) {
116             esrSystemInfo.setResourceVersion(getResourceVersion());
117
118         }
119
120         if (cacheServiceProvider.putEsrSystemInfo(vnfmId, esrSystemInfoId, esrSystemInfo)) {
121             LOGGER.info("Successfully added EsrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId,
122                     esrSystemInfo);
123             return ResponseEntity.accepted().build();
124         }
125         LOGGER.error("Unable to add esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId,
126                 esrSystemInfo);
127         return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST);
128     }
129
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);
135
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);
141         }
142
143         LOGGER.error("Couldn't find esrSystemInfoList for 'vnfm id': {} ...", vnfmId);
144         return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO);
145     }
146
147 }