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.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;
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;
79 ProducerCallbacks producerCallbacks;
81 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
82 @ApiOperation(value = "EI type identifiers", notes = "")
87 message = "EI type identifiers",
88 response = String.class,
89 responseContainer = "List"), //
91 public ResponseEntity<Object> getEiTypeIdentifiers( //
93 List<String> result = new ArrayList<>();
94 for (EiType eiType : this.eiTypes.getAllEiTypes()) {
95 result.add(eiType.getId());
98 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
101 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
102 @ApiOperation(value = "Individual EI type", notes = "")
105 @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
108 message = "Enrichment Information type is not found",
109 response = ErrorResponse.ErrorInfo.class)})
110 public ResponseEntity<Object> getEiType( //
111 @PathVariable("eiTypeId") String eiTypeId) {
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);
121 @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
122 @ApiOperation(value = "EI producer identifiers", notes = "")
127 message = "EI producer identifiers",
128 response = String.class,
129 responseContainer = "List"), //
131 public ResponseEntity<Object> getEiProducerIdentifiers( //
133 List<String> result = new ArrayList<>();
134 for (EiProducer eiProducer : this.eiProducers.getAllProducers()) {
135 result.add(eiProducer.getId());
138 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
142 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
143 produces = MediaType.APPLICATION_JSON_VALUE)
144 @ApiOperation(value = "Individual EI producer", notes = "")
147 @ApiResponse(code = 200, message = "EI jobs", response = ProducerRegistrationInfo.class), //
150 message = "Enrichment Information producer is not found",
151 response = ErrorResponse.ErrorInfo.class)})
152 public ResponseEntity<Object> getEiProducer( //
153 @PathVariable("eiProducerId") String eiProducerId) {
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);
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")
169 @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
172 message = "Enrichment Information producer is not found",
173 response = ErrorResponse.ErrorInfo.class)})
174 public ResponseEntity<Object> getEiProducerJobs( //
175 @PathVariable("eiProducerId") String eiProducerId) {
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);
186 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
187 } catch (Exception e) {
188 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
193 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/status",
194 produces = MediaType.APPLICATION_JSON_VALUE)
195 @ApiOperation(value = "EI producer status")
198 @ApiResponse(code = 200, message = "EI jobs", response = ProducerStatusInfo.class), //
201 message = "Enrichment Information producer is not found",
202 response = ErrorResponse.ErrorInfo.class)})
203 public ResponseEntity<Object> getEiProducerStatus( //
204 @PathVariable("eiProducerId") String eiProducerId) {
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);
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);
221 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
222 produces = MediaType.APPLICATION_JSON_VALUE)
223 @ApiOperation(value = "Individual EI producer", notes = "")
226 @ApiResponse(code = 201, message = "Producer created", response = VoidResponse.class), //
227 @ApiResponse(code = 200, message = "Producer updated", response = VoidResponse.class)}//
229 public ResponseEntity<Object> putEiProducer( //
230 @PathVariable("eiProducerId") String eiProducerId, //
231 @RequestBody ProducerRegistrationInfo registrationInfo) {
233 EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
234 if (previousDefinition != null) {
235 for (EiType type : previousDefinition.getEiTypes()) {
236 type.removeProducer(previousDefinition);
240 registerProducer(eiProducerId, registrationInfo);
241 if (previousDefinition != null) {
242 purgeTypes(previousDefinition.getEiTypes());
245 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
246 } catch (Exception e) {
247 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
251 private void purgeTypes(Collection<EiType> types) {
252 for (EiType type : types) {
253 if (type.getProducerIds().isEmpty()) {
254 this.eiTypes.deregisterType(type, this.eiJobs);
260 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
261 produces = MediaType.APPLICATION_JSON_VALUE)
262 @ApiOperation(value = "Individual EI producer", notes = "")
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) {
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);
278 private EiType registerType(ProducerEiTypeRegistrationInfo typeInfo) {
279 EiType type = this.eiTypes.get(typeInfo.eiTypeId);
281 type = new EiType(typeInfo.eiTypeId, typeInfo.jobDataSchema);
282 this.eiTypes.put(type);
287 EiProducer createProducer(Collection<EiType> types, String producerId, ProducerRegistrationInfo registrationInfo) {
288 return new EiProducer(producerId, types, registrationInfo.jobCreationCallbackUrl,
289 registrationInfo.jobDeletionCallbackUrl, registrationInfo.producerSupervisionCallbackUrl);
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));
297 EiProducer producer = createProducer(types, producerId, registrationInfo);
298 this.eiProducers.put(producer);
300 for (EiType type : types) {
301 for (EiJob job : this.eiJobs.getJobsForType(type)) {
302 this.producerCallbacks.notifyProducerJobStarted(producer, job) //
305 type.addProducer(producer);
310 ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
311 Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
312 for (EiType type : p.getEiTypes()) {
313 types.add(toEiTypeRegistrationInfo(type));
315 return new ProducerRegistrationInfo(types, p.getJobCreationCallbackUrl(), p.getJobDeletionCallbackUrl(),
316 p.getProducerSupervisionCallbackUrl());
319 private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) {
320 return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId());
323 private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
324 return new ProducerEiTypeInfo(t.getJobDataSchema(), t.getProducerIds());