Update to Spring Boot 2
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / ServiceOrderResource.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11  * specific language governing permissions and limitations under the License.
12  */
13 package org.onap.nbi.apis.serviceorder;
14
15 import java.util.List;
16 import java.util.Optional;
17 import javax.validation.Valid;
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
19 import org.onap.nbi.apis.serviceorder.model.StateType;
20 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
21 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
22 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
23 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
24 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
25 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
26 import org.onap.nbi.commons.JsonRepresentation;
27 import org.onap.nbi.commons.MultiCriteriaRequestBuilder;
28 import org.onap.nbi.commons.ResourceManagement;
29 import org.onap.nbi.exceptions.ValidationException;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.data.mongodb.core.MongoTemplate;
32 import org.springframework.data.mongodb.core.query.Query;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.MediaType;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.util.MultiValueMap;
37 import org.springframework.validation.Errors;
38 import org.springframework.web.bind.annotation.DeleteMapping;
39 import org.springframework.web.bind.annotation.GetMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PostMapping;
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 import org.springframework.web.bind.annotation.RestController;
47
48 @RestController
49 @RequestMapping("/serviceOrder")
50 public class ServiceOrderResource extends ResourceManagement {
51
52     @Autowired
53     ServiceOrderService serviceOrderService;
54
55     @Autowired
56     CheckOrderConsistenceManager checkOrderConsistenceManager;
57
58     @Autowired
59     CreateAAICustomerManager createAAICustomer;
60
61     @Autowired
62     CreateAAIServiceTypeManager createAAIServiceType;
63
64     @Autowired
65     MongoTemplate mongoTemplate;
66
67     @Autowired
68     SOTaskManager serviceOrchestratorManager;
69
70     @Autowired
71     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
72
73
74     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
75     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
76         @RequestParam MultiValueMap<String, String> params) {
77
78         Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
79         if (!optionalServiceOrder.isPresent()) {
80             return ResponseEntity.notFound().build();
81         }
82
83         JsonRepresentation filter = new JsonRepresentation(params);
84         return this.getResponse(optionalServiceOrder.get(), filter);
85     }
86
87     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
88     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
89
90         Query query = multiCriteriaRequestBuilder.buildRequest(params);
91         List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
92         JsonRepresentation filter = new JsonRepresentation(params);
93         long totalCount = serviceOrderService.countServiceOrder();
94         HttpHeaders headers = new HttpHeaders();
95         headers.add("X-Total-Count", String.valueOf(totalCount));
96         headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
97
98         return this.findResponse(serviceOrders, filter, headers);
99
100     }
101
102     @DeleteMapping(value = "/{serviceOrderId}")
103     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
104
105         serviceOrderService.deleteServiceOrder(serviceOrderId);
106
107         return this.deleteResponse();
108
109     }
110
111     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
112     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
113         @RequestParam MultiValueMap<String, String> params) {
114
115         if (errors != null && errors.hasErrors()) {
116             throw new ValidationException(errors.getAllErrors());
117         }
118
119         ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
120         JsonRepresentation filter = new JsonRepresentation(params);
121         return this.createResponse(serviceOrderSaved, filter);
122
123     }
124
125
126     @PutMapping(value = "/test/{serviceOrderId}", consumes = MediaType.APPLICATION_JSON_VALUE)
127     public ResponseEntity<Object> checkServiceOrderRessource(@PathVariable String serviceOrderId,
128         @RequestParam MultiValueMap<String, String> params) {
129         Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
130         if (!optionalServiceOrder.isPresent()) {
131             return ResponseEntity.notFound().build();
132         }
133         ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
134         JsonRepresentation filter = new JsonRepresentation(params);
135         return this.createResponse(serviceOrder, filter);
136     }
137
138
139     public ServiceOrder checkServiceOrder(ServiceOrder serviceOrder) {
140         ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
141         if (serviceOrderInfo.isServiceOrderRejected()) {
142             serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
143         } else if (serviceOrderInfo.isAllItemsCompleted()) {
144             serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
145         } else {
146             createAAICustomer.createAAICustomer(serviceOrder, serviceOrderInfo);
147             if (StateType.ACKNOWLEDGED == serviceOrder.getState()) {
148                 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
149                 if (StateType.ACKNOWLEDGED == serviceOrder.getState()) {
150                     serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
151                 }
152             }
153
154         }
155         return serviceOrder;
156     }
157
158 }