2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
23 import java.util.List;
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;
45 import io.swagger.annotations.ApiOperation;
46 import io.swagger.annotations.ApiParam;
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.
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
58 @RequestMapping(value = A1MediatorController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
59 public class A1MediatorController {
61 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
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 + "}";
72 // Populated by the autowired constructor
73 private final A1MediatorApiBuilder a1MediatorClientBuilder;
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());
83 @ApiOperation(value = "Gets the A1 client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
84 @GetMapping(DashboardConstants.VERSION_METHOD)
86 public SuccessTransport getA1MediatorClientVersion() {
87 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(A1MediatorApi.class));
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;
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);
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,
119 return a1MediatorClientBuilder.getA1MediatorApi(instanceKey).a1ControllerGetPolicyInstance(policyTypeId,
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,
133 A1MediatorApi api = a1MediatorClientBuilder.getA1MediatorApi(instanceKey);
134 api.a1ControllerCreateOrReplacePolicyInstance(policyTypeId, policyInstanceId, policyBody);
135 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);