Add Apache license header per file
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / ServiceOrderResource.java
1 /**
2  *
3  *     Copyright (c) 2017 Orange.  All rights reserved.
4  *
5  *     Licensed under the Apache License, Version 2.0 (the "License");
6  *     you may not use this file except in compliance with the License.
7  *     You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *     Unless required by applicable law or agreed to in writing, software
12  *     distributed under the License is distributed on an "AS IS" BASIS,
13  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *     See the License for the specific language governing permissions and
15  *     limitations under the License.
16  */
17 package org.onap.nbi.apis.serviceorder;
18
19 import java.util.Date;
20 import java.util.List;
21 import javax.validation.Valid;
22 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
23 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
24 import org.onap.nbi.apis.serviceorder.model.StateType;
25 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
26 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderInfoRepository;
27 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderRepository;
28 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
29 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
30 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
31 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
32 import org.onap.nbi.commons.JsonRepresentation;
33 import org.onap.nbi.commons.ResourceManagement;
34 import org.onap.nbi.exceptions.ValidationException;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.data.mongodb.core.MongoTemplate;
37 import org.springframework.data.mongodb.core.query.Query;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.scheduling.annotation.EnableScheduling;
42 import org.springframework.scheduling.annotation.Scheduled;
43 import org.springframework.util.MultiValueMap;
44 import org.springframework.validation.Errors;
45 import org.springframework.web.bind.annotation.DeleteMapping;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.PostMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.RequestParam;
52 import org.springframework.web.bind.annotation.RestController;
53
54 @RestController
55 @RequestMapping("/serviceOrder")
56 @EnableScheduling
57 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
58
59     @Autowired
60     ServiceOrderRepository serviceOrderRepository;
61
62     @Autowired
63     CheckOrderConsistenceManager checkOrderConsistenceManager;
64
65     @Autowired
66     CreateAAICustomerManager createAAICustomer;
67
68     @Autowired
69     CreateAAIServiceTypeManager createAAIServiceType;
70
71     @Autowired
72     MongoTemplate mongoTemplate;
73
74     @Autowired
75     SOTaskManager serviceOrchestratorManager;
76
77     @Autowired
78     ServiceOrderInfoRepository serviceOrderInfoRepository;
79
80     @Autowired
81     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
82
83
84     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
85     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
86             @RequestParam MultiValueMap<String, String> params) {
87
88         ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
89         if (serviceOrder == null) {
90             return ResponseEntity.notFound().build();
91         }
92
93         JsonRepresentation filter = new JsonRepresentation(params);
94         return this.getResponse(serviceOrder, filter);
95     }
96
97     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
98     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
99
100         Query query = multiCriteriaRequestBuilder.buildRequest(params);
101         List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
102         JsonRepresentation filter = new JsonRepresentation(params);
103         long totalCount = serviceOrderRepository.count();
104         HttpHeaders headers = new HttpHeaders();
105         headers.add("X-Total-Count", String.valueOf(totalCount));
106         headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
107
108         ResponseEntity<Object> response = this.findResponse(serviceOrders, filter, headers);
109         return response;
110
111     }
112
113     @DeleteMapping(value = "/{serviceOrderId}")
114     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
115
116         serviceOrderRepository.delete(serviceOrderId);
117
118         return this.deleteResponse();
119
120     }
121
122     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
123     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
124             @RequestParam MultiValueMap<String, String> params) {
125
126
127         if (errors != null && errors.hasErrors()) {
128             throw new ValidationException(errors.getAllErrors());
129         }
130
131         serviceOrder.setState(StateType.ACKNOWLEDGED);
132         serviceOrder.setOrderDate(new Date());
133         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
134             serviceOrderItem.setState(StateType.ACKNOWLEDGED);
135         }
136
137         ServiceOrder serviceOrderSaved = serviceOrderRepository.save(serviceOrder);
138         serviceOrderSaved.setHref("serviceOrder/" + serviceOrderSaved.getId());
139         serviceOrderRepository.save(serviceOrderSaved);
140         JsonRepresentation filter = new JsonRepresentation(params);
141         return this.createResponse(serviceOrderSaved, filter);
142
143     }
144
145     @Scheduled(fixedDelay = 5000)
146     public void scheduleCheckServiceOrders() {
147         List<ServiceOrder> acknowledgedOrders = serviceOrderRepository.findByState(StateType.ACKNOWLEDGED);
148         for (ServiceOrder serviceOrder : acknowledgedOrders) {
149             ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
150             if (serviceOrderInfo.isServiceOrderRejected()) {
151                 changeServiceOrderState(serviceOrder, StateType.REJECTED);
152             } else if (serviceOrderInfo.isAllItemsCompleted()) {
153                 changeServiceOrderState(serviceOrder, StateType.COMPLETED);
154             } else {
155                 serviceOrderRepository.save(serviceOrder);
156                 createAAICustomer.createAAICustomer(serviceOrderInfo);
157                 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
158                 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
159             }
160         }
161     }
162
163     /**
164      *
165      * @param serviceOrder
166      * @param stateType
167      */
168     private void changeServiceOrderState(ServiceOrder serviceOrder, StateType stateType) {
169         serviceOrder.setState(stateType);
170         serviceOrder.setCompletionDateTime(new Date());
171         serviceOrderRepository.save(serviceOrder);
172     }
173
174 }