386d43c1ad4c85e6a73d4d051ba2a5c25c60d3da
[nonrtric.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / A1Controller.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 java.lang.invoke.MethodHandles;
23 import java.util.List;
24 import javax.servlet.http.HttpServletResponse;
25 import org.oransc.ric.a1controller.client.api.A1ControllerApi;
26 import org.oransc.ric.a1controller.client.model.InputNRRidPTidPIidPISchema;
27 import org.oransc.ric.a1controller.client.model.InputNRRidPTidPIidPISchemaInput;
28 import org.oransc.ric.a1controller.client.model.InputNRRidPTidPIidSchema;
29 import org.oransc.ric.a1controller.client.model.InputNRRidPTidPIidSchemaInput;
30 import org.oransc.ric.a1controller.client.model.InputNRRidPTidSchema;
31 import org.oransc.ric.a1controller.client.model.InputNRRidPTidSchemaInput;
32 import org.oransc.ric.a1controller.client.model.InputNRRidSchema;
33 import org.oransc.ric.a1controller.client.model.InputNRRidSchemaInput;
34 import org.oransc.ric.a1controller.client.model.OutputDescNamePTSchema;
35 import org.oransc.ric.a1controller.client.model.OutputDescNamePTSchemaOutput;
36 import org.oransc.ric.a1controller.client.model.OutputPISchema;
37 import org.oransc.ric.a1controller.client.model.OutputPIidsListSchema;
38 import org.oransc.ric.a1controller.client.model.OutputPTidsListSchema;
39 import org.oransc.ric.portal.dashboard.DashboardApplication;
40 import org.oransc.ric.portal.dashboard.DashboardConstants;
41 import org.oransc.ric.portal.dashboard.model.PolicyInstance;
42 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
43 import org.oransc.ric.portal.dashboard.model.PolicyType;
44 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
45 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.http.MediaType;
50 import org.springframework.security.access.annotation.Secured;
51 import org.springframework.util.Assert;
52 import org.springframework.web.bind.annotation.DeleteMapping;
53 import org.springframework.web.bind.annotation.GetMapping;
54 import org.springframework.web.bind.annotation.PathVariable;
55 import org.springframework.web.bind.annotation.PutMapping;
56 import org.springframework.web.bind.annotation.RequestBody;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.RestController;
59 import io.swagger.annotations.ApiOperation;
60
61 /**
62  * Proxies calls from the front end to the A1 Controller via the A1 Mediator
63  * API.
64  *
65  * If a method throws RestClientResponseException, it is handled by
66  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception,
67  * org.springframework.web.context.request.WebRequest)}
68  * which returns status 502. All other exceptions are handled by Spring which
69  * returns status 500.
70  */
71 @RestController
72 @RequestMapping(value = A1Controller.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
73 public class A1Controller {
74
75     private static final String NEAR_RT_RIC_ID = "NearRtRic1";
76
77         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
78
79         // Publish paths in constants so tests are easy to write
80         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/policy";
81         // Endpoints
82         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
83         public static final String POLICY_TYPES_METHOD = "policytypes";
84         public static final String POLICY_TYPE_ID_NAME = "policy_type_id";
85         public static final String POLICIES_NAME = "policies";
86         public static final String POLICY_INSTANCE_ID_NAME = "policy_instance_id";
87
88         // Populated by the autowired constructor
89         private final A1ControllerApi a1ControllerApi;
90
91         @Autowired
92         public A1Controller(final A1ControllerApi A1ControllerApi) {
93                 Assert.notNull(A1ControllerApi, "API must not be null");
94                 this.a1ControllerApi = A1ControllerApi;
95                 if (logger.isDebugEnabled())
96                         logger.debug("ctor: configured with client type {}", A1ControllerApi.getClass().getName());
97         }
98
99         @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.",
100                 response = SuccessTransport.class)
101         @GetMapping(VERSION_METHOD)
102         // No role required
103         public SuccessTransport getA1ControllerClientVersion() {
104                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1ControllerApi.class));
105         }
106
107         /*
108          * The fields are defined in the A1Control Typescript interface.
109          */
110         @ApiOperation(value = "Gets the policy types from Near Realtime-RIC via the A1 Controller API")
111         @GetMapping(POLICY_TYPES_METHOD)
112         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
113         public Object getAllPolicyTypes(HttpServletResponse response) {
114                 logger.debug("getAllPolicyTypes");
115                 InputNRRidSchemaInput nrrid = new InputNRRidSchemaInput();
116                 nrrid.setNearRtRicId(NEAR_RT_RIC_ID);
117                 InputNRRidSchema inputSchema = new InputNRRidSchema();
118                 inputSchema.setInput(nrrid);
119                 OutputPTidsListSchema outputPTidsListSchema =
120                         a1ControllerApi.a1ControllerGetAllPolicyTypes(inputSchema);
121                 List<Integer> policyTypeIds = outputPTidsListSchema.getOutput().getPolicyTypeIdList();
122                 PolicyTypes policyTypes = new PolicyTypes();
123                 InputNRRidPTidSchema typeSchema = new InputNRRidPTidSchema();
124                 InputNRRidPTidSchemaInput typeId = new InputNRRidPTidSchemaInput();
125                 typeId.setNearRtRicId(NEAR_RT_RIC_ID);
126                 for (Integer policyTypeId : policyTypeIds) {
127                         typeId.setPolicyTypeId(policyTypeId);
128                         typeSchema.setInput(typeId);
129                         OutputDescNamePTSchema controllerGetPolicyType =
130                                 a1ControllerApi.a1ControllerGetPolicyType(typeSchema);
131                         OutputDescNamePTSchemaOutput policyTypeSchema = controllerGetPolicyType.getOutput();
132                         PolicyType type = new PolicyType(policyTypeId, policyTypeSchema.getName(),
133                                         policyTypeSchema.getDescription(), policyTypeSchema.getPolicyType().toString());
134                         policyTypes.add(type);
135                 }
136                 return policyTypes;
137         }
138
139         @ApiOperation(value = "Returns the policy instances for the given policy type.")
140         @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME)
141         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
142         public Object getPolicyInstances(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString) {
143                 logger.debug("getPolicyInstances {}", policyTypeIdString);
144                 InputNRRidPTidSchemaInput typeIdInput = new InputNRRidPTidSchemaInput();
145                 typeIdInput.setNearRtRicId(NEAR_RT_RIC_ID);
146                 Integer policyTypeId = Integer.decode(policyTypeIdString);
147                 typeIdInput.setPolicyTypeId(policyTypeId);
148                 InputNRRidPTidSchema inputSchema = new InputNRRidPTidSchema();
149                 inputSchema.setInput(typeIdInput);
150                 OutputPIidsListSchema controllerGetAllInstancesForType =
151                         a1ControllerApi.a1ControllerGetAllInstancesForType(inputSchema);
152                 List<String> instancesForType = controllerGetAllInstancesForType.getOutput().getPolicyInstanceIdList();
153                 PolicyInstances instances = new PolicyInstances();
154                 InputNRRidPTidPIidSchemaInput instanceIdInput = new InputNRRidPTidPIidSchemaInput();
155                 instanceIdInput.setNearRtRicId(NEAR_RT_RIC_ID);
156                 instanceIdInput.setPolicyTypeId(policyTypeId);
157                 InputNRRidPTidPIidSchema instanceInputSchema = new InputNRRidPTidPIidSchema();
158                 for (String instanceId : instancesForType) {
159                         instanceIdInput.setPolicyInstanceId(instanceId);
160                         instanceInputSchema.setInput(instanceIdInput);
161                         OutputPISchema policyInstance =
162                                 a1ControllerApi.a1ControllerGetPolicyInstance(instanceInputSchema);
163                         PolicyInstance instance =
164                                 new PolicyInstance(instanceId, policyInstance.getOutput().getPolicyInstance());
165                         instances.add(instance);
166                 }
167                 return instances;
168         }
169
170         @ApiOperation(value = "Returns a policy instance of a type")
171         @GetMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{"
172                 + POLICY_INSTANCE_ID_NAME + "}")
173         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
174         public Object getPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
175                         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
176                 logger.debug("getPolicyInstance {}:{}", policyTypeIdString, policyInstanceId);
177                 InputNRRidPTidPIidSchemaInput instanceIdInput = new InputNRRidPTidPIidSchemaInput();
178                 instanceIdInput.setNearRtRicId(NEAR_RT_RIC_ID);
179                 instanceIdInput.setPolicyTypeId(Integer.decode(policyTypeIdString));
180                 instanceIdInput.setPolicyInstanceId(policyInstanceId);
181                 InputNRRidPTidPIidSchema inputSchema = new InputNRRidPTidPIidSchema();
182                 inputSchema.setInput(instanceIdInput);
183                 OutputPISchema policyInstance = a1ControllerApi.a1ControllerGetPolicyInstance(inputSchema);
184                 return policyInstance.getOutput().getPolicyInstance();
185         }
186
187         @ApiOperation(value = "Creates the policy instances for the given policy type.")
188         @PutMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{"
189                 + POLICY_INSTANCE_ID_NAME + "}")
190         @Secured({ DashboardConstants.ROLE_ADMIN })
191         public void putPolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
192                         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId, @RequestBody String instance) {
193                 logger.debug("putPolicyInstance typeId: {}, instanceId: {}, instance: {}", policyTypeIdString,
194                         policyInstanceId, instance);
195                 InputNRRidPTidPIidPISchemaInput createInstanceInput = new InputNRRidPTidPIidPISchemaInput();
196                 createInstanceInput.setNearRtRicId(NEAR_RT_RIC_ID);
197                 createInstanceInput.setPolicyTypeId(Integer.decode(policyTypeIdString));
198                 createInstanceInput.setPolicyInstanceId(policyInstanceId);
199                 createInstanceInput.setPolicyInstance(instance);
200                 InputNRRidPTidPIidPISchema inputSchema = new InputNRRidPTidPIidPISchema();
201                 inputSchema.setInput(createInstanceInput);
202                 a1ControllerApi.a1ControllerCreatePolicyInstance(inputSchema);
203         }
204
205         @ApiOperation(value = "Deletes the policy instances for the given policy type.")
206         @DeleteMapping(POLICY_TYPES_METHOD + "/{" + POLICY_TYPE_ID_NAME + "}/" + POLICIES_NAME + "/{"
207                         + POLICY_INSTANCE_ID_NAME + "}")
208         @Secured({ DashboardConstants.ROLE_ADMIN })
209         public void deletePolicyInstance(@PathVariable(POLICY_TYPE_ID_NAME) String policyTypeIdString,
210                         @PathVariable(POLICY_INSTANCE_ID_NAME) String policyInstanceId) {
211                 logger.debug("deletePolicyInstance typeId: {}, instanceId: {}", policyTypeIdString, policyInstanceId);
212                 InputNRRidPTidPIidSchemaInput instanceIdInput = new InputNRRidPTidPIidSchemaInput();
213                 instanceIdInput.setNearRtRicId(NEAR_RT_RIC_ID);
214                 instanceIdInput.setPolicyTypeId(Integer.decode(policyTypeIdString));
215                 instanceIdInput.setPolicyInstanceId(policyInstanceId);
216                 InputNRRidPTidPIidSchema inputSchema = new InputNRRidPTidPIidSchema();
217                 inputSchema.setInput(instanceIdInput);
218                 a1ControllerApi.a1ControllerDeletePolicyInstance(inputSchema);
219         }
220 }