e773117a1805fb7e94339c6feebf5475cf258418
[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.controllers.ErrorResponse;
37 import org.oransc.enrichment.controllers.VoidResponse;
38 import org.oransc.enrichment.controllers.producer.ProducerRegistrationInfo.ProducerEiTypeRegistrationInfo;
39 import org.oransc.enrichment.repository.EiJob;
40 import org.oransc.enrichment.repository.EiJobs;
41 import org.oransc.enrichment.repository.EiProducer;
42 import org.oransc.enrichment.repository.EiProducers;
43 import org.oransc.enrichment.repository.EiType;
44 import org.oransc.enrichment.repository.EiTypes;
45 import org.oransc.enrichment.repository.ImmutableEiProducerRegistrationInfo;
46 import org.oransc.enrichment.repository.ImmutableEiTypeRegistrationInfo;
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     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
79     @ApiOperation(value = "EI type identifiers", notes = "")
80     @ApiResponses(
81         value = { //
82             @ApiResponse(
83                 code = 200,
84                 message = "EI type identifiers",
85                 response = String.class,
86                 responseContainer = "List"), //
87         })
88     public ResponseEntity<Object> getEiTypeIdentifiers( //
89     ) {
90         List<String> result = new ArrayList<>();
91         for (EiType eiType : this.eiTypes.getAllEiTypes()) {
92             result.add(eiType.getId());
93         }
94
95         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
96     }
97
98     @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
99     @ApiOperation(value = "Individual EI type", notes = "")
100     @ApiResponses(
101         value = { //
102             @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
103             @ApiResponse(
104                 code = 404,
105                 message = "Enrichment Information type is not found",
106                 response = ErrorResponse.ErrorInfo.class)})
107     public ResponseEntity<Object> getEiType( //
108         @PathVariable("eiTypeId") String eiTypeId) {
109         try {
110             EiType t = this.eiTypes.getType(eiTypeId);
111             ProducerEiTypeInfo info = toEiTypeInfo(t);
112             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
113         } catch (Exception e) {
114             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
115         }
116     }
117
118     @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
119     @ApiOperation(value = "EI producer identifiers", notes = "")
120     @ApiResponses(
121         value = { //
122             @ApiResponse(
123                 code = 200,
124                 message = "EI producer identifiers",
125                 response = String.class,
126                 responseContainer = "List"), //
127         })
128     public ResponseEntity<Object> getEiProducerIdentifiers( //
129     ) {
130         List<String> result = new ArrayList<>();
131         for (EiProducer eiProducer : this.eiProducers.getAllProducers()) {
132             result.add(eiProducer.getId());
133         }
134
135         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
136     }
137
138     @GetMapping(
139         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
140         produces = MediaType.APPLICATION_JSON_VALUE)
141     @ApiOperation(value = "Individual EI producer", notes = "")
142     @ApiResponses(
143         value = { //
144             @ApiResponse(code = 200, message = "EI jobs", response = ProducerRegistrationInfo.class), //
145             @ApiResponse(
146                 code = 404,
147                 message = "Enrichment Information producer is not found",
148                 response = ErrorResponse.ErrorInfo.class)})
149     public ResponseEntity<Object> getEiProducer( //
150         @PathVariable("eiProducerId") String eiProducerId) {
151         try {
152             EiProducer p = this.eiProducers.getProducer(eiProducerId);
153             ProducerRegistrationInfo info = toEiProducerRegistrationInfo(p);
154             return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
155         } catch (Exception e) {
156             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
157         }
158     }
159
160     @GetMapping(
161         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
162         produces = MediaType.APPLICATION_JSON_VALUE)
163     @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer")
164     @ApiResponses(
165         value = { //
166             @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
167             @ApiResponse(
168                 code = 404,
169                 message = "Enrichment Information producer is not found",
170                 response = ErrorResponse.ErrorInfo.class)})
171     public ResponseEntity<Object> getEiProducerJobs( //
172         @PathVariable("eiProducerId") String eiProducerId) {
173         try {
174             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
175             Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
176             for (EiType type : producer.getEiTypes()) {
177                 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
178                     ProducerJobInfo request = new ProducerJobInfo(eiJob);
179                     producerJobs.add(request);
180                 }
181             }
182
183             return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
184         } catch (Exception e) {
185             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
186         }
187     }
188
189     @GetMapping(
190         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
191         produces = MediaType.APPLICATION_JSON_VALUE)
192     @ApiOperation(value = "EI producer status")
193     @ApiResponses(
194         value = { //
195             @ApiResponse(code = 200, message = "EI jobs", response = ProducerStatusInfo.class), //
196             @ApiResponse(
197                 code = 404,
198                 message = "Enrichment Information producer is not found",
199                 response = ErrorResponse.ErrorInfo.class)})
200     public ResponseEntity<Object> getEiProducerStatus( //
201         @PathVariable("eiProducerId") String eiProducerId) {
202         try {
203             EiProducer producer = this.eiProducers.getProducer(eiProducerId);
204             return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
205         } catch (Exception e) {
206             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
207         }
208     }
209
210     private ProducerStatusInfo producerStatusInfo(EiProducer producer) {
211         ProducerStatusInfo.OperationalState opState =
212             producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
213                 : ProducerStatusInfo.OperationalState.DISABLED;
214         return new ProducerStatusInfo(opState);
215     }
216
217     @PutMapping(
218         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
219         produces = MediaType.APPLICATION_JSON_VALUE)
220     @ApiOperation(value = "Individual EI producer", notes = "")
221     @ApiResponses(
222         value = { //
223             @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), //
224             @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)}//
225     )
226     public ResponseEntity<Object> putEiProducer( //
227         @PathVariable("eiProducerId") String eiProducerId, //
228         @RequestBody ProducerRegistrationInfo registrationInfo) {
229         try {
230             EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
231             this.eiProducers.registerProducer(toEiProducerRegistrationInfo(eiProducerId, registrationInfo));
232             return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
233         } catch (Exception e) {
234             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
235         }
236     }
237
238     @DeleteMapping(
239         path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
240         produces = MediaType.APPLICATION_JSON_VALUE)
241     @ApiOperation(value = "Individual EI producer", notes = "")
242     @ApiResponses(
243         value = { //
244             @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class),
245             @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class),
246             @ApiResponse(code = 404, message = "Producer is not found", response = ErrorResponse.ErrorInfo.class)})
247     public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
248         try {
249             final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
250             this.eiProducers.deregisterProducer(producer, this.eiTypes);
251             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
252         } catch (Exception e) {
253             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
254         }
255     }
256
257     private ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
258         Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
259         for (EiType type : p.getEiTypes()) {
260             types.add(toEiTypeRegistrationInfo(type));
261         }
262         return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
263     }
264
265     private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) {
266         return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId());
267     }
268
269     private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
270         Collection<String> producerIds = this.eiProducers.getProducerIdsForType(t.getId());
271         return new ProducerEiTypeInfo(t.getJobDataSchema(), producerIds);
272     }
273
274     private EiProducers.EiProducerRegistrationInfo toEiProducerRegistrationInfo(String eiProducerId,
275         ProducerRegistrationInfo info) {
276         Collection<EiProducers.EiTypeRegistrationInfo> supportedTypes = new ArrayList<>();
277         for (ProducerEiTypeRegistrationInfo typeInfo : info.types) {
278             EiProducers.EiTypeRegistrationInfo i = ImmutableEiTypeRegistrationInfo.builder() //
279                 .id(typeInfo.eiTypeId) //
280                 .jobDataSchema(typeInfo.jobDataSchema) //
281                 .build();
282             supportedTypes.add(i);
283         }
284         return ImmutableEiProducerRegistrationInfo.builder() //
285             .id(eiProducerId) //
286             .jobCallbackUrl(info.jobCallbackUrl) //
287             .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
288             .supportedTypes(supportedTypes) //
289             .build();
290     }
291
292 }