ac1ad8c559245c828a9e0ef9fcb5cf5bc547db63
[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.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;
42
43 /**
44  * @author Waqas Ikram (waqas.ikram@est.tech)
45  *
46  */
47 @Controller
48 @RequestMapping(path = LINES_OF_BUSINESS_URL)
49 public class LinesOfBusinessController {
50     private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class);
51
52     private final LinesOfBusinessCacheServiceProvider cacheServiceProvider;
53
54     @Autowired
55     public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) {
56         this.cacheServiceProvider = cacheServiceProvider;
57     }
58
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) {
63
64         LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...",
65                 lineOfBusiness.getLineOfBusinessName());
66
67         if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) {
68             lineOfBusiness.setResourceVersion(getResourceVersion());
69
70         }
71         cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness);
72         return ResponseEntity.accepted().build();
73     }
74
75
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);
85         }
86         LOGGER.error("Unable to find LineOfBusiness in cache using {}", lineOfBusinessName);
87         return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);
88     }
89
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());
96
97         final Optional<Relationship> optional =
98                 cacheServiceProvider.addRelationShip(lineOfBusinessName, relationship, request.getRequestURI());
99
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);
104         }
105
106         LOGGER.error("Couldn't add {} relationship for 'line-of-business-name': {} ...", relationship.getRelatedTo(),
107                 lineOfBusinessName);
108
109         return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);
110
111     }
112
113 }