2b64e8d1a734a6e641ba4d1a9e199fa064d92981
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / A1MediatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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
25 import org.oransc.ric.a1med.client.api.A1MediatorApi;
26 import org.oransc.ric.a1med.client.model.PolicyTypeSchema;
27 import org.oransc.ric.portal.dashboard.DashboardApplication;
28 import org.oransc.ric.portal.dashboard.DashboardConstants;
29 import org.oransc.ric.portal.dashboard.config.A1MediatorApiBuilder;
30 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
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.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.RestController;
44
45 import io.swagger.annotations.ApiOperation;
46 import io.swagger.annotations.ApiParam;
47
48 /**
49  * Proxies calls from the front end to the A1 Mediator API to get and put
50  * policies. All methods are deliberately kept ignorant of the data format.
51  * 
52  * If a method throws RestClientResponseException, it is handled by
53  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
54  * which returns status 502. All other exceptions are handled by Spring which
55  * returns status 500.
56  */
57 @RestController
58 @RequestMapping(value = A1MediatorController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
59 public class A1MediatorController {
60
61         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62
63         // Publish paths in constants so tests are easy to write
64         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/a1-p";
65         public static final String PP_TYPE_ID = "poltype";
66         public static final String PP_INST_ID = "polinst";
67         // The get- and put-instance methods use the same path
68         private static final String POLICY_INSTANCE_METHOD_PATH = /* controller path + */ DashboardConstants.RIC_INSTANCE_KEY
69                         + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + PP_TYPE_ID + "/{" + PP_TYPE_ID + "}/" + PP_INST_ID
70                         + "/{" + PP_INST_ID + "}";
71
72         // Populated by the autowired constructor
73         private final A1MediatorApiBuilder a1MediatorClientBuilder;
74
75         @Autowired
76         public A1MediatorController(final A1MediatorApiBuilder a1MediatorApiBuilder) {
77                 Assert.notNull(a1MediatorApiBuilder, "builder must not be null");
78                 this.a1MediatorClientBuilder = a1MediatorApiBuilder;
79                 if (logger.isDebugEnabled())
80                         logger.debug("ctor: configured with builder type {}", a1MediatorApiBuilder.getClass().getName());
81         }
82
83         @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
84         @GetMapping(DashboardConstants.VERSION_METHOD)
85         // No role required
86         public SuccessTransport getA1MediatorClientVersion() {
87                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1MediatorApi.class));
88         }
89
90         @ApiOperation(value = "Gets the registered policy type IDs from the A1 Mediator", response = Integer.class, responseContainer = "List")
91         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + PP_TYPE_ID)
92         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
93         public List<Integer> getAllPolicyTypes(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
94                 logger.debug("getAllPolicyTypes: instance {}", instanceKey);
95                 Object result = a1MediatorClientBuilder.getA1MediatorApi(instanceKey).a1ControllerGetAllPolicyTypes();
96                 @SuppressWarnings("unchecked")
97                 List<Integer> result2 = (List<Integer>) result;
98                 return result2;
99         }
100
101         @ApiOperation(value = "Gets the specified policy type from the A1 Mediator", response = PolicyTypeSchema.class)
102         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + PP_TYPE_ID
103                         + "/{" + PP_TYPE_ID + "}")
104         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
105         public PolicyTypeSchema getPolicyType(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
106                         @PathVariable(PP_TYPE_ID) Integer policyTypeId) {
107                 logger.debug("getPolicyType: instance {} typeId {}", instanceKey, policyTypeId);
108                 return a1MediatorClientBuilder.getA1MediatorApi(instanceKey).a1ControllerGetPolicyType(policyTypeId);
109         }
110
111         @ApiOperation(value = "Gets the specified policy instance from the A1 Mediator")
112         @GetMapping(POLICY_INSTANCE_METHOD_PATH)
113         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
114         public Object getPolicyInstance(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
115                         @PathVariable(PP_TYPE_ID) Integer policyTypeId, //
116                         @PathVariable(PP_INST_ID) String policyInstanceId) {
117                 logger.debug("getPolicyInstance: instance {} typeId {} instanceId {}", instanceKey, policyTypeId,
118                                 policyInstanceId);
119                 return a1MediatorClientBuilder.getA1MediatorApi(instanceKey).a1ControllerGetPolicyInstance(policyTypeId,
120                                 policyInstanceId);
121         }
122
123         @ApiOperation(value = "Creates or replaces the specified policy instance at the A1 Mediator")
124         @PutMapping(POLICY_INSTANCE_METHOD_PATH)
125         @Secured({ DashboardConstants.ROLE_ADMIN })
126         public ResponseEntity<String> createPolicyInstance(
127                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
128                         @PathVariable(PP_TYPE_ID) Integer policyTypeId, //
129                         @PathVariable(PP_INST_ID) String policyInstanceId,
130                         @ApiParam(value = "Policy body") @RequestBody String policyBody) {
131                 logger.debug("createPolicyInstance: instance {} typeId {} instanceId {}", instanceKey, policyTypeId,
132                                 policyInstanceId);
133                 A1MediatorApi api = a1MediatorClientBuilder.getA1MediatorApi(instanceKey);
134                 api.a1ControllerCreateOrReplacePolicyInstance(policyTypeId, policyInstanceId, policyBody);
135                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
136         }
137
138 }