Add backend implementation of enrichment api
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / controller / EnrichmentController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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 package org.oransc.portal.nonrtric.controlpanel.controller;
21
22 import java.lang.invoke.MethodHandles;
23 import org.oransc.portal.nonrtric.controlpanel.ControlPanelConstants;
24 import org.oransc.portal.nonrtric.controlpanel.eiproducerapi.EiProducerApi;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.security.access.annotation.Secured;
31 import org.springframework.util.Assert;
32 import org.springframework.web.bind.annotation.GetMapping;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RestController;
36 import io.swagger.annotations.ApiOperation;
37
38 /**
39  * Proxies calls from the front end to the EI Producer API.
40  *
41  * If a method throws RestClientResponseException, it is handled by
42  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
43  * which returns status 502. All other exceptions are handled by Spring which
44  * returns status 500.
45  */
46 @RestController
47 @RequestMapping(value = EnrichmentController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
48 public class EnrichmentController {
49
50     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52     // Publish paths in constants so tests are easy to write
53     public static final String CONTROLLER_PATH = ControlPanelConstants.ENDPOINT_PREFIX + "/enrichment";
54     // Endpoints
55     public static final String VERSION_METHOD = ControlPanelConstants.VERSION_METHOD;
56     public static final String EI_TYPES = "eitypes";
57     public static final String EI_PRODUCERS = "eiproducers";
58     public static final String EI_JOBS = "eijobs";
59     public static final String EI_TYPE_ID = "ei_type_id";
60     public static final String EI_PRODUCER_ID = "ei_producer_id";
61     public static final String STATUS = "status";
62
63     // Populated by the autowired constructor
64     private final EiProducerApi eiProducerApi;
65
66     @Autowired
67     public EnrichmentController(final EiProducerApi eiProducerApi) {
68         Assert.notNull(eiProducerApi, "API must not be null");
69         this.eiProducerApi = eiProducerApi;
70         logger.debug("enrichment: configured with client type {}", eiProducerApi.getClass().getName());
71     }
72
73     /*
74      * The fields are defined in the Enrichment Control Typescript interface.
75      */
76     @ApiOperation(value = "Get the EI type identifiers")
77     @GetMapping(EI_TYPES)
78     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
79     public ResponseEntity<String> getAllEiTypeIds() {
80         logger.debug("getAllEiTypeIds");
81         return this.eiProducerApi.getAllEiTypeIds();
82     }
83
84     @ApiOperation(value = "Get an individual EI type")
85     @GetMapping(EI_TYPES + "/{" + EI_TYPE_ID + "}")
86     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
87     public ResponseEntity<String> getEiType(@PathVariable(EI_TYPE_ID) String eiTypeId) {
88         logger.debug("getEiType {}", eiTypeId);
89         return this.eiProducerApi.getEiType(eiTypeId);
90     }
91
92     @ApiOperation(value = "Get the EI producer identifiers")
93     @GetMapping(EI_PRODUCERS)
94     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
95     public ResponseEntity<String> getAllEiProducerIds() {
96         logger.debug("getAllEiProducerIds");
97         return this.eiProducerApi.getAllEiProducerIds();
98     }
99
100     @ApiOperation(value = "Get an individual EI producer")
101     @GetMapping(EI_PRODUCERS + "/{" + EI_PRODUCER_ID + "}")
102     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
103     public ResponseEntity<String> getEiProducer(@PathVariable(EI_PRODUCER_ID) String eiProducerId) {
104         logger.debug("getEiProducer {}", eiProducerId);
105         return this.eiProducerApi.getEiProducer(eiProducerId);
106     }
107
108     @ApiOperation(value = "Get the EI job definitions for one EI producer")
109     @GetMapping(EI_PRODUCERS + "/{" + EI_PRODUCER_ID + "}/" + EI_JOBS)
110     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
111     public ResponseEntity<String> getEiJobsForOneEiProducer(@PathVariable(EI_PRODUCER_ID) String eiProducerId) {
112         logger.debug("getEiJobsForOneEiProducer {}", eiProducerId);
113         return this.eiProducerApi.getEiJobsForOneEiProducer(eiProducerId);
114     }
115
116     @ApiOperation(value = "Get the status of an EI producer")
117     @GetMapping(EI_PRODUCERS + "/{" + EI_PRODUCER_ID + "}/" + STATUS)
118     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
119     public ResponseEntity<String> getEiProducerStatus(@PathVariable(EI_PRODUCER_ID) String eiProducerId) {
120         logger.debug("getEiProducerStatus {}", eiProducerId);
121         return this.eiProducerApi.getEiProducerStatus(eiProducerId);
122     }
123 }