2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
14 package org.onap.nbi.apis.serviceorder;
16 import java.util.List;
17 import java.util.Optional;
18 import javax.validation.Valid;
19 import org.onap.nbi.OnapComponentsUrlPaths;
20 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIOwningEntityManager;
21 import org.onap.nbi.commons.EWInterfaceUtils;
22 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
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.service.ServiceOrderService;
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.MultiCriteriaRequestBuilder;
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.util.MultiValueMap;
41 import org.springframework.validation.Errors;
42 import org.springframework.web.bind.annotation.DeleteMapping;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestHeader;
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(OnapComponentsUrlPaths.SERVICE_ORDER_PATH)
55 public class ServiceOrderResource extends ResourceManagement {
58 ServiceOrderService serviceOrderService;
61 CheckOrderConsistenceManager checkOrderConsistenceManager;
64 CreateAAICustomerManager createAAICustomer;
67 CreateAAIOwningEntityManager createAAIOwningEntityManager;
70 CreateAAIServiceTypeManager createAAIServiceType;
73 MongoTemplate mongoTemplate;
76 SOTaskManager serviceOrchestratorManager;
79 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
82 EWInterfaceUtils eWInterfaceUtils;
84 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
85 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
86 @RequestParam MultiValueMap<String, String> params,
87 @RequestHeader(value = "Target", required = false) String targetUrl) {
88 if (targetUrl != null) {
89 targetUrl = targetUrl + OnapComponentsUrlPaths.SERVICE_ORDER_PATH + "/" + serviceOrderId;
90 return eWInterfaceUtils.callGetRequestTarget(targetUrl);
92 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
93 if (!optionalServiceOrder.isPresent()) {
94 return ResponseEntity.notFound().build();
96 JsonRepresentation filter = new JsonRepresentation(params);
97 return this.getResponse(optionalServiceOrder.get(), filter);
102 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
103 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
105 Query query = multiCriteriaRequestBuilder.buildRequest(params);
106 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
107 JsonRepresentation filter = new JsonRepresentation(params);
108 long totalCount = serviceOrderService.countServiceOrder();
109 HttpHeaders headers = new HttpHeaders();
110 headers.add("X-Total-Count", String.valueOf(totalCount));
111 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
113 return this.findResponse(serviceOrders, filter, headers);
117 @DeleteMapping(value = "/{serviceOrderId}")
118 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
120 serviceOrderService.deleteServiceOrder(serviceOrderId);
122 return this.deleteResponse();
126 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
127 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
128 @RequestParam MultiValueMap<String, String> params,
129 @RequestHeader(value = "Target", required = false) String targetUrl) {
130 if (targetUrl != null) {
131 targetUrl = targetUrl + OnapComponentsUrlPaths.SERVICE_ORDER_PATH;
132 return eWInterfaceUtils.callPostRequestTarget(serviceOrder, targetUrl);
134 if (errors != null && errors.hasErrors()) {
135 throw new ValidationException(errors.getAllErrors());
139 ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
140 JsonRepresentation filter = new JsonRepresentation(params);
141 return this.createResponse(serviceOrderSaved, filter);
145 @PutMapping(value = "/test/{serviceOrderId}", consumes = MediaType.APPLICATION_JSON_VALUE)
146 public ResponseEntity<Object> checkServiceOrderRessource(@PathVariable String serviceOrderId,
147 @RequestParam MultiValueMap<String, String> params) {
148 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
149 if (!optionalServiceOrder.isPresent()) {
150 return ResponseEntity.notFound().build();
152 ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
153 JsonRepresentation filter = new JsonRepresentation(params);
154 return this.createResponse(serviceOrder, filter);
157 public ServiceOrder checkServiceOrder(ServiceOrder serviceOrder) {
158 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
159 if (serviceOrderInfo.isServiceOrderRejected()) {
160 serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
161 } else if (serviceOrderInfo.isAllItemsCompleted()) {
162 serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
164 createAAICustomer.createAAICustomer(serviceOrder, serviceOrderInfo);
165 createAAIOwningEntityManager.createAAIOwningEntity(serviceOrder, serviceOrderInfo);
167 if (StateType.ACKNOWLEDGED == serviceOrder.getState()) {
168 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
169 if (StateType.ACKNOWLEDGED == serviceOrder.getState()) {
170 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
171 serviceOrderService.updateOrderState(serviceOrder, StateType.INPROGRESS_TASK_CREATED);