Merge "Fixed broken unittest in policy agent"
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / controllers / consumer / ConsumerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.enrichment.controllers.consumer;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
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;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
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;
51
52 @RestController("ConsumerController")
53 @Api(tags = {ConsumerConsts.CONSUMER_API_NAME})
54 public class ConsumerController {
55
56     @Autowired
57     private EiJobs eiJobs;
58
59     @Autowired
60     private EiTypes eiTypes;
61
62     private static Gson gson = new GsonBuilder() //
63         .serializeNulls() //
64         .create(); //
65
66     @GetMapping(path = ConsumerConsts.A1E_API_ROOT + "/eitypes", produces = MediaType.APPLICATION_JSON_VALUE)
67     @ApiOperation(value = "Query EI type identifiers", notes = "DETAILS TBD")
68     @ApiResponses(
69         value = { //
70             @ApiResponse(
71                 code = 200,
72                 message = "EI type identifiers",
73                 response = String.class,
74                 responseContainer = "List"), //
75         })
76     public ResponseEntity<Object> getEiTypeIdentifiers( //
77     ) {
78         List<String> result = new ArrayList<>();
79         for (EiType eiType : this.eiTypes.getAllEiTypes()) {
80             result.add(eiType.id());
81         }
82
83         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
84     }
85
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")
88     @ApiResponses(
89         value = { //
90             @ApiResponse(code = 200, message = "EI type", response = ConsumerEiTypeInfo.class), //
91             @ApiResponse(
92                 code = 404,
93                 message = "Enrichment Information type is not found",
94                 response = ErrorResponse.ErrorInfo.class)})
95     public ResponseEntity<Object> getEiType( //
96         @PathVariable("eiTypeId") String eiTypeId) {
97         try {
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);
103         }
104     }
105
106     @GetMapping(
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")
110     @ApiResponses(
111         value = { //
112             @ApiResponse(
113                 code = 200,
114                 message = "EI job identifiers",
115                 response = String.class,
116                 responseContainer = "List"), //
117             @ApiResponse(
118                 code = 404,
119                 message = "Enrichment Information type is not found",
120                 response = ErrorResponse.ErrorInfo.class)})
121     public ResponseEntity<Object> getEiJobIds( //
122         @PathVariable("eiTypeId") String eiTypeId, //
123         @ApiParam(
124             name = ConsumerConsts.OWNER_PARAM,
125             required = false, //
126             value = ConsumerConsts.OWNER_PARAM_DESCRIPTION) //
127         String owner) {
128         try {
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());
133             }
134             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
135         } catch (Exception e) {
136             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
137         }
138     }
139
140     @GetMapping(
141         path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs/{eiJobId}",
142         produces = MediaType.APPLICATION_JSON_VALUE)
143     @ApiOperation(value = "Individual EI Job", notes = "")
144     @ApiResponses(
145         value = { //
146             @ApiResponse(code = 200, message = "EI Job", response = ConsumerEiJobInfo.class), //
147             @ApiResponse(
148                 code = 404,
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) {
154         try {
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);
160         }
161     }
162
163     @GetMapping(
164         path = ConsumerConsts.A1E_API_ROOT + "/eitypes/{eiTypeId}/eijobs/{eiJobId}/status",
165         produces = MediaType.APPLICATION_JSON_VALUE)
166     @ApiOperation(value = "EI Job status", notes = "")
167     @ApiResponses(
168         value = { //
169             @ApiResponse(code = 200, message = "EI Job status", response = ConsumerEiJobStatus.class), //
170             @ApiResponse(
171                 code = 404,
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) {
177         try {
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);
183         }
184     }
185
186     private ConsumerEiJobStatus toEiJobStatus(EiJob job) {
187         return new ConsumerEiJobStatus(ConsumerEiJobStatus.OperationalState.ENABLED);
188     }
189
190     @DeleteMapping(
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")
194     @ApiResponses(
195         value = { //
196             @ApiResponse(code = 200, message = "Not used", response = void.class),
197             @ApiResponse(code = 204, message = "Job deleted", response = void.class),
198             @ApiResponse(
199                 code = 404,
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) {
205         try {
206             this.eiJobs.remove(eiJobId);
207             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
208         } catch (Exception e) {
209             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
210         }
211     }
212
213     @PutMapping(
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")
218     @ApiResponses(
219         value = { //
220             @ApiResponse(code = 201, message = "Job created", response = void.class), //
221             @ApiResponse(code = 200, message = "Job updated", response = void.class), // ,
222             @ApiResponse(
223                 code = 404,
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) {
230         try {
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);
237         }
238     }
239
240     // Status TBD
241
242     private EiJob toEiJob(ConsumerEiJobInfo info, String id, String typeId) {
243         return ImmutableEiJob.builder() //
244             .id(id) //
245             .typeId(typeId) //
246             .owner(info.owner) //
247             .jobData(info.jobData) //
248             .build();
249     }
250
251     private ConsumerEiTypeInfo toEiTypeInfo(EiType t) {
252         return new ConsumerEiTypeInfo(t.jobDataSchema());
253     }
254
255     private ConsumerEiJobInfo toEiJobInfo(EiJob s) {
256         return new ConsumerEiJobInfo(s.jobData(), s.owner());
257     }
258 }