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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.data.mongodb.core.MongoTemplate;
38 import org.springframework.data.mongodb.core.query.Query;
39 import org.springframework.http.HttpHeaders;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
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.PutMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestHeader;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RequestParam;
53 import org.springframework.web.bind.annotation.RestController;
56 @RequestMapping(OnapComponentsUrlPaths.SERVICE_ORDER_PATH)
57 public class ServiceOrderResource extends ResourceManagement {
59 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class);
62 ServiceOrderService serviceOrderService;
65 CheckOrderConsistenceManager checkOrderConsistenceManager;
68 CreateAAICustomerManager createAAICustomer;
71 CreateAAIOwningEntityManager createAAIOwningEntityManager;
74 CreateAAIServiceTypeManager createAAIServiceType;
77 MongoTemplate mongoTemplate;
80 SOTaskManager serviceOrchestratorManager;
83 MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
86 EWInterfaceUtils eWInterfaceUtils;
88 @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
89 public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
90 @RequestParam MultiValueMap<String, String> params,
91 @RequestHeader(value = "Target", required = false) String targetUrl) {
92 if (targetUrl != null) {
93 targetUrl = targetUrl + OnapComponentsUrlPaths.SERVICE_ORDER_PATH + "/" + serviceOrderId;
94 return eWInterfaceUtils.callGetRequestTarget(targetUrl);
96 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
97 if (!optionalServiceOrder.isPresent()) {
98 return ResponseEntity.notFound().build();
100 JsonRepresentation filter = new JsonRepresentation(params);
101 return this.getResponse(optionalServiceOrder.get(), filter);
106 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
107 public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
109 Query query = multiCriteriaRequestBuilder.buildRequest(params);
110 List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
111 JsonRepresentation filter = new JsonRepresentation(params);
112 long totalCount = serviceOrderService.countServiceOrder();
113 HttpHeaders headers = new HttpHeaders();
114 headers.add("X-Total-Count", String.valueOf(totalCount));
115 headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
117 return this.findResponse(serviceOrders, filter, headers);
121 @DeleteMapping(value = "/{serviceOrderId}")
122 public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
124 serviceOrderService.deleteServiceOrder(serviceOrderId);
126 return this.deleteResponse();
130 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
131 public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
132 @RequestParam MultiValueMap<String, String> params,
133 @RequestHeader(value = "Target", required = false) String targetUrl) {
134 if (targetUrl != null) {
135 targetUrl = targetUrl + OnapComponentsUrlPaths.SERVICE_ORDER_PATH;
136 return eWInterfaceUtils.callPostRequestTarget(serviceOrder, targetUrl);
138 if (errors != null && errors.hasErrors()) {
139 throw new ValidationException(errors.getAllErrors());
143 ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
144 JsonRepresentation filter = new JsonRepresentation(params);
145 return this.createResponse(serviceOrderSaved, filter);
149 @PutMapping(value = "/test/{serviceOrderId}", consumes = MediaType.APPLICATION_JSON_VALUE)
150 public ResponseEntity<Object> checkServiceOrderRessource(@PathVariable String serviceOrderId,
151 @RequestParam MultiValueMap<String, String> params) {
152 Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
153 if (!optionalServiceOrder.isPresent()) {
154 return ResponseEntity.notFound().build();
156 ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
157 JsonRepresentation filter = new JsonRepresentation(params);
158 return this.createResponse(serviceOrder, filter);
161 public ServiceOrder checkServiceOrder(ServiceOrder serviceOrder) {
162 LOGGER.debug("Checking Service order : {} ", serviceOrder.getId());
163 ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
164 if (serviceOrderInfo.isServiceOrderRejected()) {
165 serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
166 } else if (serviceOrderInfo.isAllItemsCompleted()) {
167 serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
169 createAAICustomer.createAAICustomer(serviceOrder, serviceOrderInfo);
170 createAAIOwningEntityManager.createAAIOwningEntity(serviceOrder, serviceOrderInfo);
172 if (StateType.ACKNOWLEDGED == serviceOrder.getState()) {
173 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
174 if (StateType.ACKNOWLEDGED == serviceOrder.getState()) {
175 serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
176 serviceOrderService.updateOrderState(serviceOrder, StateType.INPROGRESS_TASK_CREATED);