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.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;
44 * @author Waqas Ikram (waqas.ikram@est.tech)
48 @RequestMapping(path = PLATFORMS_URL)
49 public class PlatformController {
50 private static final Logger LOGGER = LoggerFactory.getLogger(PlatformController.class);
52 private final PlatformCacheServiceProvider cacheServiceProvider;
55 public PlatformController(final PlatformCacheServiceProvider cacheServiceProvider) {
56 this.cacheServiceProvider = cacheServiceProvider;
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());
65 if (platform.getResourceVersion() == null || platform.getResourceVersion().isEmpty()) {
66 platform.setResourceVersion(getResourceVersion());
69 cacheServiceProvider.putPlatform(platformName, platform);
70 return ResponseEntity.accepted().build();
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);
83 LOGGER.error("Unable to find Platform in cahce using {}", platformName);
84 return getRequestErrorResponseEntity(request);
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());
94 final Optional<Relationship> optional =
95 cacheServiceProvider.addRelationShip(platformName, relationship, request.getRequestURI());
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);
103 LOGGER.error("Couldn't add {} relationship for 'platform-name': {} ...", relationship.getRelatedTo(),
106 return RequestErrorResponseUtils.getRequestErrorResponseEntity(request);