Adding tenant relationship and generic vnf post 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.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;
28 import java.util.Map;
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;
48
49 /**
50  * @author Waqas Ikram (waqas.ikram@est.tech)
51  *
52  */
53 @Controller
54 @RequestMapping(path = LINES_OF_BUSINESS_URL)
55 public class LinesOfBusinessController {
56     private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class);
57
58     private final LinesOfBusinessCacheServiceProvider cacheServiceProvider;
59
60     @Autowired
61     public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) {
62         this.cacheServiceProvider = cacheServiceProvider;
63     }
64
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) {
69
70         LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...",
71                 lineOfBusiness.getLineOfBusinessName());
72
73         if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) {
74             lineOfBusiness.setResourceVersion(getResourceVersion());
75
76         }
77         cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness);
78         return ResponseEntity.accepted().build();
79     }
80
81
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) {
88
89         LOGGER.info(
90                 "retrieving Platform for 'platform-name': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
91                 lineOfBusinessName, depth, resultIndex, resultSize, format);
92
93         final Optional<LineOfBusiness> optional = cacheServiceProvider.getLineOfBusiness(lineOfBusinessName);
94         if (optional.isPresent()) {
95
96             final Format value = Format.forValue(format);
97             switch (value) {
98                 case RAW:
99                     final LineOfBusiness platform = optional.get();
100                     LOGGER.info("found LineOfBusiness {} in cache", platform);
101                     return ResponseEntity.ok(platform);
102                 case COUNT:
103                     final Map<String, Object> map = new HashMap<>();
104                     map.put(LINE_OF_BUSINESS, 1);
105                     return ResponseEntity.ok(new Results(map));
106                 default:
107                     break;
108             }
109             LOGGER.error("invalid format type :{}", format);
110         }
111         LOGGER.error("Unable to find LineOfBusiness in cache using {}", lineOfBusinessName);
112         return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);
113     }
114
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());
121
122         final Optional<Relationship> optional =
123                 cacheServiceProvider.addRelationShip(lineOfBusinessName, relationship, request.getRequestURI());
124
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);
129         }
130
131         LOGGER.error("Couldn't add {} relationship for 'line-of-business-name': {} ...", relationship.getRelatedTo(),
132                 lineOfBusinessName);
133
134         return getRequestErrorResponseEntity(request, LINE_OF_BUSINESS);
135
136     }
137
138 }