Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / ServiceOrderResource.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12  */
13
14 package org.onap.nbi.apis.serviceorder;
15
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;
52
53 @RestController
54 @RequestMapping(OnapComponentsUrlPaths.SERVICE_ORDER_PATH)
55 public class ServiceOrderResource extends ResourceManagement {
56
57     @Autowired
58     ServiceOrderService serviceOrderService;
59
60     @Autowired
61     CheckOrderConsistenceManager checkOrderConsistenceManager;
62
63     @Autowired
64     CreateAAICustomerManager createAAICustomer;
65
66     @Autowired
67     CreateAAIOwningEntityManager createAAIOwningEntityManager;
68
69     @Autowired
70     CreateAAIServiceTypeManager createAAIServiceType;
71
72     @Autowired
73     MongoTemplate mongoTemplate;
74
75     @Autowired
76     SOTaskManager serviceOrchestratorManager;
77
78     @Autowired
79     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
80
81     @Autowired
82     EWInterfaceUtils eWInterfaceUtils;
83
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);
91         } else {
92             Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
93             if (!optionalServiceOrder.isPresent()) {
94                 return ResponseEntity.notFound().build();
95             } else {
96                 JsonRepresentation filter = new JsonRepresentation(params);
97                 return this.getResponse(optionalServiceOrder.get(), filter);
98             }
99         }
100     }
101
102     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
103     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
104
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()));
112
113         return this.findResponse(serviceOrders, filter, headers);
114
115     }
116
117     @DeleteMapping(value = "/{serviceOrderId}")
118     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
119
120         serviceOrderService.deleteServiceOrder(serviceOrderId);
121
122         return this.deleteResponse();
123
124     }
125
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);
133         } else {
134             if (errors != null && errors.hasErrors()) {
135                 throw new ValidationException(errors.getAllErrors());
136             }
137         }
138
139         ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
140         JsonRepresentation filter = new JsonRepresentation(params);
141         return this.createResponse(serviceOrderSaved, filter);
142
143     }
144
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();
151         }
152         ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
153         JsonRepresentation filter = new JsonRepresentation(params);
154         return this.createResponse(serviceOrder, filter);
155     }
156
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);
163         } else {
164             createAAICustomer.createAAICustomer(serviceOrder, serviceOrderInfo);
165             createAAIOwningEntityManager.createAAIOwningEntity(serviceOrder, serviceOrderInfo);
166
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);
172                 }
173             }
174
175         }
176         return serviceOrder;
177     }
178
179 }