2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.nbi.apis.serviceorder;
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
19 import org.onap.nbi.apis.serviceorder.model.StateType;
20 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
21 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
22 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
23 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
24 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
25 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
26 import org.onap.nbi.commons.JsonRepresentation;
27 import org.onap.nbi.commons.MultiCriteriaRequestBuilder;
28 import org.onap.nbi.commons.ResourceManagement;
29 import org.onap.nbi.exceptions.ValidationException;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.data.mongodb.core.MongoTemplate;
32 import org.springframework.data.mongodb.core.query.Query;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.MediaType;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.scheduling.annotation.EnableScheduling;
37 import org.springframework.scheduling.annotation.Scheduled;
38 import org.springframework.util.MultiValueMap;
39 import org.springframework.validation.Errors;
40 import org.springframework.web.bind.annotation.*;
42 import javax.validation.Valid;
43 import java.util.List;
46 @RequestMapping("/serviceOrder")
48 public class ServiceOrderResource extends ResourceManagement {
53 ServiceOrderService serviceOrderService;
56 CheckOrderConsistenceManager checkOrderConsistenceManager;
59 CreateAAICustomerManager createAAICustomer;
62 CreateAAIServiceTypeManager createAAIServiceType;
65 MongoTemplate mongoTemplate;
68 SOTaskManager serviceOrchestratorManager;
71 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
74 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
75 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
76 @RequestParam MultiValueMap<String, String> params) {
78 ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
79 if (serviceOrder == null) {
80 return ResponseEntity.notFound().build();
83 JsonRepresentation filter = new JsonRepresentation(params);
84 return this.getResponse(serviceOrder, filter);
87 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
88 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
90 Query query = multiCriteriaRequestBuilder.buildRequest(params);
91 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
92 JsonRepresentation filter = new JsonRepresentation(params);
93 long totalCount = serviceOrderService.countServiceOrder();
94 HttpHeaders headers = new HttpHeaders();
95 headers.add("X-Total-Count", String.valueOf(totalCount));
96 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
98 return this.findResponse(serviceOrders, filter, headers);
102 @DeleteMapping(value = "/{serviceOrderId}")
103 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
105 serviceOrderService.deleteServiceOrder(serviceOrderId);
107 return this.deleteResponse();
111 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
112 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
113 @RequestParam MultiValueMap<String, String> params) {
116 if (errors != null && errors.hasErrors()) {
117 throw new ValidationException(errors.getAllErrors());
120 ServiceOrder serviceOrderSaved =serviceOrderService.createServiceOrder(serviceOrder);
121 serviceOrderService.updateOrderHref(serviceOrderSaved);
122 JsonRepresentation filter = new JsonRepresentation(params);
123 return this.createResponse(serviceOrderSaved, filter);
127 @Scheduled(fixedDelay = 5000)
128 public void scheduleCheckServiceOrders() {
129 List<ServiceOrder> acknowledgedOrders = serviceOrderService.findServiceOrdersByState(StateType.ACKNOWLEDGED);
130 for (ServiceOrder serviceOrder : acknowledgedOrders) {
131 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
132 if (serviceOrderInfo.isServiceOrderRejected()) {
133 serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
134 } else if (serviceOrderInfo.isAllItemsCompleted()) {
135 serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
137 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
138 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
139 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
140 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
141 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);