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.consumer;
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.List;
35 import org.oransc.enrichment.controllers.ErrorResponse;
36 import org.oransc.enrichment.repository.EiJob;
37 import org.oransc.enrichment.repository.EiJobs;
38 import org.oransc.enrichment.repository.EiType;
39 import org.oransc.enrichment.repository.EiTypes;
40 import org.oransc.enrichment.repository.ImmutableEiJob;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.bind.annotation.DeleteMapping;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.PutMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RestController;
52 @RestController("ConsumerController")
53 @Api(tags = {ConsumerConsts.CONSUMER_API_NAME})
54 public class ConsumerController {
57 private EiJobs eiJobs;
60 private EiTypes eiTypes;
62 private static Gson gson = new GsonBuilder() //
66 @GetMapping(path = ConsumerConsts.A1E_API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
67 @ApiOperation(value = "Query EI type identifiers", notes = "DETAILS TBD")
72 message = "EI type identifiers",
73 response = String.class,
74 responseContainer = "List"), //
76 public ResponseEntity<Object> getEiTypeIdentifiers( //
78 List<String> result = new ArrayList<>();
79 for (EiType eiType : this.eiTypes.getAllEiTypes()) {
80 result.add(eiType.id());
83 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
86 @GetMapping(path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}", produces = MediaType.APPLICATION_JSON_VALUE)
87 @ApiOperation(value = "Definitions for an individual EI Type", notes = "Query EI type")
90 @ApiResponse(code = 200, message = "EI type", response = ConsumerEiTypeInfo.class), //
93 message = "Enrichment Information type is not found",
94 response = ErrorResponse.ErrorInfo.class)})
95 public ResponseEntity<Object> getEiType( //
96 @PathVariable("eiTypeId") String eiTypeId) {
98 EiType t = this.eiTypes.getType(eiTypeId);
99 ConsumerEiTypeInfo info = toEiTypeInfo(t);
100 return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK);
101 } catch (Exception e) {
102 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
107 path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs",
108 produces = MediaType.APPLICATION_JSON_VALUE)
109 @ApiOperation(value = "Query EI job identifiers", notes = "Returns the EI Job identifiers for an EI Type")
114 message = "EI job identifiers",
115 response = String.class,
116 responseContainer = "List"), //
119 message = "Enrichment Information type is not found",
120 response = ErrorResponse.ErrorInfo.class)})
121 public ResponseEntity<Object> getEiJobIds( //
122 @PathVariable("eiTypeId") String eiTypeId, //
124 name = ConsumerConsts.OWNER_PARAM,
126 value = ConsumerConsts.OWNER_PARAM_DESCRIPTION) //
129 this.eiTypes.getType(eiTypeId); // Just to check that the type exists
130 List<String> result = new ArrayList<>();
131 for (EiJob job : this.eiJobs.getJobsForType(eiTypeId)) {
132 result.add(job.id());
134 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
135 } catch (Exception e) {
136 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
141 path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs/{eiJobId}",
142 produces = MediaType.APPLICATION_JSON_VALUE)
143 @ApiOperation(value = "Individual EI Job", notes = "")
146 @ApiResponse(code = 200, message = "EI Job", response = ConsumerEiJobInfo.class), //
149 message = "Enrichment Information type or job is not found",
150 response = ErrorResponse.ErrorInfo.class)})
151 public ResponseEntity<Object> getIndividualEiJob( //
152 @PathVariable("eiTypeId") String eiTypeId, //
153 @PathVariable("eiJobId") String eiJobId) {
155 this.eiTypes.getType(eiTypeId); // Just to check that the type exists
156 EiJob job = this.eiJobs.getJob(eiJobId);
157 return new ResponseEntity<>(gson.toJson(toEiJobInfo(job)), HttpStatus.OK);
158 } catch (Exception e) {
159 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
164 path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs/{eiJobId}/status",
165 produces = MediaType.APPLICATION_JSON_VALUE)
166 @ApiOperation(value = "EI Job status", notes = "")
169 @ApiResponse(code = 200, message = "EI Job status", response = ConsumerEiJobStatus.class), //
172 message = "Enrichment Information type or job is not found",
173 response = ErrorResponse.ErrorInfo.class)})
174 public ResponseEntity<Object> getEiJobStatus( //
175 @PathVariable("eiTypeId") String eiTypeId, //
176 @PathVariable("eiJobId") String eiJobId) {
178 this.eiTypes.getType(eiTypeId); // Just to check that the type exists
179 EiJob job = this.eiJobs.getJob(eiJobId);
180 return new ResponseEntity<>(gson.toJson(toEiJobStatus(job)), HttpStatus.OK);
181 } catch (Exception e) {
182 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
186 private ConsumerEiJobStatus toEiJobStatus(EiJob job) {
187 return new ConsumerEiJobStatus(ConsumerEiJobStatus.OperationalState.ENABLED);
191 path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs/{eiJobId}",
192 produces = MediaType.APPLICATION_JSON_VALUE)
193 @ApiOperation(value = "Individual EI Job", notes = "Delete EI job")
196 @ApiResponse(code = 200, message = "Not used", response = void.class),
197 @ApiResponse(code = 204, message = "Job deleted", response = void.class),
200 message = "Enrichment Information type or job is not found",
201 response = ErrorResponse.ErrorInfo.class)})
202 public ResponseEntity<Object> deleteIndividualEiJob( //
203 @PathVariable("eiTypeId") String eiTypeId, //
204 @PathVariable("eiJobId") String eiJobId) {
206 this.eiJobs.remove(eiJobId);
207 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
208 } catch (Exception e) {
209 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
214 path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs/{eiJobId}", //
215 produces = MediaType.APPLICATION_JSON_VALUE, //
216 consumes = MediaType.APPLICATION_JSON_VALUE)
217 @ApiOperation(value = "Individual EI Job", notes = "Create or update an EI Job")
220 @ApiResponse(code = 201, message = "Job created", response = void.class), //
221 @ApiResponse(code = 200, message = "Job updated", response = void.class), // ,
224 message = "Enrichment Information type is not found",
225 response = ErrorResponse.ErrorInfo.class)})
226 public ResponseEntity<Object> putIndividualEiJob( //
227 @PathVariable("eiTypeId") String eiTypeId, //
228 @PathVariable("eiJobId") String eiJobId, //
229 @RequestBody ConsumerEiJobInfo eiJobInfo) {
231 this.eiTypes.getType(eiTypeId); // Just to check that the type exists
232 final boolean newJob = this.eiJobs.get(eiJobId) == null;
233 this.eiJobs.put(toEiJob(eiJobInfo, eiJobId, eiTypeId));
234 return new ResponseEntity<>(newJob ? HttpStatus.CREATED : HttpStatus.OK);
235 } catch (Exception e) {
236 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
242 private EiJob toEiJob(ConsumerEiJobInfo info, String id, String typeId) {
243 return ImmutableEiJob.builder() //
246 .owner(info.owner) //
247 .jobData(info.jobData) //
251 private ConsumerEiTypeInfo toEiTypeInfo(EiType t) {
252 return new ConsumerEiTypeInfo(t.jobDataSchema());
255 private ConsumerEiJobInfo toEiJobInfo(EiJob s) {
256 return new ConsumerEiJobInfo(s.jobData(), s.owner());