1 package org.onap.nbi.apis.serviceorder;
5 import javax.validation.Valid;
6 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
7 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
8 import org.onap.nbi.apis.serviceorder.model.StateType;
9 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
10 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderInfoRepository;
11 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderRepository;
12 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
13 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
14 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
15 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
16 import org.onap.nbi.commons.JsonRepresentation;
17 import org.onap.nbi.commons.ResourceManagement;
18 import org.onap.nbi.exceptions.ValidationException;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.data.mongodb.core.MongoTemplate;
21 import org.springframework.data.mongodb.core.query.Query;
22 import org.springframework.http.HttpHeaders;
23 import org.springframework.http.MediaType;
24 import org.springframework.http.ResponseEntity;
25 import org.springframework.scheduling.annotation.EnableScheduling;
26 import org.springframework.scheduling.annotation.Scheduled;
27 import org.springframework.util.MultiValueMap;
28 import org.springframework.validation.Errors;
29 import org.springframework.web.bind.annotation.DeleteMapping;
30 import org.springframework.web.bind.annotation.GetMapping;
31 import org.springframework.web.bind.annotation.PathVariable;
32 import org.springframework.web.bind.annotation.PostMapping;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestParam;
36 import org.springframework.web.bind.annotation.RestController;
39 @RequestMapping("/serviceOrder")
41 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
44 ServiceOrderRepository serviceOrderRepository;
47 CheckOrderConsistenceManager checkOrderConsistenceManager;
50 CreateAAICustomerManager createAAICustomer;
53 CreateAAIServiceTypeManager createAAIServiceType;
56 MongoTemplate mongoTemplate;
59 SOTaskManager serviceOrchestratorManager;
62 ServiceOrderInfoRepository serviceOrderInfoRepository;
65 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
68 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
69 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
70 @RequestParam MultiValueMap<String, String> params) {
72 ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
73 if (serviceOrder == null) {
74 return ResponseEntity.notFound().build();
77 JsonRepresentation filter = new JsonRepresentation(params);
78 return this.getResponse(serviceOrder, filter);
81 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
82 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
84 Query query = multiCriteriaRequestBuilder.buildRequest(params);
85 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
86 JsonRepresentation filter = new JsonRepresentation(params);
87 long totalCount = serviceOrderRepository.count();
88 HttpHeaders headers = new HttpHeaders();
89 headers.add("X-Total-Count", String.valueOf(totalCount));
90 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
92 ResponseEntity<Object> response = this.findResponse(serviceOrders, filter, headers);
97 @DeleteMapping(value = "/{serviceOrderId}")
98 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
100 serviceOrderRepository.delete(serviceOrderId);
102 return this.deleteResponse();
106 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
107 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
108 @RequestParam MultiValueMap<String, String> params) {
111 if (errors != null && errors.hasErrors()) {
112 throw new ValidationException(errors.getAllErrors());
115 serviceOrder.setState(StateType.ACKNOWLEDGED);
116 serviceOrder.setOrderDate(new Date());
117 for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
118 serviceOrderItem.setState(StateType.ACKNOWLEDGED);
121 ServiceOrder serviceOrderSaved = serviceOrderRepository.save(serviceOrder);
122 serviceOrderSaved.setHref("serviceOrder/" + serviceOrderSaved.getId());
123 serviceOrderRepository.save(serviceOrderSaved);
124 JsonRepresentation filter = new JsonRepresentation(params);
125 return this.createResponse(serviceOrderSaved, filter);
129 @Scheduled(fixedDelay = 5000)
130 public void scheduleCheckServiceOrders() {
131 List<ServiceOrder> acknowledgedOrders = serviceOrderRepository.findByState(StateType.ACKNOWLEDGED);
132 for (ServiceOrder serviceOrder : acknowledgedOrders) {
133 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
134 if (serviceOrderInfo.isServiceOrderRejected()) {
135 changeServiceOrderState(serviceOrder, StateType.REJECTED);
136 } else if (serviceOrderInfo.isAllItemsCompleted()) {
137 changeServiceOrderState(serviceOrder, StateType.COMPLETED);
139 serviceOrderRepository.save(serviceOrder);
140 createAAICustomer.createAAICustomer(serviceOrderInfo);
141 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
142 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
149 * @param serviceOrder
152 private void changeServiceOrderState(ServiceOrder serviceOrder, StateType stateType) {
153 serviceOrder.setState(stateType);
154 serviceOrder.setCompletionDateTime(new Date());
155 serviceOrderRepository.save(serviceOrder);