Fix imports and pom problem
[nonrtric.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / PolicyController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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.ric.portal.dashboard.controller;
21
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24 import io.swagger.annotations.ApiOperation;
25 import java.lang.invoke.MethodHandles;
26 import java.util.Collection;
27 import javax.servlet.http.HttpServletResponse;
28 import org.oransc.ric.portal.dashboard.DashboardConstants;
29 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
30 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
31 import org.oransc.ric.portal.dashboard.policyagentapi.PolicyAgentApi;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.security.access.annotation.Secured;
38 import org.springframework.util.Assert;
39 import org.springframework.web.bind.annotation.DeleteMapping;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PutMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47
48 /**
49  * Proxies calls from the front end to the Policy agent API.
50  *
51  * If a method throws RestClientResponseException, it is handled by
52  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
53  * which returns status 502. All other exceptions are handled by Spring which
54  * returns status 500.
55  */
56 @RestController
57 @RequestMapping(value = PolicyController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
58 public class PolicyController {
59
60     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61     private static Gson gson = new GsonBuilder() //
62         .serializeNulls() //
63         .create(); //
64
65     // Publish paths in constants so tests are easy to write
66     public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/policy";
67     // Endpoints
68     public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
69     public static final String POLICY_TYPES_METHOD = "policytypes";
70     public static final String POLICY_TYPE_ID_NAME = "policy_type_id";
71     public static final String POLICIES_NAME = "policies";
72     public static final String POLICY_INSTANCE_ID_NAME = "policy_instance_id";
73
74     // Populated by the autowired constructor
75     private final PolicyAgentApi policyAgentApi;
76
77     @Autowired
78     public PolicyController(final PolicyAgentApi policyAgentApi) {
79         Assert.notNull(policyAgentApi, "API must not be null");
80         this.policyAgentApi = policyAgentApi;
81         logger.debug("ctor: configured with client type {}", policyAgentApi.getClass().getName());
82     }
83
84     /*
85      * The fields are defined in the Policy Control Typescript interface.
86      */
87     @ApiOperation(value = "Gets the policy types from Near Realtime-RIC")
88     @GetMapping(POLICY_TYPES_METHOD)
89     @Secured({DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD})
90     public ResponseEntity<PolicyTypes> getAllPolicyTypes(HttpServletResponse response) {
91         logger.debug("getAllPolicyTypes");
92         return this.policyAgentApi.getAllPolicyTypes();
93     }
94
95     @ApiOperation(value = "Returns the policy instances for the given policy type.")
96     @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME)
97     @Secured({DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD})
98     public ResponseEntity<String> getPolicyInstances(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString) {
99         logger.debug("getPolicyInstances {}", policyTypeIdString);
100
101         ResponseEntity<PolicyInstances> response = this.policyAgentApi.getPolicyInstancesForType(policyTypeIdString);
102         if (!response.getStatusCode().is2xxSuccessful()) {
103             return new ResponseEntity<>(response.getStatusCode());
104         }
105         String json = gson.toJson(response.getBody());
106         return new ResponseEntity<>(json, response.getStatusCode());
107     }
108
109     @ApiOperation(value = "Returns a policy instance of a type")
110     @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME
111         + "}")
112     @Secured({DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD})
113     public ResponseEntity<String> getPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
114         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
115         logger.debug("getPolicyInstance {}:{}", policyTypeIdString, policyInstanceId);
116         return this.policyAgentApi.getPolicyInstance(policyInstanceId);
117     }
118
119     @ApiOperation(value = "Creates the policy instances for the given policy type.")
120     @PutMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{" + POLICY_INSTANCE_ID_NAME
121         + "}")
122     @Secured({DashboardConstants.ROLE_ADMIN})
123     public ResponseEntity<String> putPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
124         @RequestParam(name = "ric", required = true) String ric,
125         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId, @RequestBody String instance) {
126         logger.debug("putPolicyInstance typeId: {}, instanceId: {}, instance: {}", policyTypeIdString, policyInstanceId,
127             instance);
128         return this.policyAgentApi.putPolicy(policyTypeIdString, policyInstanceId, instance, ric);
129     }
130
131     @ApiOperation(value = "Deletes the policy instances for the given policy type.")
132     @DeleteMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{"
133         + POLICY_INSTANCE_ID_NAME + "}")
134     @Secured({DashboardConstants.ROLE_ADMIN})
135     public void deletePolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
136         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
137         logger.debug("deletePolicyInstance typeId: {}, instanceId: {}", policyTypeIdString, policyInstanceId);
138         this.policyAgentApi.deletePolicy(policyInstanceId);
139     }
140
141     @ApiOperation(value = "Returns the rics supporting the given policy type.")
142     @GetMapping("/rics")
143     @Secured({DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD})
144     public ResponseEntity<String> getRicsSupportingType(
145         @RequestParam(name = "policyType", required = true) String supportingPolicyType) {
146         logger.debug("getRicsSupportingType {}", supportingPolicyType);
147
148         ResponseEntity<Collection<String>> result = this.policyAgentApi.getRicsSupportingType(supportingPolicyType);
149         if (!result.getStatusCode().is2xxSuccessful()) {
150             return new ResponseEntity<>(result.getStatusCode());
151         }
152         String json = gson.toJson(result.getBody());
153         return new ResponseEntity<>(json, result.getStatusCode());
154     }
155
156 };