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