HUB Resource
[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.scheduling.annotation.EnableScheduling;
37 import org.springframework.scheduling.annotation.Scheduled;
38 import org.springframework.util.MultiValueMap;
39 import org.springframework.validation.Errors;
40 import org.springframework.web.bind.annotation.*;
41
42 import javax.validation.Valid;
43 import java.util.List;
44
45 @RestController
46 @RequestMapping("/serviceOrder")
47 @EnableScheduling
48 public class ServiceOrderResource extends ResourceManagement {
49
50
51
52     @Autowired
53     ServiceOrderService serviceOrderService;
54
55     @Autowired
56     CheckOrderConsistenceManager checkOrderConsistenceManager;
57
58     @Autowired
59     CreateAAICustomerManager createAAICustomer;
60
61     @Autowired
62     CreateAAIServiceTypeManager createAAIServiceType;
63
64     @Autowired
65     MongoTemplate mongoTemplate;
66
67     @Autowired
68     SOTaskManager serviceOrchestratorManager;
69
70     @Autowired
71     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
72
73
74     @GetMapping(value = "/{serviceOrderId}", produces = MediaType.APPLICATION_JSON_VALUE)
75     public ResponseEntity<Object> getServiceOrder(@PathVariable String serviceOrderId,
76             @RequestParam MultiValueMap<String, String> params) {
77
78         ServiceOrder serviceOrder = serviceOrderService.findServiceOrderById(serviceOrderId);
79         if (serviceOrder == null) {
80             return ResponseEntity.notFound().build();
81         }
82
83         JsonRepresentation filter = new JsonRepresentation(params);
84         return this.getResponse(serviceOrder, filter);
85     }
86
87     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
88     public ResponseEntity<Object> findServiceOrder(@RequestParam MultiValueMap<String, String> params) {
89
90         Query query = multiCriteriaRequestBuilder.buildRequest(params);
91         List<ServiceOrder> serviceOrders = mongoTemplate.find(query, ServiceOrder.class);
92         JsonRepresentation filter = new JsonRepresentation(params);
93         long totalCount = serviceOrderService.countServiceOrder();
94         HttpHeaders headers = new HttpHeaders();
95         headers.add("X-Total-Count", String.valueOf(totalCount));
96         headers.add("X-Result-Count", String.valueOf(serviceOrders.size()));
97
98         return this.findResponse(serviceOrders, filter, headers);
99
100     }
101
102     @DeleteMapping(value = "/{serviceOrderId}")
103     public ResponseEntity<Object> deleteServiceOrder(@PathVariable String serviceOrderId) {
104
105         serviceOrderService.deleteServiceOrder(serviceOrderId);
106
107         return this.deleteResponse();
108
109     }
110
111     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
112     public ResponseEntity<Object> createServiceOrder(@Valid @RequestBody ServiceOrder serviceOrder, Errors errors,
113             @RequestParam MultiValueMap<String, String> params) {
114
115
116         if (errors != null && errors.hasErrors()) {
117             throw new ValidationException(errors.getAllErrors());
118         }
119
120         ServiceOrder serviceOrderSaved =serviceOrderService.createServiceOrder(serviceOrder);
121         serviceOrderService.updateOrderHref(serviceOrderSaved);
122         JsonRepresentation filter = new JsonRepresentation(params);
123         return this.createResponse(serviceOrderSaved, filter);
124
125     }
126
127     @Scheduled(fixedDelay = 5000)
128     public void scheduleCheckServiceOrders() {
129         List<ServiceOrder> acknowledgedOrders = serviceOrderService.findServiceOrdersByState(StateType.ACKNOWLEDGED);
130         for (ServiceOrder serviceOrder : acknowledgedOrders) {
131             ServiceOrderInfo serviceOrderInfo = checkOrderConsistenceManager.checkServiceOrder(serviceOrder);
132             if (serviceOrderInfo.isServiceOrderRejected()) {
133                 serviceOrderService.updateOrderState(serviceOrder, StateType.REJECTED);
134             } else if (serviceOrderInfo.isAllItemsCompleted()) {
135                 serviceOrderService.updateOrderState(serviceOrder, StateType.COMPLETED);
136             } else {
137                 createAAICustomer.createAAICustomer(serviceOrder,serviceOrderInfo);
138                 if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
139                     createAAIServiceType.createAAIServiceType(serviceOrder, serviceOrderInfo);
140                     if(StateType.ACKNOWLEDGED==serviceOrder.getState()) {
141                         serviceOrchestratorManager.registerServiceOrder(serviceOrder, serviceOrderInfo);
142                     }
143                 }
144
145             }
146         }
147     }
148
149 }