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