3 * Copyright (c) 2017 Orange. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.nbi.apis.serviceorder;
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;
55 @RequestMapping("/serviceOrder")
57 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
60 ServiceOrderRepository serviceOrderRepository;
63 CheckOrderConsistenceManager checkOrderConsistenceManager;
66 CreateAAICustomerManager createAAICustomer;
69 CreateAAIServiceTypeManager createAAIServiceType;
72 MongoTemplate mongoTemplate;
75 SOTaskManager serviceOrchestratorManager;
78 ServiceOrderInfoRepository serviceOrderInfoRepository;
81 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
84 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
85 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
86 @RequestParam MultiValueMap<String, String> params) {
88 ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
89 if (serviceOrder == null) {
90 return ResponseEntity.notFound().build();
93 JsonRepresentation filter = new JsonRepresentation(params);
94 return this.getResponse(serviceOrder, filter);
97 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
98 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
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()));
108 ResponseEntity<Object> response = this.findResponse(serviceOrders, filter, headers);
113 @DeleteMapping(value = "/{serviceOrderId}")
114 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
116 serviceOrderRepository.delete(serviceOrderId);
118 return this.deleteResponse();
122 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
123 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
124 @RequestParam MultiValueMap<String, String> params) {
127 if (errors != null && errors.hasErrors()) {
128 throw new ValidationException(errors.getAllErrors());
131 serviceOrder.setState(StateType.ACKNOWLEDGED);
132 serviceOrder.setOrderDate(new Date());
133 for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
134 serviceOrderItem.setState(StateType.ACKNOWLEDGED);
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);
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);
155 serviceOrderRepository.save(serviceOrder);
156 createAAICustomer.createAAICustomer(serviceOrderInfo);
157 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
158 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
165 * @param serviceOrder
168 private void changeServiceOrderState(ServiceOrder serviceOrder, StateType stateType) {
169 serviceOrder.setState(stateType);
170 serviceOrder.setCompletionDateTime(new Date());
171 serviceOrderRepository.save(serviceOrder);