306e3a91375ccdd5c1fcffc59c81ff274fcf6f21
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / producer / ProducerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment.controllers.producer;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30
31 import java.lang.invoke.MethodHandles;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35
36 import org.oransc.enrichment.clients.ProducerCallbacks;
37 import org.oransc.enrichment.clients.ProducerJobInfo;
38 import org.oransc.enrichment.controllers.ErrorResponse;
39 import org.oransc.enrichment.controllers.VoidResponse;
40 import org.oransc.enrichment.controllers.producer.ProducerRegistrationInfo.ProducerEiTypeRegistrationInfo;
41 import org.oransc.enrichment.repository.EiJob;
42 import org.oransc.enrichment.repository.EiJobs;
43 import org.oransc.enrichment.repository.EiProducer;
44 import org.oransc.enrichment.repository.EiProducers;
45 import org.oransc.enrichment.repository.EiType;
46 import org.oransc.enrichment.repository.EiTypes;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.MediaType;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.bind.annotation.DeleteMapping;
54 import org.springframework.web.bind.annotation.GetMapping;
55 import org.springframework.web.bind.annotation.PathVariable;
56 import org.springframework.web.bind.annotation.PutMapping;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RestController;
59
60 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
61 @RestController("ProducerController")
62 @Api(tags = {ProducerConsts.PRODUCER_API_NAME})
63 public class ProducerController {
64
65     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
66
67     private static Gson gson = new GsonBuilder().create();
68
69     @Autowired
70     private EiJobs eiJobs;
71
72     @Autowired
73     private EiTypes eiTypes;
74
75     @Autowired
76     private EiProducers eiProducers;
77
78     @Autowired
79     ProducerCallbacks producerCallbacks;
80
81     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
82     @ApiOperation(value = "EI type identifiers", notes = "")
83     @ApiResponses(
84         value = { //
85             @ApiResponse(
86                 code = 200,
87                 message = "EI type identifiers",
88                 response = String.class,
89                 responseContainer = "List"), //
90         })
91     public ResponseEntity<Object> getEiTypeIdentifiers( //
92     ) {
93         List<String> result = new ArrayList<>();
94         for (EiType eiType : this.eiTypes.getAllEiTypes()) {
95             result.add(eiType.getId());
96         }
97
98         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
99     }
100
101     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
102     @ApiOperation(value = "Individual EI type", notes = "")
103     @ApiResponses(
104         value = { //
105             @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
106             @ApiResponse(
107                 code = 404,
108                 message = "Enrichment Information type is not found",
109                 response = ErrorResponse.ErrorInfo.class)})
110     public ResponseEntity<Object> getEiType( //
111         @PathVariable("eiTypeId") String eiTypeId) {
112         try {
113             EiType t = this.eiTypes.getType(eiTypeId);
114             ProducerEiTypeInfo info = toEiTypeInfo(t);
115             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
116         } catch (Exception e) {
117             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
118         }
119     }
120
121     @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
122     @ApiOperation(value = "EI producer identifiers", notes = "")
123     @ApiResponses(
124         value = { //
125             @ApiResponse(
126                 code = 200,
127                 message = "EI producer identifiers",
128                 response = String.class,
129                 responseContainer = "List"), //
130         })
131     public ResponseEntity<Object> getEiProducerIdentifiers( //
132     ) {
133         List<String> result = new ArrayList<>();
134         for (EiProducer eiProducer : this.eiProducers.getAllProducers()) {
135             result.add(eiProducer.getId());
136         }
137
138         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
139     }
140
141     @GetMapping(
142         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
143         produces = MediaType.APPLICATION_JSON_VALUE)
144     @ApiOperation(value = "Individual EI producer", notes = "")
145     @ApiResponses(
146         value = { //
147             @ApiResponse(code = 200, message = "EI jobs", response = ProducerRegistrationInfo.class), //
148             @ApiResponse(
149                 code = 404,
150                 message = "Enrichment Information producer is not found",
151                 response = ErrorResponse.ErrorInfo.class)})
152     public ResponseEntity<Object> getEiProducer( //
153         @PathVariable("eiProducerId") String eiProducerId) {
154         try {
155             EiProducer p = this.eiProducers.getProducer(eiProducerId);
156             ProducerRegistrationInfo info = toEiProducerRegistrationInfo(p);
157             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
158         } catch (Exception e) {
159             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
160         }
161     }
162
163     @GetMapping(
164         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
165         produces = MediaType.APPLICATION_JSON_VALUE)
166     @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer")
167     @ApiResponses(
168         value = { //
169             @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
170             @ApiResponse(
171                 code = 404,
172                 message = "Enrichment Information producer is not found",
173                 response = ErrorResponse.ErrorInfo.class)})
174     public ResponseEntity<Object> getEiProducerJobs( //
175         @PathVariable("eiProducerId") String eiProducerId) {
176         try {
177             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
178             Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
179             for (EiType type : producer.getEiTypes()) {
180                 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
181                     ProducerJobInfo request = new ProducerJobInfo(eiJob);
182                     producerJobs.add(request);
183                 }
184             }
185
186             return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
187         } catch (Exception e) {
188             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
189         }
190     }
191
192     @GetMapping(
193         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
194         produces = MediaType.APPLICATION_JSON_VALUE)
195     @ApiOperation(value = "EI producer status")
196     @ApiResponses(
197         value = { //
198             @ApiResponse(code = 200, message = "EI jobs", response = ProducerStatusInfo.class), //
199             @ApiResponse(
200                 code = 404,
201                 message = "Enrichment Information producer is not found",
202                 response = ErrorResponse.ErrorInfo.class)})
203     public ResponseEntity<Object> getEiProducerStatus( //
204         @PathVariable("eiProducerId") String eiProducerId) {
205         try {
206             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
207             return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
208         } catch (Exception e) {
209             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
210         }
211     }
212
213     private ProducerStatusInfo producerStatusInfo(EiProducer producer) {
214         ProducerStatusInfo.OperationalState opState =
215             producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
216                 : ProducerStatusInfo.OperationalState.DISABLED;
217         return new ProducerStatusInfo(opState);
218     }
219
220     @PutMapping(
221         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
222         produces = MediaType.APPLICATION_JSON_VALUE)
223     @ApiOperation(value = "Individual EI producer", notes = "")
224     @ApiResponses(
225         value = { //
226             @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), //
227             @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)}//
228     )
229     public ResponseEntity<Object> putEiProducer( //
230         @PathVariable("eiProducerId") String eiProducerId, //
231         @RequestBody ProducerRegistrationInfo registrationInfo) {
232         try {
233             EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
234             if (previousDefinition != null) {
235                 for (EiType type : previousDefinition.getEiTypes()) {
236                     type.removeProducer(previousDefinition);
237                 }
238             }
239
240             registerProducer(eiProducerId, registrationInfo);
241             if (previousDefinition != null) {
242                 purgeTypes(previousDefinition.getEiTypes());
243             }
244
245             return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
246         } catch (Exception e) {
247             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
248         }
249     }
250
251     private void purgeTypes(Collection<EiType> types) {
252         for (EiType type : types) {
253             if (type.getProducerIds().isEmpty()) {
254                 this.eiTypes.deregisterType(type, this.eiJobs);
255             }
256         }
257     }
258
259     @DeleteMapping(
260         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
261         produces = MediaType.APPLICATION_JSON_VALUE)
262     @ApiOperation(value = "Individual EI producer", notes = "")
263     @ApiResponses(
264         value = { //
265             @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class),
266             @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class),
267             @ApiResponse(code = 404, message = "Producer is not found", response = ErrorResponse.ErrorInfo.class)})
268     public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
269         try {
270             final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
271             this.eiProducers.deregisterProducer(producer, this.eiTypes, this.eiJobs);
272             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
273         } catch (Exception e) {
274             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
275         }
276     }
277
278     private EiType registerType(ProducerEiTypeRegistrationInfo typeInfo) {
279         EiType type = this.eiTypes.get(typeInfo.eiTypeId);
280         if (type == null) {
281             type = new EiType(typeInfo.eiTypeId, typeInfo.jobDataSchema);
282             this.eiTypes.put(type);
283         }
284         return type;
285     }
286
287     EiProducer createProducer(Collection<EiType> types, String producerId, ProducerRegistrationInfo registrationInfo) {
288         return new EiProducer(producerId, types, registrationInfo.jobCreationCallbackUrl,
289             registrationInfo.jobDeletionCallbackUrl, registrationInfo.producerSupervisionCallbackUrl);
290     }
291
292     private EiProducer registerProducer(String producerId, ProducerRegistrationInfo registrationInfo) {
293         ArrayList<EiType> types = new ArrayList<>();
294         for (ProducerEiTypeRegistrationInfo typeInfo : registrationInfo.types) {
295             types.add(registerType(typeInfo));
296         }
297         EiProducer producer = createProducer(types, producerId, registrationInfo);
298         this.eiProducers.put(producer);
299
300         for (EiType type : types) {
301             for (EiJob job : this.eiJobs.getJobsForType(type)) {
302                 this.producerCallbacks.notifyProducerJobStarted(producer, job) //
303                     .subscribe();
304             }
305             type.addProducer(producer);
306         }
307         return producer;
308     }
309
310     ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
311         Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
312         for (EiType type : p.getEiTypes()) {
313             types.add(toEiTypeRegistrationInfo(type));
314         }
315         return new ProducerRegistrationInfo(types, p.getJobCreationCallbackUrl(), p.getJobDeletionCallbackUrl(),
316             p.getProducerSupervisionCallbackUrl());
317     }
318
319     private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) {
320         return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId());
321     }
322
323     private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
324         return new ProducerEiTypeInfo(t.getJobDataSchema(), t.getProducerIds());
325     }
326 }