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 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;
54 @RequestMapping("/serviceOrder")
56 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
59 ServiceOrderRepository serviceOrderRepository;
62 CheckOrderConsistenceManager checkOrderConsistenceManager;
65 CreateAAICustomerManager createAAICustomer;
68 CreateAAIServiceTypeManager createAAIServiceType;
71 MongoTemplate mongoTemplate;
74 SOTaskManager serviceOrchestratorManager;
77 ServiceOrderInfoRepository serviceOrderInfoRepository;
80 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
83 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
84 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
85 @RequestParam MultiValueMap<String, String> params) {
87 ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
88 if (serviceOrder == null) {
89 return ResponseEntity.notFound().build();
92 JsonRepresentation filter = new JsonRepresentation(params);
93 return this.getResponse(serviceOrder, filter);
96 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
97 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
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()));
107 ResponseEntity<Object> response = this.findResponse(serviceOrders, filter, headers);
112 @DeleteMapping(value = "/{serviceOrderId}")
113 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
115 serviceOrderRepository.delete(serviceOrderId);
117 return this.deleteResponse();
121 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
122 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
123 @RequestParam MultiValueMap<String, String> params) {
126 if (errors != null && errors.hasErrors()) {
127 throw new ValidationException(errors.getAllErrors());
130 serviceOrder.setState(StateType.ACKNOWLEDGED);
131 serviceOrder.setOrderDate(new Date());
132 for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
133 serviceOrderItem.setState(StateType.ACKNOWLEDGED);
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);
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);
154 serviceOrderRepository.save(serviceOrder);
155 createAAICustomer.createAAICustomer(serviceOrderInfo);
156 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
157 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
164 * @param serviceOrder
167 private void changeServiceOrderState(ServiceOrder serviceOrder, StateType stateType) {
168 serviceOrder.setState(stateType);
169 serviceOrder.setCompletionDateTime(new Date());
170 serviceOrderRepository.save(serviceOrder);