2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import io.swagger.annotations.ApiOperation;
24 import java.lang.invoke.MethodHandles;
26 import javax.servlet.http.HttpServletResponse;
28 import org.oransc.ric.portal.dashboard.DashboardConstants;
29 import org.oransc.ric.portal.dashboard.policyagentapi.PolicyAgentApi;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.security.access.annotation.Secured;
36 import org.springframework.util.Assert;
37 import org.springframework.web.bind.annotation.DeleteMapping;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PutMapping;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestParam;
44 import org.springframework.web.bind.annotation.RestController;
47 * Proxies calls from the front end to the Policy agent API.
49 * If a method throws RestClientResponseException, it is handled by
50 * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
51 * which returns status 502. All other exceptions are handled by Spring which
55 @RequestMapping(value = PolicyController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
56 public class PolicyController {
58 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 // Publish paths in constants so tests are easy to write
61 public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/policy";
63 public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
64 public static final String POLICY_TYPES_METHOD = "policytypes";
65 public static final String POLICY_TYPE_ID_NAME = "policy_type_id";
66 public static final String POLICIES_NAME = "policies";
67 public static final String POLICY_INSTANCE_ID_NAME = "policy_instance_id";
69 // Populated by the autowired constructor
70 private final PolicyAgentApi policyAgentApi;
73 public PolicyController(final PolicyAgentApi policyAgentApi) {
74 Assert.notNull(policyAgentApi, "API must not be null");
75 this.policyAgentApi = policyAgentApi;
76 logger.debug("ctor: configured with client type {}", policyAgentApi.getClass().getName());
80 * The fields are defined in the Policy Control Typescript interface.
82 @ApiOperation(value = "Gets the policy types from Near Realtime-RIC")
83 @GetMapping(POLICY_TYPES_METHOD)
84 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
85 public ResponseEntity<String> getAllPolicyTypes(HttpServletResponse response) {
86 logger.debug("getAllPolicyTypes");
87 return this.policyAgentApi.getAllPolicyTypes();
90 @ApiOperation(value = "Returns the policy instances for the given policy type.")
91 @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME)
92 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
93 public ResponseEntity<String> getPolicyInstances(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString) {
94 logger.debug("getPolicyInstances {}", policyTypeIdString);
95 return this.policyAgentApi.getPolicyInstancesForType(policyTypeIdString);
98 @ApiOperation(value = "Returns a policy instance of a type")
99 @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME
101 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
102 public ResponseEntity<Object> getPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
103 @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
104 logger.debug("getPolicyInstance {}:{}", policyTypeIdString, policyInstanceId);
105 return this.policyAgentApi.getPolicyInstance(policyInstanceId);
108 @ApiOperation(value = "Creates the policy instances for the given policy type.")
109 @PutMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME
111 @Secured({ DashboardConstants.ROLE_ADMIN })
112 public ResponseEntity<String> putPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
113 @RequestParam(name = "ric", required = true) String ric,
114 @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId, @RequestBody String instance) {
115 logger.debug("putPolicyInstance typeId: {}, instanceId: {}, instance: {}", policyTypeIdString, policyInstanceId,
117 return this.policyAgentApi.putPolicy(policyTypeIdString, policyInstanceId, instance, ric);
120 @ApiOperation(value = "Deletes the policy instances for the given policy type.")
121 @DeleteMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{"
122 + POLICY_INSTANCE_ID_NAME + "}")
123 @Secured({ DashboardConstants.ROLE_ADMIN })
124 public ResponseEntity<String> deletePolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
125 @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
126 logger.debug("deletePolicyInstance typeId: {}, instanceId: {}", policyTypeIdString, policyInstanceId);
127 return this.policyAgentApi.deletePolicy(policyInstanceId);
130 @ApiOperation(value = "Returns the rics supporting the given policy type.")
132 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
133 public ResponseEntity<String> getRicsSupportingType(
134 @RequestParam(name = "policyType", required = true) String supportingPolicyType) {
135 logger.debug("getRicsSupportingType {}", supportingPolicyType);
137 return this.policyAgentApi.getRicsSupportingType(supportingPolicyType);