2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
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
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
23 import org.onap.dmaap.dbcapi.aaf.AafService;
24 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
25 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
26 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
27 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
28 import org.onap.dmaap.dbcapi.model.ApiError;
29 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
30 import org.onap.dmaap.dbcapi.model.MR_Client;
32 import static java.lang.String.format;
34 public class AafPermissionService extends BaseLoggingClass {
36 private static final String INSTANCE_PREFIX = ":topic.";
37 private final AafService aafService;
38 private final DmaapService dmaapService;
40 public AafPermissionService() {
41 this(new AafService(AafService.ServiceType.AAF_TopicMgr), new DmaapService());
44 AafPermissionService(AafService aafService, DmaapService dmaapService) {
45 this.aafService = aafService;
46 this.dmaapService = dmaapService;
49 ApiError assignClientToRole(MR_Client client, String role) {
50 AafUserRole ur = new AafUserRole(client.getClientIdentity(), role);
51 int rc = aafService.addUserRole(ur);
52 if (rc != 201 && rc != 409) {
53 return handleErrorStatus(rc, client,
54 format("Failed to add user %s to role %s", client.getClientIdentity(), role));
56 return handleOkStatus(client);
59 ApiError grantClientRolePerms(MR_Client client) {
60 return forEachClientAction(client, this::grantPermForClientRole);
63 ApiError revokeClientPerms(MR_Client client) {
64 return forEachClientAction(client, this::revokePermForClientRole);
67 private ApiError forEachClientAction(MR_Client client, PermissionUpdate permissionUpdate) {
69 String instance = INSTANCE_PREFIX + client.getFqtn();
71 for (String action : client.getAction()) {
72 permissionUpdate.execute(client.getClientRole(), instance, action);
75 } catch (PermissionServiceException e) {
76 return handleErrorStatus(e.getCode(), client, e.getMessage());
78 return handleOkStatus(client);
81 private void grantPermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
82 if (clientRole != null) {
83 DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
84 DmaapGrant g = new DmaapGrant(perm, clientRole);
85 int code = aafService.addGrant(g);
86 if (code != 201 && code != 409) {
87 throw new PermissionServiceException(code, format("Grant of %s|%s|%s failed for %s",
88 dmaapService.getTopicPerm(), instance, action, clientRole));
91 logger.warn("No Grant of {}|{}|{} because role is null ", dmaapService.getTopicPerm(), instance, action);
95 private void revokePermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
96 DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
97 DmaapGrant g = new DmaapGrant(perm, clientRole);
98 int code = aafService.delGrant(g);
99 if (code != 200 && code != 404) {
100 throw new PermissionServiceException(code, format("Revoke of %s|%s|%s failed for %s",
101 dmaapService.getTopicPerm(), instance, action, clientRole));
105 private ApiError handleErrorStatus(int code, MR_Client client, String message) {
106 ApiError apiError = new ApiError(code, message);
107 client.setStatus(DmaapObject_Status.INVALID);
108 logger.warn(apiError.getMessage());
112 private ApiError handleOkStatus(MR_Client client) {
113 client.setStatus(DmaapObject_Status.VALID);
114 return new ApiError(200, "OK");
117 private class PermissionServiceException extends Exception {
118 private final int code;
119 private final String message;
121 PermissionServiceException(int code, String message) {
123 this.message = message;
126 public int getCode() {
131 public String getMessage() {
137 interface PermissionUpdate {
138 void execute(String clientRole, String instance, String action) throws PermissionServiceException;