problem with when SO not responding
[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.Date;
19 import java.util.List;
20 import javax.validation.Valid;
21 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
22 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
23 import org.onap.nbi.apis.serviceorder.model.StateType;
24 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
25 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderRepository;
26 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
27 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
28 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
29 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
30 import org.onap.nbi.commons.JsonRepresentation;
31 import org.onap.nbi.commons.ResourceManagement;
32 import org.onap.nbi.exceptions.ValidationException;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.data.mongodb.core.MongoTemplate;
35 import org.springframework.data.mongodb.core.query.Query;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.scheduling.annotation.EnableScheduling;
40 import org.springframework.scheduling.annotation.Scheduled;
41 import org.springframework.util.MultiValueMap;
42 import org.springframework.validation.Errors;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
50 import org.springframework.web.bind.annotation.RestController;
51
52 @RestController
53 @RequestMapping("/serviceOrder")
54 @EnableScheduling
55 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
56
57     @Autowired
58     ServiceOrderRepository serviceOrderRepository;
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 = serviceOrderRepository.findOne(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 = serviceOrderRepository.count();
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         serviceOrderRepository.delete(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.setState(StateType.ACKNOWLEDGED);
126         serviceOrder.setOrderDate(new Date());
127         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
128             serviceOrderItem.setState(StateType.ACKNOWLEDGED);
129         }
130
131         ServiceOrder serviceOrderSaved = serviceOrderRepository.save(serviceOrder);
132         serviceOrderSaved.setHref("serviceOrder/" + serviceOrderSaved.getId());
133         serviceOrderRepository.save(serviceOrderSaved);
134         JsonRepresentation filter = new JsonRepresentation(params);
135         return this.createResponse(serviceOrderSaved, filter);
136
137     }
138
139     @Scheduled(fixedDelay = 5000)
140     public void scheduleCheckServiceOrders() {
141         List<ServiceOrder> acknowledgedOrders = serviceOrderRepository.findByState(StateType.ACKNOWLEDGED);
142         for (ServiceOrder serviceOrder : acknowledgedOrders) {
143             ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
144             if (serviceOrderInfo.isServiceOrderRejected()) {
145                 changeServiceOrderState(serviceOrder, StateType.REJECTED);
146             } else if (serviceOrderInfo.isAllItemsCompleted()) {
147                 changeServiceOrderState(serviceOrder, StateType.COMPLETED);
148             } else {
149                 serviceOrderRepository.save(serviceOrder);
150                 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
151                 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
152                     createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
153                     if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
154                         serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
155                     }
156                 }
157
158             }
159         }
160     }
161
162     /**
163      *
164      * @param serviceOrder
165      * @param stateType
166      */
167     private void changeServiceOrderState(ServiceOrder serviceOrder, StateType stateType) {
168         serviceOrder.setState(stateType);
169         serviceOrder.setCompletionDateTime(new Date());
170         serviceOrderRepository.save(serviceOrder);
171     }
172
173 }