ca01af99a3d13d852eede9872764f27aa2e3137e
[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");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.nbi.apis.serviceorder;
17
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;
49
50 @RestController
51 @RequestMapping("/serviceOrder")
52 @EnableScheduling
53 public class ServiceOrderResource extends ResourceManagement {
54
55
56
57     @Autowired
58     ServiceOrderService serviceOrderService;
59
60     @Autowired
61     CheckOrderConsistenceManager checkOrderConsistenceManager;
62
63     @Autowired
64     CreateAAICustomerManager createAAICustomer;
65
66     @Autowired
67     CreateAAIServiceTypeManager createAAIServiceType;
68
69     @Autowired
70     MongoTemplate mongoTemplate;
71
72     @Autowired
73     SOTaskManager serviceOrchestratorManager;
74
75     @Autowired
76     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
77
78
79     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
80     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
81             @RequestParam MultiValueMap<String, String> params) {
82
83         ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
84         if (serviceOrder == null) {
85             return ResponseEntity.notFound().build();
86         }
87
88         JsonRepresentation filter = new JsonRepresentation(params);
89         return this.getResponse(serviceOrder, filter);
90     }
91
92     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
93     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
94
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()));
102
103         return this.findResponse(serviceOrders, filter, headers);
104
105     }
106
107     @DeleteMapping(value = "/{serviceOrderId}")
108     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
109
110         serviceOrderService.deleteServiceOrder(serviceOrderId);
111
112         return this.deleteResponse();
113
114     }
115
116     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
117     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
118             @RequestParam MultiValueMap<String, String> params) {
119
120
121         if (errors != null && errors.hasErrors()) {
122             throw new ValidationException(errors.getAllErrors());
123         }
124
125         ServiceOrder serviceOrderSaved =serviceOrderService.updateOrderAndItemStateToAcknowledged(serviceOrder);
126         serviceOrderService.updateOrderHref(serviceOrderSaved);
127         JsonRepresentation filter = new JsonRepresentation(params);
128         return this.createResponse(serviceOrderSaved, filter);
129
130     }
131
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);
141             } else {
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);
147                     }
148                 }
149
150             }
151         }
152     }
153
154 }