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.ResourceManagement;
28 import org.onap.nbi.exceptions.ValidationException;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.data.mongodb.core.MongoTemplate;
31 import org.springframework.data.mongodb.core.query.Query;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.scheduling.annotation.EnableScheduling;
36 import org.springframework.scheduling.annotation.Scheduled;
37 import org.springframework.util.MultiValueMap;
38 import org.springframework.validation.Errors;
39 import org.springframework.web.bind.annotation.*;
41 import javax.validation.Valid;
42 import java.util.List;
45 @RequestMapping("/serviceOrder")
47 public class ServiceOrderResource extends ResourceManagement {
52 ServiceOrderService serviceOrderService;
55 CheckOrderConsistenceManager checkOrderConsistenceManager;
58 CreateAAICustomerManager createAAICustomer;
61 CreateAAIServiceTypeManager createAAIServiceType;
64 MongoTemplate mongoTemplate;
67 SOTaskManager serviceOrchestratorManager;
70 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
73 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
74 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
75 @RequestParam MultiValueMap<String, String> params) {
77 ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
78 if (serviceOrder == null) {
79 return ResponseEntity.notFound().build();
82 JsonRepresentation filter = new JsonRepresentation(params);
83 return this.getResponse(serviceOrder, filter);
86 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
87 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
89 Query query = multiCriteriaRequestBuilder.buildRequest(params);
90 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
91 JsonRepresentation filter = new JsonRepresentation(params);
92 long totalCount = serviceOrderService.countServiceOrder();
93 HttpHeaders headers = new HttpHeaders();
94 headers.add("X-Total-Count", String.valueOf(totalCount));
95 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
97 return this.findResponse(serviceOrders, filter, headers);
101 @DeleteMapping(value = "/{serviceOrderId}")
102 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
104 serviceOrderService.deleteServiceOrder(serviceOrderId);
106 return this.deleteResponse();
110 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
111 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
112 @RequestParam MultiValueMap<String, String> params) {
115 if (errors != null && errors.hasErrors()) {
116 throw new ValidationException(errors.getAllErrors());
119 ServiceOrder serviceOrderSaved =serviceOrderService.createServiceOrder(serviceOrder);
120 serviceOrderService.updateOrderHref(serviceOrderSaved);
121 JsonRepresentation filter = new JsonRepresentation(params);
122 return this.createResponse(serviceOrderSaved, filter);
126 @Scheduled(fixedDelay = 5000)
127 public void scheduleCheckServiceOrders() {
128 List<ServiceOrder> acknowledgedOrders = serviceOrderService.findServiceOrdersByState(StateType.ACKNOWLEDGED);
129 for (ServiceOrder serviceOrder : acknowledgedOrders) {
130 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
131 if (serviceOrderInfo.isServiceOrderRejected()) {
132 serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
133 } else if (serviceOrderInfo.isAllItemsCompleted()) {
134 serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
136 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
137 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
138 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
139 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
140 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);