2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.nbi.apis.serviceorder;
18 import java.util.List;
19 import javax.validation.Valid;
20 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
21 import org.onap.nbi.apis.serviceorder.model.StateType;
22 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
23 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
24 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
25 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
26 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
27 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
28 import org.onap.nbi.commons.JsonRepresentation;
29 import org.onap.nbi.commons.ResourceManagement;
30 import org.onap.nbi.exceptions.ValidationException;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.data.mongodb.core.MongoTemplate;
33 import org.springframework.data.mongodb.core.query.Query;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.scheduling.annotation.EnableScheduling;
38 import org.springframework.scheduling.annotation.Scheduled;
39 import org.springframework.util.MultiValueMap;
40 import org.springframework.validation.Errors;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.web.bind.annotation.RestController;
51 @RequestMapping("/serviceOrder")
53 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
58 ServiceOrderService serviceOrderService;
61 CheckOrderConsistenceManager checkOrderConsistenceManager;
64 CreateAAICustomerManager createAAICustomer;
67 CreateAAIServiceTypeManager createAAIServiceType;
70 MongoTemplate mongoTemplate;
73 SOTaskManager serviceOrchestratorManager;
76 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
79 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
80 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
81 @RequestParam MultiValueMap<String, String> params) {
83 ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
84 if (serviceOrder == null) {
85 return ResponseEntity.notFound().build();
88 JsonRepresentation filter = new JsonRepresentation(params);
89 return this.getResponse(serviceOrder, filter);
92 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
93 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
95 Query query = multiCriteriaRequestBuilder.buildRequest(params);
96 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
97 JsonRepresentation filter = new JsonRepresentation(params);
98 long totalCount = serviceOrderService.countServiceOrder();
99 HttpHeaders headers = new HttpHeaders();
100 headers.add("X-Total-Count", String.valueOf(totalCount));
101 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
103 return this.findResponse(serviceOrders, filter, headers);
107 @DeleteMapping(value = "/{serviceOrderId}")
108 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
110 serviceOrderService.deleteServiceOrder(serviceOrderId);
112 return this.deleteResponse();
116 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
117 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
118 @RequestParam MultiValueMap<String, String> params) {
121 if (errors != null && errors.hasErrors()) {
122 throw new ValidationException(errors.getAllErrors());
125 ServiceOrder serviceOrderSaved =serviceOrderService.updateOrderAndItemStateToAcknowledged(serviceOrder);
126 serviceOrderService.updateOrderHref(serviceOrderSaved);
127 JsonRepresentation filter = new JsonRepresentation(params);
128 return this.createResponse(serviceOrderSaved, filter);
132 @Scheduled(fixedDelay = 5000)
133 public void scheduleCheckServiceOrders() {
134 List<ServiceOrder> acknowledgedOrders = serviceOrderService.findServiceOrdersByState(StateType.ACKNOWLEDGED);
135 for (ServiceOrder serviceOrder : acknowledgedOrders) {
136 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
137 if (serviceOrderInfo.isServiceOrderRejected()) {
138 serviceOrderService.updateOrderFinalState(serviceOrder, StateType.REJECTED);
139 } else if (serviceOrderInfo.isAllItemsCompleted()) {
140 serviceOrderService.updateOrderFinalState(serviceOrder, StateType.COMPLETED);
142 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
143 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
144 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
145 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
146 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);