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.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.so.aaisimulator.utils.Constants.LINES_OF_BUSINESS_URL;
24 import static org.onap.so.aaisimulator.utils.Constants.LINE_OF_BUSINESS;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
27 import java.util.HashMap;
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.LineOfBusiness;
33 import org.onap.aai.domain.yang.Relationship;
34 import org.onap.so.aaisimulator.models.Format;
35 import org.onap.so.aaisimulator.models.Results;
36 import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
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 import org.springframework.web.bind.annotation.RequestParam;
50 * @author Waqas Ikram (waqas.ikram@est.tech)
54 @RequestMapping(path = LINES_OF_BUSINESS_URL)
55 public class LinesOfBusinessController {
56 private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class);
58 private final LinesOfBusinessCacheServiceProvider cacheServiceProvider;
61 public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) {
62 this.cacheServiceProvider = cacheServiceProvider;
65 @PutMapping(value = "{line-of-business-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
66 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
67 public ResponseEntity<?> putLineOfBusiness(@RequestBody final LineOfBusiness lineOfBusiness,
68 @PathVariable("line-of-business-name") final String lineOfBusinessName, final HttpServletRequest request) {
70 LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...",
71 lineOfBusiness.getLineOfBusinessName());
73 if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) {
74 lineOfBusiness.setResourceVersion(getResourceVersion());
77 cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness);
78 return ResponseEntity.accepted().build();
82 @GetMapping(value = "{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
83 public ResponseEntity<?> getLineOfBusiness(@PathVariable("line-of-business-name") final String lineOfBusinessName,
84 @RequestParam(name = "depth", required = false) final Integer depth,
85 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
86 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
87 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
90 "retrieving Platform for 'platform-name': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
91 lineOfBusinessName, depth, resultIndex, resultSize, format);
93 final Optional<LineOfBusiness> optional = cacheServiceProvider.getLineOfBusiness(lineOfBusinessName);
94 if (optional.isPresent()) {
96 final Format value = Format.forValue(format);
99 final LineOfBusiness platform = optional.get();
100 LOGGER.info("found LineOfBusiness {} in cache", platform);
101 return ResponseEntity.ok(platform);
103 final Map<String, Object> map = new HashMap<>();
104 map.put(LINE_OF_BUSINESS, 1);
105 return ResponseEntity.ok(new Results(map));
109 LOGGER.error("invalid format type :{}", format);
111 LOGGER.error("Unable to find LineOfBusiness in cache using {}", lineOfBusinessName);
112 return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);
115 @PutMapping(value = "/{line-of-business-name}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
116 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
117 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
118 public ResponseEntity<?> putRelationShip(@PathVariable("line-of-business-name") final String lineOfBusinessName,
119 @RequestBody final Relationship relationship, final HttpServletRequest request) {
120 LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
122 final Optional<Relationship> optional =
123 cacheServiceProvider.addRelationShip(lineOfBusinessName, relationship, request.getRequestURI());
125 if (optional.isPresent()) {
126 final Relationship resultantRelationship = optional.get();
127 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
128 return ResponseEntity.accepted().body(resultantRelationship);
131 LOGGER.error("Couldn't add {} relationship for 'line-of-business-name': {} ...", relationship.getRelatedTo(),
134 return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);