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