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