Adding customer and serive subscription handling
[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.ServiceSubscription;
30 import org.onap.so.aai.simulator.service.providers.CustomerServiceProvider;
31 import org.onap.so.aai.simulator.utils.RequestErrorBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PutMapping;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43
44 /**
45  * @author waqas.ikram@ericsson.com
46  *
47  */
48 @Controller
49 @RequestMapping(path = BUSINESS_URL)
50 public class BusinessController {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(BusinessController.class);
53     private final CustomerServiceProvider customerServiceProvider;
54
55     @Autowired
56     public BusinessController(final CustomerServiceProvider customerServiceProvider) {
57         this.customerServiceProvider = customerServiceProvider;
58     }
59
60     @GetMapping(value = "/customers/customer/{global-customer-id}",
61             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
62     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
63             final HttpServletRequest request) {
64         LOGGER.info("Will retrieve customer for 'global customer id': {} ...", globalCustomerId);
65
66         final Optional<Customer> optional = customerServiceProvider.getCustomer(globalCustomerId);
67         if (optional.isPresent()) {
68             final Customer customer = optional.get();
69             LOGGER.info("found customer {} in cache", customer);
70             return ResponseEntity.ok(customer);
71         }
72
73         LOGGER.error("Couldn't find {} in cache", globalCustomerId);
74         return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE)
75                 .variables(request.getMethod(), request.getRequestURI()).build(), HttpStatus.NOT_FOUND);
76     }
77
78     @PutMapping(value = "/customers/customer/{global-customer-id}",
79             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
80             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
81     public ResponseEntity<?> putCustomer(@RequestBody final Customer customer,
82             @PathVariable("global-customer-id") final String globalCustomerId, final HttpServletRequest request) {
83         LOGGER.info("Will put customer for 'global customer id': {} ...", globalCustomerId);
84
85         if (customer.getResourceVersion() == null || customer.getResourceVersion().isEmpty()) {
86             customer.setResourceVersion(System.currentTimeMillis() + "");
87
88         }
89         customerServiceProvider.putCustomer(globalCustomerId, customer);
90         return ResponseEntity.accepted().build();
91
92     }
93
94     @GetMapping(
95             value = "/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}",
96             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
97     public ResponseEntity<?> getCustomer(@PathVariable("global-customer-id") final String globalCustomerId,
98             @PathVariable("service-type") final String serviceType, final HttpServletRequest request) {
99         LOGGER.info("Will retrieve service subscription for 'global customer id': {} and 'service type': {} ...",
100                 globalCustomerId, serviceType);
101
102         final Optional<ServiceSubscription> optional =
103                 customerServiceProvider.getServiceSubscription(globalCustomerId, serviceType);
104         if (optional.isPresent()) {
105             final ServiceSubscription serviceSubscription = optional.get();
106             LOGGER.info("found service subscription  {} in cache", serviceSubscription);
107             return ResponseEntity.ok(serviceSubscription);
108         }
109
110         LOGGER.error("Couldn't find {} in cache", globalCustomerId);
111         return new ResponseEntity<>(new RequestErrorBuilder().messageId(ERROR_MESSAGE_ID).text(ERROR_MESSAGE)
112                 .variables(request.getMethod(), request.getRequestURI()).build(), HttpStatus.NOT_FOUND);
113     }
114
115
116 }