c48965aacd7b789e5ee48f0a96b496a3b2e8998b
[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 package org.onap.nbi.apis.serviceorder;
14
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;
51
52 @RestController
53 @RequestMapping(OnapComponentsUrlPaths.SERVICE_ORDER_PATH)
54 public class ServiceOrderResource extends ResourceManagement {
55
56     @Autowired
57     ServiceOrderService serviceOrderService;
58
59     @Autowired
60     CheckOrderConsistenceManager checkOrderConsistenceManager;
61
62     @Autowired
63     CreateAAICustomerManager createAAICustomer;
64
65     @Autowired
66     CreateAAIOwningEntityManager createAAIOwningEntityManager;
67
68     @Autowired
69     CreateAAIServiceTypeManager createAAIServiceType;
70
71     @Autowired
72     MongoTemplate mongoTemplate;
73
74     @Autowired
75     SOTaskManager serviceOrchestratorManager;
76
77     @Autowired
78     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
79
80     @Autowired
81     EWInterfaceUtils eWInterfaceUtils;
82
83
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);
90         } else {
91             Optional<ServiceOrder> optionalServiceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
92             if (!optionalServiceOrder.isPresent()) {
93                 return ResponseEntity.notFound().build();
94             } else {
95                 JsonRepresentation filter = new JsonRepresentation(params);
96                 return this.getResponse(optionalServiceOrder.get(), filter);
97             }
98         }
99     }
100
101     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
102     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
103
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()));
111
112         return this.findResponse(serviceOrders, filter, headers);
113
114     }
115
116     @DeleteMapping(value = "/{serviceOrderId}")
117     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
118
119         serviceOrderService.deleteServiceOrder(serviceOrderId);
120
121         return this.deleteResponse();
122
123     }
124
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);
131         } else {
132             if (errors != null && errors.hasErrors()) {
133                 throw new ValidationException(errors.getAllErrors());
134             }
135         }
136
137         ServiceOrder serviceOrderSaved = serviceOrderService.createServiceOrder(serviceOrder);
138         JsonRepresentation filter = new JsonRepresentation(params);
139         return this.createResponse(serviceOrderSaved, filter);
140
141     }
142
143
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();
150         }
151         ServiceOrder serviceOrder = checkServiceOrder(optionalServiceOrder.get());
152         JsonRepresentation filter = new JsonRepresentation(params);
153         return this.createResponse(serviceOrder, filter);
154     }
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 }