2 * Copyright (c) 2018 Orange
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
13 package org.onap.nbi.apis.serviceorder;
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;
49 @RequestMapping("/serviceOrder")
50 public class ServiceOrderResource extends ResourceManagement {
53 ServiceOrderService serviceOrderService;
56 CheckOrderConsistenceManager checkOrderConsistenceManager;
59 CreateAAICustomerManager createAAICustomer;
62 CreateAAIServiceTypeManager createAAIServiceType;
65 MongoTemplate mongoTemplate;
68 SOTaskManager serviceOrchestratorManager;
71 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
74 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
75 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
76 @RequestParam MultiValueMap<String, String> params) {
78 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
79 if (!optionalServiceOrder.isPresent()) {
80 return ResponseEntity.notFound().build();
83 JsonRepresentation filter = new JsonRepresentation(params);
84 return this.getResponse(optionalServiceOrder.get(), filter);
87 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
88 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
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()));
98 return this.findResponse(serviceOrders, filter, headers);
102 @DeleteMapping(value = "/{serviceOrderId}")
103 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
105 serviceOrderService.deleteServiceOrder(serviceOrderId);
107 return this.deleteResponse();
111 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
112 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
113 @RequestParam MultiValueMap<String, String> params) {
115 if (errors != null && errors.hasErrors()) {
116 throw new ValidationException(errors.getAllErrors());
119 ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
120 JsonRepresentation filter = new JsonRepresentation(params);
121 return this.createResponse(serviceOrderSaved, filter);
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();
133 ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
134 JsonRepresentation filter = new JsonRepresentation(params);
135 return this.createResponse(serviceOrder, filter);
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);
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);