Remove 'All rights reserved.' on apache 2 license
[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.ServiceOrderInfoRepository;
26 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderRepository;
27 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
28 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
29 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
30 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
31 import org.onap.nbi.commons.JsonRepresentation;
32 import org.onap.nbi.commons.ResourceManagement;
33 import org.onap.nbi.exceptions.ValidationException;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.data.mongodb.core.MongoTemplate;
36 import org.springframework.data.mongodb.core.query.Query;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.MediaType;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.scheduling.annotation.EnableScheduling;
41 import org.springframework.scheduling.annotation.Scheduled;
42 import org.springframework.util.MultiValueMap;
43 import org.springframework.validation.Errors;
44 import org.springframework.web.bind.annotation.DeleteMapping;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PathVariable;
47 import org.springframework.web.bind.annotation.PostMapping;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.web.bind.annotation.RestController;
52
53 @RestController
54 @RequestMapping("/serviceOrder")
55 @EnableScheduling
56 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
57
58     @Autowired
59     ServiceOrderRepository serviceOrderRepository;
60
61     @Autowired
62     CheckOrderConsistenceManager checkOrderConsistenceManager;
63
64     @Autowired
65     CreateAAICustomerManager createAAICustomer;
66
67     @Autowired
68     CreateAAIServiceTypeManager createAAIServiceType;
69
70     @Autowired
71     MongoTemplate mongoTemplate;
72
73     @Autowired
74     SOTaskManager serviceOrchestratorManager;
75
76     @Autowired
77     ServiceOrderInfoRepository serviceOrderInfoRepository;
78
79     @Autowired
80     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
81
82
83     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
84     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
85             @RequestParam MultiValueMap<String, String> params) {
86
87         ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
88         if (serviceOrder == null) {
89             return ResponseEntity.notFound().build();
90         }
91
92         JsonRepresentation filter = new JsonRepresentation(params);
93         return this.getResponse(serviceOrder, filter);
94     }
95
96     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
97     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
98
99         Query query = multiCriteriaRequestBuilder.buildRequest(params);
100         List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
101         JsonRepresentation filter = new JsonRepresentation(params);
102         long totalCount = serviceOrderRepository.count();
103         HttpHeaders headers = new HttpHeaders();
104         headers.add("X-Total-Count", String.valueOf(totalCount));
105         headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
106
107         ResponseEntity<Object> response = this.findResponse(serviceOrders, filter, headers);
108         return response;
109
110     }
111
112     @DeleteMapping(value = "/{serviceOrderId}")
113     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
114
115         serviceOrderRepository.delete(serviceOrderId);
116
117         return this.deleteResponse();
118
119     }
120
121     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
122     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
123             @RequestParam MultiValueMap<String, String> params) {
124
125
126         if (errors != null && errors.hasErrors()) {
127             throw new ValidationException(errors.getAllErrors());
128         }
129
130         serviceOrder.setState(StateType.ACKNOWLEDGED);
131         serviceOrder.setOrderDate(new Date());
132         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
133             serviceOrderItem.setState(StateType.ACKNOWLEDGED);
134         }
135
136         ServiceOrder serviceOrderSaved = serviceOrderRepository.save(serviceOrder);
137         serviceOrderSaved.setHref("serviceOrder/" + serviceOrderSaved.getId());
138         serviceOrderRepository.save(serviceOrderSaved);
139         JsonRepresentation filter = new JsonRepresentation(params);
140         return this.createResponse(serviceOrderSaved, filter);
141
142     }
143
144     @Scheduled(fixedDelay = 5000)
145     public void scheduleCheckServiceOrders() {
146         List<ServiceOrder> acknowledgedOrders = serviceOrderRepository.findByState(StateType.ACKNOWLEDGED);
147         for (ServiceOrder serviceOrder : acknowledgedOrders) {
148             ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
149             if (serviceOrderInfo.isServiceOrderRejected()) {
150                 changeServiceOrderState(serviceOrder, StateType.REJECTED);
151             } else if (serviceOrderInfo.isAllItemsCompleted()) {
152                 changeServiceOrderState(serviceOrder, StateType.COMPLETED);
153             } else {
154                 serviceOrderRepository.save(serviceOrder);
155                 createAAICustomer.createAAICustomer(serviceOrderInfo);
156                 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
157                 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
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 }