Standardisation of names
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / controller / PolicyController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.controller;
22
23 import io.swagger.annotations.ApiOperation;
24
25 import java.lang.invoke.MethodHandles;
26
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.oransc.portal.nonrtric.controlpanel.ControlPanelConstants;
30 import org.oransc.portal.nonrtric.controlpanel.policyagentapi.PolicyAgentApi;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.MediaType;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.security.access.annotation.Secured;
37 import org.springframework.util.Assert;
38 import org.springframework.web.bind.annotation.DeleteMapping;
39 import org.springframework.web.bind.annotation.GetMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PutMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestParam;
45 import org.springframework.web.bind.annotation.RestController;
46
47 /**
48  * Proxies calls from the front end to the Policy Agent API.
49  *
50  * If a method throws RestClientResponseException, it is handled by
51  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
52  * which returns status 502. All other exceptions are handled by Spring which
53  * returns status 500.
54  */
55 @RestController
56 @RequestMapping(value = PolicyController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
57 public class PolicyController {
58
59     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60
61     // Publish paths in constants so tests are easy to write
62     public static final String CONTROLLER_PATH = ControlPanelConstants.ENDPOINT_PREFIX + "/policy";
63     // Endpoints
64     public static final String VERSION_METHOD = ControlPanelConstants.VERSION_METHOD;
65     public static final String POLICY_TYPES_METHOD = "policytypes";
66     public static final String POLICY_TYPE_ID_NAME = "policy_type_id";
67     public static final String POLICIES_NAME = "policies";
68     public static final String POLICY_INSTANCE_ID_NAME = "policy_instance_id";
69
70     // Populated by the autowired constructor
71     private final PolicyAgentApi policyAgentApi;
72
73     @Autowired
74     public PolicyController(final PolicyAgentApi policyAgentApi) {
75         Assert.notNull(policyAgentApi, "API must not be null");
76         this.policyAgentApi = policyAgentApi;
77         logger.debug("ctor: configured with client type {}", policyAgentApi.getClass().getName());
78     }
79
80     /*
81      * The fields are defined in the Policy Control Typescript interface.
82      */
83     @ApiOperation(value = "Gets the policy types from Near-RT RIC")
84     @GetMapping(POLICY_TYPES_METHOD)
85     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
86     public ResponseEntity<String> getAllPolicyTypes(HttpServletResponse response) {
87         logger.debug("getAllPolicyTypes");
88         return this.policyAgentApi.getAllPolicyTypes();
89     }
90
91     @ApiOperation(value = "Returns the policy instances for the given policy type.")
92     @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME)
93     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
94     public ResponseEntity<String> getPolicyInstances(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString) {
95         logger.debug("getPolicyInstances {}", policyTypeIdString);
96         return this.policyAgentApi.getPolicyInstancesForType(policyTypeIdString);
97     }
98
99     @ApiOperation(value = "Returns a policy instance of a type")
100     @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME
101         + "}")
102     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
103     public ResponseEntity<Object> getPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
104         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
105         logger.debug("getPolicyInstance {}:{}", policyTypeIdString, policyInstanceId);
106         return this.policyAgentApi.getPolicyInstance(policyInstanceId);
107     }
108
109     @ApiOperation(value = "Creates the policy instances for the given policy type.")
110     @PutMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME
111         + "}")
112     @Secured({ControlPanelConstants.ROLE_ADMIN})
113     public ResponseEntity<String> putPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
114         @RequestParam(name = "ric", required = true) String ric,
115         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId, @RequestBody String instance) {
116         logger.debug("putPolicyInstance ric: {}, typeId: {}, instanceId: {}, instance: {}", ric, policyTypeIdString,
117             policyInstanceId, instance);
118         return this.policyAgentApi.putPolicy(policyTypeIdString, policyInstanceId, instance, ric);
119     }
120
121     @ApiOperation(value = "Deletes the policy instances for the given policy type.")
122     @DeleteMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{"
123         + POLICY_INSTANCE_ID_NAME + "}")
124     @Secured({ControlPanelConstants.ROLE_ADMIN})
125     public ResponseEntity<String> deletePolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
126         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
127         logger.debug("deletePolicyInstance typeId: {}, instanceId: {}", policyTypeIdString, policyInstanceId);
128         return this.policyAgentApi.deletePolicy(policyInstanceId);
129     }
130
131     @ApiOperation(value = "Returns the rics supporting the given policy type.")
132     @GetMapping("/rics")
133     @Secured({ControlPanelConstants.ROLE_ADMIN, ControlPanelConstants.ROLE_STANDARD})
134     public ResponseEntity<String> getRicsSupportingType(
135         @RequestParam(name = "policyType", required = true) String supportingPolicyType) {
136         logger.debug("getRicsSupportingType {}", supportingPolicyType);
137
138         return this.policyAgentApi.getRicsSupportingType(supportingPolicyType);
139     }
140 }