2cd1c9a24ec438dae6dab821116d1a02fba8305f
[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");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.apis.serviceorder;
17
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
19 import org.onap.nbi.apis.serviceorder.model.StateType;
20 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
21 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
22 import org.onap.nbi.apis.serviceorder.workflow.CheckOrderConsistenceManager;
23 import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager;
24 import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager;
25 import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager;
26 import org.onap.nbi.commons.JsonRepresentation;
27 import org.onap.nbi.commons.MultiCriteriaRequestBuilder;
28 import org.onap.nbi.commons.ResourceManagement;
29 import org.onap.nbi.exceptions.ValidationException;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.data.mongodb.core.MongoTemplate;
32 import org.springframework.data.mongodb.core.query.Query;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.MediaType;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.util.MultiValueMap;
37 import org.springframework.validation.Errors;
38 import org.springframework.web.bind.annotation.*;
39
40 import javax.validation.Valid;
41 import java.util.List;
42
43 @RestController
44 @RequestMapping("/serviceOrder")
45 public class ServiceOrderResource extends ResourceManagement {
46
47     @Autowired
48     ServiceOrderService serviceOrderService;
49
50     @Autowired
51     CheckOrderConsistenceManager checkOrderConsistenceManager;
52
53     @Autowired
54     CreateAAICustomerManager createAAICustomer;
55
56     @Autowired
57     CreateAAIServiceTypeManager createAAIServiceType;
58
59     @Autowired
60     MongoTemplate mongoTemplate;
61
62     @Autowired
63     SOTaskManager serviceOrchestratorManager;
64
65     @Autowired
66     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
67
68
69
70
71     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
72     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
73             @RequestParam MultiValueMap<String, String> params) {
74
75         ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
76         if (serviceOrder == null) {
77             return ResponseEntity.notFound().build();
78         }
79
80         JsonRepresentation filter = new JsonRepresentation(params);
81         return this.getResponse(serviceOrder, filter);
82     }
83
84     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
85     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
86
87         Query query = multiCriteriaRequestBuilder.buildRequest(params);
88         List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
89         JsonRepresentation filter = new JsonRepresentation(params);
90         long totalCount = serviceOrderService.countServiceOrder();
91         HttpHeaders headers = new HttpHeaders();
92         headers.add("X-Total-Count", String.valueOf(totalCount));
93         headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
94
95         return this.findResponse(serviceOrders, filter, headers);
96
97     }
98
99     @DeleteMapping(value = "/{serviceOrderId}")
100     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
101
102         serviceOrderService.deleteServiceOrder(serviceOrderId);
103
104         return this.deleteResponse();
105
106     }
107
108     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
109     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
110             @RequestParam MultiValueMap<String, String> params) {
111
112
113         if (errors != null && errors.hasErrors()) {
114             throw new ValidationException(errors.getAllErrors());
115         }
116
117         ServiceOrder serviceOrderSaved =serviceOrderService.createServiceOrder(serviceOrder);
118         JsonRepresentation filter = new JsonRepresentation(params);
119         return this.createResponse(serviceOrderSaved, filter);
120
121     }
122
123
124     @PutMapping(value = "/test/{serviceOrderId}",consumes = MediaType.APPLICATION_JSON_VALUE)
125     public ResponseEntity<Object> checkServiceOrderRessource(@PathVariable String serviceOrderId,@RequestParam MultiValueMap<String, String> params){
126         ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
127         serviceOrder = checkServiceOrder(serviceOrder);
128         JsonRepresentation filter = new JsonRepresentation(params);
129         return this.createResponse(serviceOrder,filter);
130     }
131
132
133     public ServiceOrder checkServiceOrder(ServiceOrder serviceOrder) {
134         ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
135         if (serviceOrderInfo.isServiceOrderRejected()) {
136             serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
137         } else if (serviceOrderInfo.isAllItemsCompleted()) {
138             serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
139         } else {
140             createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
141             if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
142                 createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
143                 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
144                     serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
145                 }
146             }
147
148         }
149         return serviceOrder;
150     }
151
152 }