Chore: Fix typo in INFO
[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.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;
54
55 @RestController
56 @RequestMapping(OnapComponentsUrlPaths.SERVICE_ORDER_PATH)
57 public class ServiceOrderResource extends ResourceManagement {
58
59         private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class);
60
61     @Autowired
62     ServiceOrderService serviceOrderService;
63
64     @Autowired
65     CheckOrderConsistenceManager checkOrderConsistenceManager;
66
67     @Autowired
68     CreateAAICustomerManager createAAICustomer;
69
70     @Autowired
71     CreateAAIOwningEntityManager createAAIOwningEntityManager;
72
73     @Autowired
74     CreateAAIServiceTypeManager createAAIServiceType;
75
76     @Autowired
77     MongoTemplate mongoTemplate;
78
79     @Autowired
80     SOTaskManager serviceOrchestratorManager;
81
82     @Autowired
83     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
84
85     @Autowired
86     EWInterfaceUtils eWInterfaceUtils;
87
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);
95         } else {
96             Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
97             if (!optionalServiceOrder.isPresent()) {
98                 return ResponseEntity.notFound().build();
99             } else {
100                 JsonRepresentation filter = new JsonRepresentation(params);
101                 return this.getResponse(optionalServiceOrder.get(), filter);
102             }
103         }
104     }
105
106     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
107     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
108
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()));
116
117         return this.findResponse(serviceOrders, filter, headers);
118
119     }
120
121     @DeleteMapping(value = "/{serviceOrderId}")
122     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
123
124         serviceOrderService.deleteServiceOrder(serviceOrderId);
125
126         return this.deleteResponse();
127
128     }
129
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);
137         } else {
138             if (errors != null && errors.hasErrors()) {
139                 throw new ValidationException(errors.getAllErrors());
140             }
141         }
142
143         ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
144         JsonRepresentation filter = new JsonRepresentation(params);
145         return this.createResponse(serviceOrderSaved, filter);
146
147     }
148
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();
155         }
156         ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
157         JsonRepresentation filter = new JsonRepresentation(params);
158         return this.createResponse(serviceOrder, filter);
159     }
160
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);
168         } else {
169             createAAICustomer.createAAICustomer(serviceOrder, serviceOrderInfo);
170             createAAIOwningEntityManager.createAAIOwningEntity(serviceOrder, serviceOrderInfo);
171
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);
177                 }
178             }
179
180         }
181         return serviceOrder;
182     }
183
184 }