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.
13 package org.onap.nbi.apis.serviceorder;
15 import java.util.List;
16 import java.util.Optional;
17 import javax.validation.Valid;
18 import org.onap.nbi.OnapComponentsUrlPaths;
19 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIOwningEntityManager;
20 import org.onap.nbi.commons.EWInterfaceUtils;
21 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
22 import org.onap.nbi.apis.serviceorder.model.StateType;
23 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
24 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
25 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
26 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
27 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
28 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
29 import org.onap.nbi.commons.JsonRepresentation;
30 import org.onap.nbi.commons.MultiCriteriaRequestBuilder;
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.util.MultiValueMap;
40 import org.springframework.validation.Errors;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.PutMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestHeader;
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(OnapComponentsUrlPaths.SERVICE_ORDER_PATH)
54 public class ServiceOrderResource extends ResourceManagement {
57 ServiceOrderService serviceOrderService;
60 CheckOrderConsistenceManager checkOrderConsistenceManager;
63 CreateAAICustomerManager createAAICustomer;
66 CreateAAIOwningEntityManager createAAIOwningEntityManager;
69 CreateAAIServiceTypeManager createAAIServiceType;
72 MongoTemplate mongoTemplate;
75 SOTaskManager serviceOrchestratorManager;
78 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
81 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,@RequestHeader(value="Target",required = false)String targetUrl) {
87 if (targetUrl != null) {
88 targetUrl = targetUrl + OnapComponentsUrlPaths.SERVICE_ORDER_PATH + "/" + serviceOrderId;
89 return eWInterfaceUtils.callGetRequestTarget(targetUrl);
91 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
92 if (!optionalServiceOrder.isPresent()) {
93 return ResponseEntity.notFound().build();
95 JsonRepresentation filter = new JsonRepresentation(params);
96 return this.getResponse(optionalServiceOrder.get(), filter);
101 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
102 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
104 Query query = multiCriteriaRequestBuilder.buildRequest(params);
105 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
106 JsonRepresentation filter = new JsonRepresentation(params);
107 long totalCount = serviceOrderService.countServiceOrder();
108 HttpHeaders headers = new HttpHeaders();
109 headers.add("X-Total-Count", String.valueOf(totalCount));
110 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
112 return this.findResponse(serviceOrders, filter, headers);
116 @DeleteMapping(value = "/{serviceOrderId}")
117 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
119 serviceOrderService.deleteServiceOrder(serviceOrderId);
121 return this.deleteResponse();
125 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
126 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
127 @RequestParam MultiValueMap<String, String> params, @RequestHeader(value="Target",required = false)String targetUrl) {
128 if (targetUrl != null) {
129 targetUrl = targetUrl + OnapComponentsUrlPaths.SERVICE_ORDER_PATH;
130 return eWInterfaceUtils.callPostRequestTarget(serviceOrder, targetUrl);
132 if (errors != null && errors.hasErrors()) {
133 throw new ValidationException(errors.getAllErrors());
137 ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
138 JsonRepresentation filter = new JsonRepresentation(params);
139 return this.createResponse(serviceOrderSaved, filter);
144 @PutMapping(value = "/test/{serviceOrderId}", consumes = MediaType.APPLICATION_JSON_VALUE)
145 public ResponseEntity<Object> checkServiceOrderRessource(@PathVariable String serviceOrderId,
146 @RequestParam MultiValueMap<String, String> params) {
147 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
148 if (!optionalServiceOrder.isPresent()) {
149 return ResponseEntity.notFound().build();
151 ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
152 JsonRepresentation filter = new JsonRepresentation(params);
153 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);