2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.enrichment.controllers.producer;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiParam;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
36 import org.oransc.enrichment.controllers.ErrorResponse;
37 import org.oransc.enrichment.controllers.VoidResponse;
38 import org.oransc.enrichment.exceptions.ServiceException;
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.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.MediaType;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.web.bind.annotation.DeleteMapping;
51 import org.springframework.web.bind.annotation.GetMapping;
52 import org.springframework.web.bind.annotation.PathVariable;
53 import org.springframework.web.bind.annotation.PutMapping;
54 import org.springframework.web.bind.annotation.RequestBody;
55 import org.springframework.web.bind.annotation.RequestParam;
56 import org.springframework.web.bind.annotation.RestController;
58 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
59 @RestController("ProducerController")
60 @Api(tags = {ProducerConsts.PRODUCER_API_NAME})
61 public class ProducerController {
63 private static Gson gson = new GsonBuilder().create();
66 private EiJobs eiJobs;
69 private EiTypes eiTypes;
72 private EiProducers eiProducers;
74 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
75 @ApiOperation(value = "EI type identifiers", notes = "")
80 message = "EI type identifiers",
81 response = String.class,
82 responseContainer = "List"), //
84 public ResponseEntity<Object> getEiTypeIdentifiers( //
86 List<String> result = new ArrayList<>();
87 for (EiType eiType : this.eiTypes.getAllEiTypes()) {
88 result.add(eiType.getId());
91 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
94 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
95 @ApiOperation(value = "Individual EI type", notes = "")
98 @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
101 message = "Enrichment Information type is not found",
102 response = ErrorResponse.ErrorInfo.class)})
103 public ResponseEntity<Object> getEiType( //
104 @PathVariable("eiTypeId") String eiTypeId) {
106 EiType t = this.eiTypes.getType(eiTypeId);
107 ProducerEiTypeInfo info = toEiTypeInfo(t);
108 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
109 } catch (Exception e) {
110 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
114 @PutMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
117 @ApiResponse(code = 200, message = "OK", response = VoidResponse.class), @ApiResponse(
119 message = "Bad request",
120 response = ErrorResponse.ErrorInfo.class)})
121 @ApiOperation(value = "Individual EI type", notes = "")
122 public ResponseEntity<Object> putEiType( //
123 @PathVariable("eiTypeId") String eiTypeId, @RequestBody ProducerEiTypeInfo registrationInfo) {
125 EiType previousDefinition = this.eiTypes.get(eiTypeId);
126 if (registrationInfo.jobDataSchema == null) {
127 return ErrorResponse.create("No schema provided", HttpStatus.BAD_REQUEST);
129 this.eiTypes.put(new EiType(eiTypeId, registrationInfo.jobDataSchema));
130 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
133 @DeleteMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
134 @ApiOperation(value = "Individual EI type", notes = "")
137 @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class),
138 @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class),
141 message = "Enrichment Information type is not found",
142 response = ErrorResponse.ErrorInfo.class),
145 message = "The Enrichment Information type has one or several active producers",
146 response = ErrorResponse.ErrorInfo.class)})
147 public ResponseEntity<Object> deleteEiType( //
148 @PathVariable("eiTypeId") String eiTypeId) {
150 EiType type = this.eiTypes.get(eiTypeId);
152 return ErrorResponse.create("EI type not found", HttpStatus.NOT_FOUND);
154 if (!this.eiProducers.getProducersForType(type).isEmpty()) {
155 String firstProducerId = this.eiProducers.getProducersForType(type).iterator().next().getId();
156 return ErrorResponse.create("The type has active producers: " + firstProducerId, HttpStatus.NOT_ACCEPTABLE);
158 this.eiTypes.remove(type);
159 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
162 @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
163 @ApiOperation(value = "EI producer identifiers", notes = "")
168 message = "EI producer identifiers",
169 response = String.class,
170 responseContainer = "List"), //
172 public ResponseEntity<Object> getEiProducerIdentifiers( //
176 value = "If given, only the producers for the EI Data type is returned.") //
177 @RequestParam(name = "ei_type_id", required = false) String typeId //
179 List<String> result = new ArrayList<>();
180 for (EiProducer eiProducer : typeId == null ? this.eiProducers.getAllProducers()
181 : this.eiProducers.getProducersForType(typeId)) {
182 result.add(eiProducer.getId());
185 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
189 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
190 produces = MediaType.APPLICATION_JSON_VALUE)
191 @ApiOperation(value = "Individual EI producer", notes = "")
194 @ApiResponse(code = 200, message = "EI jobs", response = ProducerRegistrationInfo.class), //
197 message = "Enrichment Information producer is not found",
198 response = ErrorResponse.ErrorInfo.class)})
199 public ResponseEntity<Object> getEiProducer( //
200 @PathVariable("eiProducerId") String eiProducerId) {
202 EiProducer p = this.eiProducers.getProducer(eiProducerId);
203 ProducerRegistrationInfo info = toEiProducerRegistrationInfo(p);
204 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
205 } catch (Exception e) {
206 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
211 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
212 produces = MediaType.APPLICATION_JSON_VALUE)
213 @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer")
216 @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
219 message = "Enrichment Information producer is not found",
220 response = ErrorResponse.ErrorInfo.class)})
221 public ResponseEntity<Object> getEiProducerJobs( //
222 @PathVariable("eiProducerId") String eiProducerId) {
224 EiProducer producer = this.eiProducers.getProducer(eiProducerId);
225 Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
226 for (EiType type : producer.getEiTypes()) {
227 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
228 ProducerJobInfo request = new ProducerJobInfo(eiJob);
229 producerJobs.add(request);
233 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
234 } catch (Exception e) {
235 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
240 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
241 produces = MediaType.APPLICATION_JSON_VALUE)
242 @ApiOperation(value = "EI producer status")
245 @ApiResponse(code = 200, message = "EI jobs", response = ProducerStatusInfo.class), //
248 message = "Enrichment Information producer is not found",
249 response = ErrorResponse.ErrorInfo.class)})
250 public ResponseEntity<Object> getEiProducerStatus( //
251 @PathVariable("eiProducerId") String eiProducerId) {
253 EiProducer producer = this.eiProducers.getProducer(eiProducerId);
254 return new ResponseEntity<>(gson.toJson(producerStatusInfo(producer)), HttpStatus.OK);
255 } catch (Exception e) {
256 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
260 private ProducerStatusInfo producerStatusInfo(EiProducer producer) {
261 ProducerStatusInfo.OperationalState opState =
262 producer.isAvailable() ? ProducerStatusInfo.OperationalState.ENABLED
263 : ProducerStatusInfo.OperationalState.DISABLED;
264 return new ProducerStatusInfo(opState);
268 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
269 produces = MediaType.APPLICATION_JSON_VALUE)
270 @ApiOperation(value = "Individual EI producer", notes = "")
273 @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), //
274 @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)}//
276 public ResponseEntity<Object> putEiProducer( //
277 @PathVariable("eiProducerId") String eiProducerId, //
278 @RequestBody ProducerRegistrationInfo registrationInfo) {
280 EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
281 this.eiProducers.registerProducer(toEiProducerRegistrationInfo(eiProducerId, registrationInfo));
282 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
283 } catch (Exception e) {
284 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
289 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
290 produces = MediaType.APPLICATION_JSON_VALUE)
291 @ApiOperation(value = "Individual EI producer", notes = "")
294 @ApiResponse(code = 200, message = "Not used", response = VoidResponse.class),
295 @ApiResponse(code = 204, message = "Producer deleted", response = VoidResponse.class),
296 @ApiResponse(code = 404, message = "Producer is not found", response = ErrorResponse.ErrorInfo.class)})
297 public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
299 final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
300 this.eiProducers.deregisterProducer(producer);
301 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
302 } catch (Exception e) {
303 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
307 private ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
308 Collection<String> types = new ArrayList<>();
309 for (EiType type : p.getEiTypes()) {
310 types.add(type.getId());
312 return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
315 private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
316 return new ProducerEiTypeInfo(t.getJobDataSchema());
319 private EiProducers.EiProducerRegistrationInfo toEiProducerRegistrationInfo(String eiProducerId,
320 ProducerRegistrationInfo info) throws ServiceException {
321 Collection<EiType> supportedTypes = new ArrayList<>();
322 for (String typeId : info.supportedTypeIds) {
323 EiType type = this.eiTypes.getType(typeId);
324 supportedTypes.add(type);
327 return ImmutableEiProducerRegistrationInfo.builder() //
329 .jobCallbackUrl(info.jobCallbackUrl) //
330 .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
331 .supportedTypes(supportedTypes) //