pnf sw upgrade csit
[integration/csit.git] / plans / usecases / pnf-sw-upgrade / so / simulator / 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.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.so.aaisimulator.utils.Constants.PLATFORM;
24 import static org.onap.so.aaisimulator.utils.Constants.PLATFORMS_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.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.Platform;
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.PlatformCacheServiceProvider;
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 = PLATFORMS_URL)
55 public class PlatformController {
56     private static final Logger LOGGER = LoggerFactory.getLogger(PlatformController.class);
57
58     private final PlatformCacheServiceProvider cacheServiceProvider;
59
60     @Autowired
61     public PlatformController(final PlatformCacheServiceProvider cacheServiceProvider) {
62         this.cacheServiceProvider = cacheServiceProvider;
63     }
64
65     @PutMapping(value = "{platform-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
66             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
67     public ResponseEntity<?> putPlatform(@RequestBody final Platform platform,
68             @PathVariable("platform-name") final String platformName, final HttpServletRequest request) {
69         LOGGER.info("Will add Platform to cache with key 'platform-name': {} ...", platform.getPlatformName());
70
71         if (platform.getResourceVersion() == null || platform.getResourceVersion().isEmpty()) {
72             platform.setResourceVersion(getResourceVersion());
73
74         }
75         cacheServiceProvider.putPlatform(platformName, platform);
76         return ResponseEntity.accepted().build();
77     }
78
79     @GetMapping(value = "/{platform-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
80     public ResponseEntity<?> getPlatform(@PathVariable("platform-name") final String platformName,
81             @RequestParam(name = "depth", required = false) final Integer depth,
82             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
83             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
84             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
85
86         LOGGER.info(
87                 "retrieving Platform for 'platform-name': {} with depth: {}, resultIndex: {}, resultSize:{}, format: {} ...",
88                 platformName, depth, resultIndex, resultSize, format);
89         final Optional<Platform> optional = cacheServiceProvider.getPlatform(platformName);
90         if (optional.isPresent()) {
91
92             final Format value = Format.forValue(format);
93             switch (value) {
94                 case RAW:
95                     final Platform platform = optional.get();
96                     LOGGER.info("found Platform {} in cache", platform);
97                     return ResponseEntity.ok(platform);
98                 case COUNT:
99                     final Map<String, Object> map = new HashMap<>();
100                     map.put(PLATFORM, 1);
101                     return ResponseEntity.ok(new Results(map));
102                 default:
103                     break;
104             }
105             LOGGER.error("invalid format type :{}", format);
106
107         }
108         LOGGER.error("Unable to find Platform in cahce using {}", platformName);
109         return getRequestErrorResponseEntity(request, PLATFORM);
110     }
111
112     @PutMapping(value = "/{platform-name}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
113             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
114             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
115     public ResponseEntity<?> putRelationShip(@PathVariable("platform-name") final String platformName,
116             @RequestBody final Relationship relationship, final HttpServletRequest request) {
117         LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
118
119         final Optional<Relationship> optional =
120                 cacheServiceProvider.addRelationShip(platformName, relationship, request.getRequestURI());
121
122         if (optional.isPresent()) {
123             final Relationship resultantRelationship = optional.get();
124             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
125             return ResponseEntity.accepted().body(resultantRelationship);
126         }
127
128         LOGGER.error("Couldn't add {} relationship for 'platform-name': {} ...", relationship.getRelatedTo(),
129                 platformName);
130
131         return getRequestErrorResponseEntity(request, PLATFORM);
132
133     }
134 }