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.ServiceOrderRepository;
26 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
27 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
28 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
29 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
30 import org.onap.nbi.commons.JsonRepresentation;
31 import org.onap.nbi.commons.ResourceManagement;
32 import org.onap.nbi.exceptions.ValidationException;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.data.mongodb.core.MongoTemplate;
35 import org.springframework.data.mongodb.core.query.Query;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.scheduling.annotation.EnableScheduling;
40 import org.springframework.scheduling.annotation.Scheduled;
41 import org.springframework.util.MultiValueMap;
42 import org.springframework.validation.Errors;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
50 import org.springframework.web.bind.annotation.RestController;
53 @RequestMapping("/serviceOrder")
55 public class ServiceOrderResource extends ResourceManagement<ServiceOrder> {
58 ServiceOrderRepository serviceOrderRepository;
61 CheckOrderConsistenceManager checkOrderConsistenceManager;
64 CreateAAICustomerManager createAAICustomer;
67 CreateAAIServiceTypeManager createAAIServiceType;
70 MongoTemplate mongoTemplate;
73 SOTaskManager serviceOrchestratorManager;
76 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
79 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
80 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
81 @RequestParam MultiValueMap<String, String> params) {
83 ServiceOrder serviceOrder = serviceOrderRepository.findOne(serviceOrderId);
84 if (serviceOrder == null) {
85 return ResponseEntity.notFound().build();
88 JsonRepresentation filter = new JsonRepresentation(params);
89 return this.getResponse(serviceOrder, filter);
92 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
93 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
95 Query query = multiCriteriaRequestBuilder.buildRequest(params);
96 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
97 JsonRepresentation filter = new JsonRepresentation(params);
98 long totalCount = serviceOrderRepository.count();
99 HttpHeaders headers = new HttpHeaders();
100 headers.add("X-Total-Count", String.valueOf(totalCount));
101 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
103 return this.findResponse(serviceOrders, filter, headers);
107 @DeleteMapping(value = "/{serviceOrderId}")
108 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
110 serviceOrderRepository.delete(serviceOrderId);
112 return this.deleteResponse();
116 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
117 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
118 @RequestParam MultiValueMap<String, String> params) {
121 if (errors != null && errors.hasErrors()) {
122 throw new ValidationException(errors.getAllErrors());
125 serviceOrder.setState(StateType.ACKNOWLEDGED);
126 serviceOrder.setOrderDate(new Date());
127 for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
128 serviceOrderItem.setState(StateType.ACKNOWLEDGED);
131 ServiceOrder serviceOrderSaved = serviceOrderRepository.save(serviceOrder);
132 serviceOrderSaved.setHref("serviceOrder/" + serviceOrderSaved.getId());
133 serviceOrderRepository.save(serviceOrderSaved);
134 JsonRepresentation filter = new JsonRepresentation(params);
135 return this.createResponse(serviceOrderSaved, filter);
139 @Scheduled(fixedDelay = 5000)
140 public void scheduleCheckServiceOrders() {
141 List<ServiceOrder> acknowledgedOrders = serviceOrderRepository.findByState(StateType.ACKNOWLEDGED);
142 for (ServiceOrder serviceOrder : acknowledgedOrders) {
143 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
144 if (serviceOrderInfo.isServiceOrderRejected()) {
145 changeServiceOrderState(serviceOrder, StateType.REJECTED);
146 } else if (serviceOrderInfo.isAllItemsCompleted()) {
147 changeServiceOrderState(serviceOrder, StateType.COMPLETED);
149 serviceOrderRepository.save(serviceOrder);
150 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
151 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
152 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
153 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
154 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);