Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases / pnf-sw-upgrade / so / simulator / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / NodesController.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.GENERIC_VNF;
23 import static org.onap.so.aaisimulator.utils.Constants.NODES_URL;
24 import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_LINK;
25 import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_TYPE;
26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
27 import java.util.LinkedHashMap;
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.GenericVnfs;
33 import org.onap.aai.domain.yang.ServiceInstance;
34 import org.onap.so.aaisimulator.models.Format;
35 import org.onap.so.aaisimulator.models.NodeServiceInstance;
36 import org.onap.so.aaisimulator.models.Results;
37 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
47
48 /**
49  * @author waqas.ikram@ericsson.com
50  *
51  */
52 @Controller
53 @RequestMapping(path = NODES_URL)
54 public class NodesController {
55
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(NodesController.class);
58
59     private final NodesCacheServiceProvider cacheServiceProvider;
60
61     @Autowired
62     public NodesController(final NodesCacheServiceProvider cacheServiceProvider) {
63         this.cacheServiceProvider = cacheServiceProvider;
64     }
65
66     @GetMapping(value = "/service-instances/service-instance/{service-instance-id}",
67             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
68     public ResponseEntity<?> getProject(@PathVariable(name = "service-instance-id") final String serviceInstanceId,
69             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
70         LOGGER.info("retrieving service instance using 'service-instance-id': {} and format: {}...", serviceInstanceId,
71                 format);
72
73         final Optional<NodeServiceInstance> optional = cacheServiceProvider.getNodeServiceInstance(serviceInstanceId);
74         if (!optional.isPresent()) {
75             LOGGER.error("Couldn't find {} in cache", serviceInstanceId);
76             return getRequestErrorResponseEntity(request);
77         }
78
79         final Format value = Format.forValue(format);
80         final NodeServiceInstance nodeServiceInstance = optional.get();
81         switch (value) {
82             case PATHED:
83                 LOGGER.info("found project {} in cache", nodeServiceInstance);
84                 final Map<String, Object> map = new LinkedHashMap<>();
85                 map.put(RESOURCE_TYPE, nodeServiceInstance.getResourceType());
86                 map.put(RESOURCE_LINK, nodeServiceInstance.getResourceLink());
87                 return ResponseEntity.ok(new Results(map));
88             case RAW:
89                 final Optional<ServiceInstance> serviceInstance =
90                         cacheServiceProvider.getServiceInstance(nodeServiceInstance);
91                 if (serviceInstance.isPresent()) {
92                     return ResponseEntity.ok(serviceInstance.get());
93                 }
94                 LOGGER.error("Unable to find Service instance in cahce using {}", nodeServiceInstance);
95                 return getRequestErrorResponseEntity(request);
96             default:
97                 break;
98         }
99         LOGGER.error("invalid format type :{}", format);
100         return getRequestErrorResponseEntity(request);
101     }
102
103     @GetMapping(value = "/generic-vnfs", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
104     public ResponseEntity<?> getGenericVnfs(@RequestParam(name = "vnf-name") final String vnfName,
105             final HttpServletRequest request) {
106         LOGGER.info("will find GenericVnfs for name: {}", vnfName);
107         final Optional<GenericVnfs> optional = cacheServiceProvider.getGenericVnfs(vnfName);
108         if (optional.isPresent()) {
109             LOGGER.info("found matching GenericVnfs for name: {}", vnfName);
110             return ResponseEntity.ok(optional.get());
111         }
112         LOGGER.error("Unable to find GenericVnfs in cahce using {}", vnfName);
113         return getRequestErrorResponseEntity(request, GENERIC_VNF);
114     }
115 }