1842cfe9366c87da00d4df5bc0f60b6a1ef75fc4
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / ServiceOrderResource.java
1 package org.onap.nbi.apis.serviceorder;
2
3 import java.util.Date;
4 import java.util.List;
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;
37
38 @RestController
39 @RequestMapping("/serviceOrder")
40 @EnableScheduling
41 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
42
43     @Autowired
44     ServiceOrderRepository serviceOrderRepository;
45
46     @Autowired
47     CheckOrderConsistenceManager checkOrderConsistenceManager;
48
49     @Autowired
50     CreateAAICustomerManager createAAICustomer;
51
52     @Autowired
53     CreateAAIServiceTypeManager createAAIServiceType;
54
55     @Autowired
56     MongoTemplate mongoTemplate;
57
58     @Autowired
59     SOTaskManager serviceOrchestratorManager;
60
61     @Autowired
62     ServiceOrderInfoRepository serviceOrderInfoRepository;
63
64     @Autowired
65     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
66
67
68     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
69     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
70             @RequestParam MultiValueMap<String, String> params) {
71
72         ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
73         if (serviceOrder == null) {
74             return ResponseEntity.notFound().build();
75         }
76
77         JsonRepresentation filter = new JsonRepresentation(params);
78         return this.getResponse(serviceOrder, filter);
79     }
80
81     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
82     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
83
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()));
91
92         ResponseEntity<Object> response = this.findResponse(serviceOrders, filter, headers);
93         return response;
94
95     }
96
97     @DeleteMapping(value = "/{serviceOrderId}")
98     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
99
100         serviceOrderRepository.delete(serviceOrderId);
101
102         return this.deleteResponse();
103
104     }
105
106     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
107     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
108             @RequestParam MultiValueMap<String, String> params) {
109
110
111         if (errors != null && errors.hasErrors()) {
112             throw new ValidationException(errors.getAllErrors());
113         }
114
115         serviceOrder.setState(StateType.ACKNOWLEDGED);
116         serviceOrder.setOrderDate(new Date());
117         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
118             serviceOrderItem.setState(StateType.ACKNOWLEDGED);
119         }
120
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);
126
127     }
128
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);
138             } else {
139                 serviceOrderRepository.save(serviceOrder);
140                 createAAICustomer.createAAICustomer(serviceOrderInfo);
141                 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
142                 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
143             }
144         }
145     }
146
147     /**
148      *
149      * @param serviceOrder
150      * @param stateType
151      */
152     private void changeServiceOrderState(ServiceOrder serviceOrder, StateType stateType) {
153         serviceOrder.setState(stateType);
154         serviceOrder.setCompletionDateTime(new Date());
155         serviceOrderRepository.save(serviceOrder);
156     }
157
158 }