2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
6 * Modifications Copyright (C) 2020 Nordix Foundation
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.portal.nonrtric.controlpanel.controller;
23 import io.swagger.annotations.ApiOperation;
25 import java.lang.invoke.MethodHandles;
27 import javax.servlet.http.HttpServletResponse;
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.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 = ControlPanelConstants.ENDPOINT_PREFIX + "/policy";
63 public static final String POLICY_TYPES_METHOD = "policytypes";
64 public static final String POLICY_TYPE_PARAM = "type";
65 public static final String POLICIES_NAME = "policies";
66 public static final String POLICY_INSTANCE_ID_NAME = "policy_instance_id";
68 // Populated by the autowired constructor
69 private final PolicyAgentApi policyAgentApi;
72 public PolicyController(final PolicyAgentApi policyAgentApi) {
73 Assert.notNull(policyAgentApi, "API must not be null");
74 this.policyAgentApi = policyAgentApi;
75 logger.debug("ctor: configured with client type {}", policyAgentApi.getClass().getName());
79 * The fields are defined in the Policy Control Typescript interface.
81 @ApiOperation(value = "Gets the policy types from Near-RT RIC")
82 @GetMapping(POLICY_TYPES_METHOD)
83 public ResponseEntity<String> getAllPolicyTypes(HttpServletResponse response) {
84 logger.debug("getAllPolicyTypes");
85 return this.policyAgentApi.getAllPolicyTypes();
88 @ApiOperation(value = "Returns the policy instances for the given policy type.")
89 @GetMapping(POLICIES_NAME)
90 public ResponseEntity<String> getPolicyInstances(
91 @RequestParam(name = "type", required = true) String policyTypeIdString) {
92 logger.debug("getPolicyInstances {}", policyTypeIdString);
93 return this.policyAgentApi.getPolicyInstancesForType(policyTypeIdString);
96 @ApiOperation(value = "Returns a policy instance of a type")
97 @GetMapping(POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME + "}")
98 public ResponseEntity<Object> getPolicyInstance(
99 @RequestParam(name = "type", required = true) String policyTypeIdString,
100 @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
101 logger.debug("getPolicyInstance {}:{}", policyTypeIdString, policyInstanceId);
102 return this.policyAgentApi.getPolicyInstance(policyInstanceId);
105 @ApiOperation(value = "Creates the policy instances for the given policy type.")
106 @PutMapping(POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME + "}")
107 public ResponseEntity<String> putPolicyInstance( //
108 @RequestParam(POLICY_TYPE_PARAM) String policyTypeIdString, //
109 @RequestParam(name = "ric", required = true) String ric, //
110 @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId, //
111 @RequestBody String instance) {
112 logger.debug("putPolicyInstance ric: {}, typeId: {}, instanceId: {}, instance: {}", ric, policyTypeIdString,
113 policyInstanceId, instance);
114 return this.policyAgentApi.putPolicy(policyTypeIdString, policyInstanceId, instance, ric);
117 @ApiOperation(value = "Deletes the policy instances for the given policy type.")
118 @DeleteMapping(POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME + "}")
119 public ResponseEntity<String> deletePolicyInstance( //
120 @RequestParam(POLICY_TYPE_PARAM) String policyTypeIdString,
121 @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
122 logger.debug("deletePolicyInstance typeId: {}, instanceId: {}", policyTypeIdString, policyInstanceId);
123 return this.policyAgentApi.deletePolicy(policyInstanceId);
126 @ApiOperation(value = "Returns the rics supporting the given policy type.")
128 public ResponseEntity<String> getRicsSupportingType(
129 @RequestParam(name = "policyType", required = true) String supportingPolicyType) {
130 logger.debug("getRicsSupportingType {}", supportingPolicyType);
132 return this.policyAgentApi.getRicsSupportingType(supportingPolicyType);