Adding aai line of business endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / LinesOfBusinessController.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.LINES_OF_BUSINESS_URL;
23 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
24 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
25 import java.util.Optional;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.aai.domain.yang.LineOfBusiness;
29 import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.web.bind.annotation.GetMapping;
36 import org.springframework.web.bind.annotation.PathVariable;
37 import org.springframework.web.bind.annotation.PutMapping;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestMapping;
40
41 /**
42  * @author Waqas Ikram (waqas.ikram@est.tech)
43  *
44  */
45 @Controller
46 @RequestMapping(path = LINES_OF_BUSINESS_URL)
47 public class LinesOfBusinessController {
48     private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class);
49
50     private final LinesOfBusinessCacheServiceProvider cacheServiceProvider;
51
52     @Autowired
53     public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) {
54         this.cacheServiceProvider = cacheServiceProvider;
55     }
56
57     @PutMapping(value = "{line-of-business-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
58             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
59     public ResponseEntity<?> putLineOfBusiness(@RequestBody final LineOfBusiness lineOfBusiness,
60             @PathVariable("line-of-business-name") final String lineOfBusinessName, final HttpServletRequest request) {
61
62         LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...",
63                 lineOfBusiness.getLineOfBusinessName());
64
65         if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) {
66             lineOfBusiness.setResourceVersion(getResourceVersion());
67
68         }
69         cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness);
70         return ResponseEntity.accepted().build();
71     }
72
73
74     @GetMapping(value = "/{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
75     public ResponseEntity<?> getLineOfBusiness(@PathVariable("line-of-business-name") final String lineOfBusinessName,
76             final HttpServletRequest request) {
77         LOGGER.info("retrieving Platform for 'platform-name': {} ...", lineOfBusinessName);
78         final Optional<LineOfBusiness> optional = cacheServiceProvider.getLineOfBusiness(lineOfBusinessName);
79         if (optional.isPresent()) {
80             final LineOfBusiness platform = optional.get();
81             LOGGER.info("found LineOfBusiness {} in cache", platform);
82             return ResponseEntity.ok(platform);
83         }
84         LOGGER.error("Unable to find LineOfBusiness in cahce using {}", lineOfBusinessName);
85         return getRequestErrorResponseEntity(request);
86     }
87
88 }