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.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;
42 * @author Waqas Ikram (waqas.ikram@est.tech)
46 @RequestMapping(path = LINES_OF_BUSINESS_URL)
47 public class LinesOfBusinessController {
48 private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class);
50 private final LinesOfBusinessCacheServiceProvider cacheServiceProvider;
53 public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) {
54 this.cacheServiceProvider = cacheServiceProvider;
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) {
62 LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...",
63 lineOfBusiness.getLineOfBusinessName());
65 if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) {
66 lineOfBusiness.setResourceVersion(getResourceVersion());
69 cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness);
70 return ResponseEntity.accepted().build();
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);
84 LOGGER.error("Unable to find LineOfBusiness in cahce using {}", lineOfBusinessName);
85 return getRequestErrorResponseEntity(request);