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.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
31 import java.lang.invoke.MethodHandles;
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.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;
60 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
61 @RestController("ProducerController")
62 @Api(tags = {ProducerConsts.PRODUCER_API_NAME})
63 public class ProducerController {
65 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
67 private static Gson gson = new GsonBuilder().create();
70 private EiJobs eiJobs;
73 private EiTypes eiTypes;
76 private EiProducers eiProducers;
78 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
79 @ApiOperation(value = "EI type identifiers", notes = "")
84 message = "EI type identifiers",
85 response = String.class,
86 responseContainer = "List"), //
88 public ResponseEntity<Object> getEiTypeIdentifiers( //
90 List<String> result = new ArrayList<>();
91 for (EiType eiType : this.eiTypes.getAllEiTypes()) {
92 result.add(eiType.getId());
95 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
98 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
99 @ApiOperation(value = "Individual EI type", notes = "")
102 @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
105 message = "Enrichment Information type is not found",
106 response = ErrorResponse.ErrorInfo.class)})
107 public ResponseEntity<Object> getEiType( //
108 @PathVariable("eiTypeId") String eiTypeId) {
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);
118 @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
119 @ApiOperation(value = "EI producer identifiers", notes = "")
124 message = "EI producer identifiers",
125 response = String.class,
126 responseContainer = "List"), //
128 public ResponseEntity<Object> getEiProducerIdentifiers( //
130 List<String> result = new ArrayList<>();
131 for (EiProducer eiProducer : this.eiProducers.getAllProducers()) {
132 result.add(eiProducer.getId());
135 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
139 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
140 produces = MediaType.APPLICATION_JSON_VALUE)
141 @ApiOperation(value = "Individual EI producer", notes = "")
144 @ApiResponse(code = 200, message = "EI jobs", response = ProducerRegistrationInfo.class), //
147 message = "Enrichment Information producer is not found",
148 response = ErrorResponse.ErrorInfo.class)})
149 public ResponseEntity<Object> getEiProducer( //
150 @PathVariable("eiProducerId") String eiProducerId) {
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);
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")
166 @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
169 message = "Enrichment Information producer is not found",
170 response = ErrorResponse.ErrorInfo.class)})
171 public ResponseEntity<Object> getEiProducerJobs( //
172 @PathVariable("eiProducerId") String eiProducerId) {
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);
183 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
184 } catch (Exception e) {
185 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
190 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
191 produces = MediaType.APPLICATION_JSON_VALUE)
192 @ApiOperation(value = "EI producer status")
195 @ApiResponse(code = 200, message = "EI jobs", response = ProducerStatusInfo.class), //
198 message = "Enrichment Information producer is not found",
199 response = ErrorResponse.ErrorInfo.class)})
200 public ResponseEntity<Object> getEiProducerStatus( //
201 @PathVariable("eiProducerId") String eiProducerId) {
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);
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);
218 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
219 produces = MediaType.APPLICATION_JSON_VALUE)
220 @ApiOperation(value = "Individual EI producer", notes = "")
223 @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), //
224 @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)}//
226 public ResponseEntity<Object> putEiProducer( //
227 @PathVariable("eiProducerId") String eiProducerId, //
228 @RequestBody ProducerRegistrationInfo registrationInfo) {
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);
239 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
240 produces = MediaType.APPLICATION_JSON_VALUE)
241 @ApiOperation(value = "Individual EI producer", notes = "")
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) {
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);
257 private ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
258 Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
259 for (EiType type : p.getEiTypes()) {
260 types.add(toEiTypeRegistrationInfo(type));
262 return new ProducerRegistrationInfo(types, p.getJobCallbackUrl(), p.getProducerSupervisionCallbackUrl());
265 private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) {
266 return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId());
269 private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
270 Collection<String> producerIds = this.eiProducers.getProducerIdsForType(t.getId());
271 return new ProducerEiTypeInfo(t.getJobDataSchema(), producerIds);
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) //
282 supportedTypes.add(i);
284 return ImmutableEiProducerRegistrationInfo.builder() //
286 .jobCallbackUrl(info.jobCallbackUrl) //
287 .producerSupervisionCallbackUrl(info.producerSupervisionCallbackUrl) //
288 .supportedTypes(supportedTypes) //