Adding aai nodes endpoint
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aai / simulator / controller / BusinessController.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.aai.simulator.controller;
21
22 import static org.onap.so.aai.simulator.utils.Constants.CUSTOMER_URL;
23 import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE;
24 import static org.onap.so.aai.simulator.utils.Constants.ERROR_MESSAGE_ID;
25 import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE;
26 import static org.onap.so.aai.simulator.utils.Utils.getResourceVersion;
27 import java.util.Optional;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.MediaType;
30 import org.onap.aai.domain.yang.Customer;
31 import org.onap.aai.domain.yang.ServiceInstance;
32 import org.onap.aai.domain.yang.ServiceInstances;
33 import org.onap.aai.domain.yang.ServiceSubscription;
34 import org.onap.so.aai.simulator.models.NodeServiceInstance;
35 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
36 import org.onap.so.aai.simulator.service.providers.NodesCacheServiceProvider;
37 import org.onap.so.aai.simulator.utils.RequestErrorBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
50
51 /**
52  * @author waqas.ikram@ericsson.com
53  *
54  */
55 @Controller
56 @RequestMapping(path = CUSTOMER_URL)
57 public class BusinessController {
58
59     private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
60     private final CustomerCacheServiceProvider cacheServiceProvider;
61     private final NodesCacheServiceProvider nodesCacheServiceProvider;
62
63     @Autowired
64     public BusinessController(final CustomerCacheServiceProvider cacheServiceProvider,
65             final NodesCacheServiceProvider nodesCacheServiceProvider) {
66         this.cacheServiceProvider = cacheServiceProvider;
67         this.nodesCacheServiceProvider = nodesCacheServiceProvider;
68     }
69
70     @GetMapping(value = "{global-customer-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
71     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
72             final HttpServletRequest request) {
73         LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
74
75         final Optional<Customer> optional = cacheServiceProvider.getCustomer(globalCustomerId);
76         if (optional.isPresent()) {
77             final Customer customer = optional.get();
78             LOGGER.info("found customer {} in cache", customer);
79             return ResponseEntity.ok(customer);
80         }
81
82         LOGGER.error("Couldn't find {} in cache", globalCustomerId);
83         return getRequestErrorResponseEntity(request);
84     }
85
86     @PutMapping(value = "/{global-customer-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
87             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
88     public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
89             @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
90         LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
91
92         if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
93             customer.setResourceVersion(getResourceVersion());
94
95         }
96         cacheServiceProvider.putCustomer(globalCustomerId, customer);
97         return ResponseEntity.accepted().build();
98
99     }
100
101     @GetMapping(value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
102             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
103     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
104             @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
105         LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
106                 globalCustomerId, serviceType);
107
108         final Optional<ServiceSubscription> optional =
109                 cacheServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
110         if (optional.isPresent()) {
111             final ServiceSubscription serviceSubscription = optional.get();
112             LOGGER.info("found service subscription  {} in cache", serviceSubscription);
113             return ResponseEntity.ok(serviceSubscription);
114         }
115
116         LOGGER.error("Couldn't find 'global customer id': {} and 'service type': {} in cache", globalCustomerId,
117                 serviceType);
118         return getRequestErrorResponseEntity(request);
119     }
120
121     @GetMapping(
122             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances",
123             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
124     public ResponseEntity<?> getSericeInstances(@PathVariable("global-customer-id") final String globalCustomerId,
125             @PathVariable("service-type") final String serviceType,
126             @RequestParam(name = "service-instance-name") final String serviceInstanceName,
127             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
128
129         LOGGER.info(
130                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance name: '{} with depth: {}...",
131                 globalCustomerId, serviceType, serviceInstanceName, depth);
132
133         final Optional<ServiceInstances> optional =
134                 cacheServiceProvider.getServiceInstances(globalCustomerId, serviceType, serviceInstanceName);
135         if (optional.isPresent()) {
136             final ServiceInstances serviceInstances = optional.get();
137             LOGGER.info("found service instance  {} in cache", serviceInstances);
138             return ResponseEntity.ok(serviceInstances);
139         }
140         LOGGER.error(
141                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance name': {} with depth: {} in cache",
142                 globalCustomerId, serviceType, serviceInstanceName, depth);
143         return getRequestErrorResponseEntity(request);
144     }
145
146     @GetMapping(
147             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
148             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
149     public ResponseEntity<?> getSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
150             @PathVariable("service-type") final String serviceType,
151             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
152             @RequestParam(name = "depth", required = false) final Integer depth,
153             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
154             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
155             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
156
157         LOGGER.info(
158                 "Will retrieve service instances for 'global customer id': {}, 'service type': {} and 'service instance id: '{} with depth: {}, resultIndex:{}, resultSize: {} and format: {}...",
159                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
160
161         final Optional<ServiceInstance> optional =
162                 cacheServiceProvider.getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
163         if (optional.isPresent()) {
164             final ServiceInstance serviceInstance = optional.get();
165             LOGGER.info("found service instance  {} in cache", serviceInstance);
166             return ResponseEntity.ok(serviceInstance);
167         }
168         LOGGER.error(
169                 "Couldn't find 'global customer id': {}, 'service type': {} and 'service instance id': {} with depth: {}, resultIndex:{}, resultSize: {} and format: {} in cache",
170                 globalCustomerId, serviceType, serviceInstanceId, depth, resultIndex, resultSize, format);
171         return getRequestErrorResponseEntity(request);
172     }
173
174     @PutMapping(
175             value = "/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}",
176             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
177     public ResponseEntity<?> putSericeInstance(@PathVariable("global-customer-id") final String globalCustomerId,
178             @PathVariable("service-type") final String serviceType,
179             @PathVariable(name = "service-instance-id") final String serviceInstanceId,
180             @RequestBody final ServiceInstance serviceInstance, final HttpServletRequest request) {
181
182         LOGGER.info(
183                 "Will add service instance for 'global customer id': {}, 'service type': {} and 'service instance id: '{} ...",
184                 globalCustomerId, serviceType, serviceInstanceId);
185
186         if (serviceInstance.getResourceVersion() == null || serviceInstance.getResourceVersion().isEmpty()) {
187             serviceInstance.setResourceVersion(getResourceVersion());
188         }
189
190         if (cacheServiceProvider.putServiceInstance(globalCustomerId, serviceType, serviceInstanceId,
191                 serviceInstance)) {
192             nodesCacheServiceProvider.putNodeServiceInstance(serviceInstanceId, new NodeServiceInstance(
193                     globalCustomerId, serviceType, serviceInstanceId, SERVICE_RESOURCE_TYPE, request.getRequestURI()));
194             return ResponseEntity.accepted().build();
195         }
196
197         LOGGER.error("Couldn't add 'global customer id': {}, 'service type': {} and 'service instance id': {} to cache",
198                 globalCustomerId, serviceType, serviceInstanceId);
199         return getRequestErrorResponseEntity(request);
200     }
201
202     private ResponseEntity<?> getRequestErrorResponseEntity(final HttpServletRequest request) {
203         return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE)
204                 .variables(request.getMethod(), request.getRequestURI(),
205                         "Node Not Found:No Node of type service-instance found at: " + request.getRequestURI(),
206                         "ERR.5.4.6114")
207                 .build(), HttpStatus.NOT_FOUND);
208     }
209
210 }