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.util.MultiValueMap;
37 import org.springframework.validation.Errors;
38 import org.springframework.web.bind.annotation.*;
40 import javax.validation.Valid;
41 import java.util.List;
44 @RequestMapping("/serviceOrder")
45 public class ServiceOrderResource extends ResourceManagement {
48 ServiceOrderService serviceOrderService;
51 CheckOrderConsistenceManager checkOrderConsistenceManager;
54 CreateAAICustomerManager createAAICustomer;
57 CreateAAIServiceTypeManager createAAIServiceType;
60 MongoTemplate mongoTemplate;
63 SOTaskManager serviceOrchestratorManager;
66 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
71 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
72 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
73 @RequestParam MultiValueMap<String, String> params) {
75 ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
76 if (serviceOrder == null) {
77 return ResponseEntity.notFound().build();
80 JsonRepresentation filter = new JsonRepresentation(params);
81 return this.getResponse(serviceOrder, filter);
84 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
85 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
87 Query query = multiCriteriaRequestBuilder.buildRequest(params);
88 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
89 JsonRepresentation filter = new JsonRepresentation(params);
90 long totalCount = serviceOrderService.countServiceOrder();
91 HttpHeaders headers = new HttpHeaders();
92 headers.add("X-Total-Count", String.valueOf(totalCount));
93 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
95 return this.findResponse(serviceOrders, filter, headers);
99 @DeleteMapping(value = "/{serviceOrderId}")
100 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
102 serviceOrderService.deleteServiceOrder(serviceOrderId);
104 return this.deleteResponse();
108 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
109 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
110 @RequestParam MultiValueMap<String, String> params) {
113 if (errors != null && errors.hasErrors()) {
114 throw new ValidationException(errors.getAllErrors());
117 ServiceOrder serviceOrderSaved =serviceOrderService.createServiceOrder(serviceOrder);
118 JsonRepresentation filter = new JsonRepresentation(params);
119 return this.createResponse(serviceOrderSaved, filter);
124 @PutMapping(value = "/test/{serviceOrderId}",consumes = MediaType.APPLICATION_JSON_VALUE)
125 public ResponseEntity<Object> checkServiceOrderRessource(@PathVariable String serviceOrderId,@RequestParam MultiValueMap<String, String> params){
126 ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
127 serviceOrder = checkServiceOrder(serviceOrder);
128 JsonRepresentation filter = new JsonRepresentation(params);
129 return this.createResponse(serviceOrder,filter);
133 public ServiceOrder checkServiceOrder(ServiceOrder serviceOrder) {
134 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
135 if (serviceOrderInfo.isServiceOrderRejected()) {
136 serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
137 } else if (serviceOrderInfo.isAllItemsCompleted()) {
138 serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
140 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
141 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
142 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
143 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
144 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);