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