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.Constants.LINE_OF_BUSINESS;
24 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
26 import java.util.Optional;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.MediaType;
29 import org.onap.aai.domain.yang.LineOfBusiness;
30 import org.onap.aai.domain.yang.Relationship;
31 import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.PutMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
44 * @author Waqas Ikram (waqas.ikram@est.tech)
48 @RequestMapping(path = LINES_OF_BUSINESS_URL)
49 public class LinesOfBusinessController {
50 private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class);
52 private final LinesOfBusinessCacheServiceProvider cacheServiceProvider;
55 public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) {
56 this.cacheServiceProvider = cacheServiceProvider;
59 @PutMapping(value = "{line-of-business-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
60 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
61 public ResponseEntity<?> putLineOfBusiness(@RequestBody final LineOfBusiness lineOfBusiness,
62 @PathVariable("line-of-business-name") final String lineOfBusinessName, final HttpServletRequest request) {
64 LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...",
65 lineOfBusiness.getLineOfBusinessName());
67 if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) {
68 lineOfBusiness.setResourceVersion(getResourceVersion());
71 cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness);
72 return ResponseEntity.accepted().build();
76 @GetMapping(value = "{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
77 public ResponseEntity<?> getLineOfBusiness(@PathVariable("line-of-business-name") final String lineOfBusinessName,
78 final HttpServletRequest request) {
79 LOGGER.info("retrieving Platform for 'platform-name': {} ...", lineOfBusinessName);
80 final Optional<LineOfBusiness> optional = cacheServiceProvider.getLineOfBusiness(lineOfBusinessName);
81 if (optional.isPresent()) {
82 final LineOfBusiness platform = optional.get();
83 LOGGER.info("found LineOfBusiness {} in cache", platform);
84 return ResponseEntity.ok(platform);
86 LOGGER.error("Unable to find LineOfBusiness in cache using {}", lineOfBusinessName);
87 return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);
90 @PutMapping(value = "/{line-of-business-name}/relationship-list/relationship",
91 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
92 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
93 public ResponseEntity<?> putRelationShip(@PathVariable("line-of-business-name") final String lineOfBusinessName,
94 @RequestBody final Relationship relationship, final HttpServletRequest request) {
95 LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
97 final Optional<Relationship> optional =
98 cacheServiceProvider.addRelationShip(lineOfBusinessName, relationship, request.getRequestURI());
100 if (optional.isPresent()) {
101 final Relationship resultantRelationship = optional.get();
102 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
103 return ResponseEntity.accepted().body(resultantRelationship);
106 LOGGER.error("Couldn't add {} relationship for 'line-of-business-name': {} ...", relationship.getRelatedTo(),
109 return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);