2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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
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.producer.ProducerRegistrationInfo.ProducerEiTypeRegistrationInfo;
40 import org.oransc.enrichment.repository.EiJob;
41 import org.oransc.enrichment.repository.EiJobs;
42 import org.oransc.enrichment.repository.EiProducer;
43 import org.oransc.enrichment.repository.EiProducers;
44 import org.oransc.enrichment.repository.EiType;
45 import org.oransc.enrichment.repository.EiTypes;
46 import org.oransc.enrichment.repository.ImmutableEiProducer;
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() //
72 private EiJobs eiJobs;
75 private EiTypes eiTypes;
78 private EiProducers eiProducers;
81 ProducerCallbacks producerCallbacks;
83 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
84 @ApiOperation(value = "EI type identifiers", notes = "")
89 message = "EI type identifiers",
90 response = String.class,
91 responseContainer = "List"), //
93 public ResponseEntity<Object> getEiTypeIdentifiers( //
95 List<String> result = new ArrayList<>();
96 for (EiType eiType : this.eiTypes.getAllEiTypes()) {
97 result.add(eiType.getId());
100 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
103 @GetMapping(path = ProducerConsts.API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
104 @ApiOperation(value = "Individual EI Type", notes = "")
107 @ApiResponse(code = 200, message = "EI type", response = ProducerEiTypeInfo.class), //
110 message = "Enrichment Information type is not found",
111 response = ErrorResponse.ErrorInfo.class)})
112 public ResponseEntity<Object> getEiType( //
113 @PathVariable("eiTypeId") String eiTypeId) {
115 EiType t = this.eiTypes.getType(eiTypeId);
116 ProducerEiTypeInfo info = toEiTypeInfo(t);
117 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
118 } catch (Exception e) {
119 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
123 @GetMapping(path = ProducerConsts.API_ROOT + "/eiproducers", produces = MediaType.APPLICATION_JSON_VALUE)
124 @ApiOperation(value = "EI producer identifiers", notes = "")
129 message = "EI producer identifiers",
130 response = String.class,
131 responseContainer = "List"), //
133 public ResponseEntity<Object> getEiProducerIdentifiers( //
135 List<String> result = new ArrayList<>();
136 for (EiProducer eiProducer : this.eiProducers.getAllProducers()) {
137 result.add(eiProducer.id());
140 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
144 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
145 produces = MediaType.APPLICATION_JSON_VALUE)
146 @ApiOperation(value = "Individual EI producer", notes = "")
149 @ApiResponse(code = 200, message = "EI Jobs", response = ProducerRegistrationInfo.class), //
152 message = "Enrichment Information producer is not found",
153 response = ErrorResponse.ErrorInfo.class)})
154 public ResponseEntity<Object> getEiProducer( //
155 @PathVariable("eiProducerId") String eiProducerId) {
157 EiProducer p = this.eiProducers.getProducer(eiProducerId);
158 ProducerRegistrationInfo info = toEiProducerRegistrationInfo(p);
159 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
160 } catch (Exception e) {
161 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
166 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}/eijobs",
167 produces = MediaType.APPLICATION_JSON_VALUE)
168 @ApiOperation(value = "EI job definitions", notes = "EI job definitions for one EI producer")
171 @ApiResponse(code = 200, message = "EI jobs", response = ProducerJobInfo.class, responseContainer = "List"), //
174 message = "Enrichment Information producer is not found",
175 response = ErrorResponse.ErrorInfo.class)})
176 public ResponseEntity<Object> getEiProducerJobs( //
177 @PathVariable("eiProducerId") String eiProducerId) {
179 EiProducer producer = this.eiProducers.getProducer(eiProducerId);
180 Collection<ProducerJobInfo> producerJobs = new ArrayList<>();
181 for (EiType type : producer.eiTypes()) {
182 for (EiJob eiJob : this.eiJobs.getJobsForType(type)) {
183 ProducerJobInfo request = new ProducerJobInfo(eiJob);
184 producerJobs.add(request);
188 return new ResponseEntity<>(gson.toJson(producerJobs), HttpStatus.OK);
189 } catch (Exception e) {
190 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
195 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
196 produces = MediaType.APPLICATION_JSON_VALUE)
197 @ApiOperation(value = "Individual EI producer", notes = "")
200 @ApiResponse(code = 201, message = "Producer created", response = void.class), //
201 @ApiResponse(code = 200, message = "Producer updated", response = void.class)}//
203 public ResponseEntity<Object> putEiProducer( //
204 @PathVariable("eiProducerId") String eiProducerId, //
205 @RequestBody ProducerRegistrationInfo registrationInfo) {
207 EiProducer previousDefinition = this.eiProducers.get(eiProducerId);
208 if (previousDefinition != null) {
209 for (EiType type : previousDefinition.eiTypes()) {
210 type.removeProducer(previousDefinition);
214 registerProducer(eiProducerId, registrationInfo);
215 if (previousDefinition != null) {
216 purgeTypes(previousDefinition.eiTypes());
219 return new ResponseEntity<>(previousDefinition == null ? HttpStatus.CREATED : HttpStatus.OK);
220 } catch (Exception e) {
221 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
225 private void purgeTypes(Collection<EiType> types) {
226 for (EiType type : types) {
227 if (type.getProducerIds().isEmpty()) {
228 this.deregisterType(type);
234 path = ProducerConsts.API_ROOT + "/eiproducers/{eiProducerId}",
235 produces = MediaType.APPLICATION_JSON_VALUE)
236 @ApiOperation(value = "Individual EI producer", notes = "")
239 @ApiResponse(code = 200, message = "Not used", response = void.class),
240 @ApiResponse(code = 204, message = "Producer deleted", response = void.class),
241 @ApiResponse(code = 404, message = "Producer is not found", response = ErrorResponse.ErrorInfo.class)})
242 public ResponseEntity<Object> deleteEiProducer(@PathVariable("eiProducerId") String eiProducerId) {
244 final EiProducer producer = this.eiProducers.getProducer(eiProducerId);
245 deregisterProducer(producer);
246 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
247 } catch (Exception e) {
248 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
252 private EiType registerType(ProducerEiTypeRegistrationInfo typeInfo) {
253 EiType type = this.eiTypes.get(typeInfo.eiTypeId);
255 type = new EiType(typeInfo.eiTypeId, typeInfo.jobDataSchema);
256 this.eiTypes.put(type);
262 EiProducer createProducer(Collection<EiType> types, String producerId, ProducerRegistrationInfo registrationInfo) {
263 return ImmutableEiProducer.builder() //
266 .jobCreationCallbackUrl(registrationInfo.jobCreationCallbackUrl) //
267 .jobDeletionCallbackUrl(registrationInfo.jobDeletionCallbackUrl) //
271 private EiProducer registerProducer(String producerId, ProducerRegistrationInfo registrationInfo) {
272 ArrayList<EiType> types = new ArrayList<>();
273 for (ProducerEiTypeRegistrationInfo typeInfo : registrationInfo.types) {
274 types.add(registerType(typeInfo));
276 EiProducer producer = createProducer(types, producerId, registrationInfo);
277 this.eiProducers.put(producer);
279 for (EiType type : types) {
280 for (EiJob job : this.eiJobs.getJobsForType(type)) {
281 this.producerCallbacks.notifyProducerJobStarted(producer, job);
283 type.addProducer(producer);
288 private void deregisterType(EiType type) {
289 this.eiTypes.remove(type);
290 for (EiJob job : this.eiJobs.getJobsForType(type.getId())) {
291 this.eiJobs.remove(job);
292 this.logger.warn("Deleted job {} because no producers left", job.id());
296 private void deregisterProducer(EiProducer producer) {
297 this.eiProducers.remove(producer);
298 for (EiType type : producer.eiTypes()) {
299 boolean removed = type.removeProducer(producer) != null;
301 this.logger.error("Bug, no producer found");
303 if (type.getProducerIds().isEmpty()) {
304 deregisterType(type);
309 ProducerRegistrationInfo toEiProducerRegistrationInfo(EiProducer p) {
310 Collection<ProducerEiTypeRegistrationInfo> types = new ArrayList<>();
311 for (EiType type : p.eiTypes()) {
312 types.add(toEiTypeRegistrationInfo(type));
314 return new ProducerRegistrationInfo(types, p.jobCreationCallbackUrl(), p.jobDeletionCallbackUrl());
317 private ProducerEiTypeRegistrationInfo toEiTypeRegistrationInfo(EiType type) {
318 return new ProducerEiTypeRegistrationInfo(type.getJobDataSchema(), type.getId());
321 private ProducerEiTypeInfo toEiTypeInfo(EiType t) {
322 return new ProducerEiTypeInfo(t.getJobDataSchema(), t.getProducerIds());