a80477cde1027955cfe82bc333f6e8e65a665fb8
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / PlatformController.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.PLATFORMS_URL;
23 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
24 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
25 import java.util.Optional;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.aai.domain.yang.Platform;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider;
31 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
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 = PLATFORMS_URL)
49 public class PlatformController {
50     private static final Logger LOGGER = LoggerFactory.getLogger(PlatformController.class);
51
52     private final PlatformCacheServiceProvider cacheServiceProvider;
53
54     @Autowired
55     public PlatformController(final PlatformCacheServiceProvider cacheServiceProvider) {
56         this.cacheServiceProvider = cacheServiceProvider;
57     }
58
59     @PutMapping(value = "{platform-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
60             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
61     public ResponseEntity<?> putPlatform(@RequestBody final Platform platform,
62             @PathVariable("platform-name") final String platformName, final HttpServletRequest request) {
63         LOGGER.info("Will add Platform to cache with key 'platform-name': {} ...", platform.getPlatformName());
64
65         if (platform.getResourceVersion() == null || platform.getResourceVersion().isEmpty()) {
66             platform.setResourceVersion(getResourceVersion());
67
68         }
69         cacheServiceProvider.putPlatform(platformName, platform);
70         return ResponseEntity.accepted().build();
71     }
72
73     @GetMapping(value = "/{platform-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
74     public ResponseEntity<?> getPlatform(@PathVariable("platform-name") final String platformName,
75             final HttpServletRequest request) {
76         LOGGER.info("retrieving Platform for 'platform-name': {} ...", platformName);
77         final Optional<Platform> optional = cacheServiceProvider.getPlatform(platformName);
78         if (optional.isPresent()) {
79             final Platform platform = optional.get();
80             LOGGER.info("found Platform {} in cache", platform);
81             return ResponseEntity.ok(platform);
82         }
83         LOGGER.error("Unable to find Platform in cahce using {}", platformName);
84         return getRequestErrorResponseEntity(request);
85     }
86
87     @PutMapping(value = "/{platform-name}/relationship-list/relationship",
88             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
89             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
90     public ResponseEntity<?> putSericeInstanceRelationShip(@PathVariable("platform-name") final String platformName,
91             @RequestBody final Relationship relationship, final HttpServletRequest request) {
92         LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
93
94         final Optional<Relationship> optional =
95                 cacheServiceProvider.addRelationShip(platformName, relationship, request.getRequestURI());
96
97         if (optional.isPresent()) {
98             final Relationship resultantRelationship = optional.get();
99             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
100             return ResponseEntity.accepted().body(resultantRelationship);
101         }
102
103         LOGGER.error("Couldn't add {} relationship for 'platform-name': {} ...", relationship.getRelatedTo(),
104                 platformName);
105
106         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request);
107
108     }
109 }