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.AafServiceImpl;
25 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
26 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
27 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
28 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
29 import org.onap.dmaap.dbcapi.model.ApiError;
30 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
31 import org.onap.dmaap.dbcapi.model.MR_Client;
33 import static java.lang.String.format;
35 public class AafPermissionService extends BaseLoggingClass {
37 private static final String INSTANCE_PREFIX = ":topic.";
38 private final AafService aafService;
39 private final DmaapService dmaapService;
41 public AafPermissionService() {
42 this(new AafServiceImpl(AafService.ServiceType.AAF_TopicMgr), new DmaapService());
45 AafPermissionService(AafService aafService, DmaapService dmaapService) {
46 this.aafService = aafService;
47 this.dmaapService = dmaapService;
50 ApiError assignClientToRole(MR_Client client, String role) {
51 AafUserRole ur = new AafUserRole(client.getClientIdentity(), role);
52 int rc = aafService.addUserRole(ur);
53 if (rc != 201 && rc != 409) {
54 return handleErrorStatus(rc, client,
55 format("Failed to add user %s to role %s", client.getClientIdentity(), role));
57 return handleOkStatus(client);
60 ApiError grantClientRolePerms(MR_Client client) {
61 return forEachClientAction(client, this::grantPermForClientRole);
64 ApiError revokeClientPerms(MR_Client client) {
65 return forEachClientAction(client, this::revokePermForClientRole);
68 private ApiError forEachClientAction(MR_Client client, PermissionUpdate permissionUpdate) {
70 String instance = INSTANCE_PREFIX + client.getFqtn();
72 for (String action : client.getAction()) {
73 permissionUpdate.execute(client.getClientRole(), instance, action);
76 } catch (PermissionServiceException e) {
77 return handleErrorStatus(e.getCode(), client, e.getMessage());
79 return handleOkStatus(client);
82 private void grantPermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
83 if (clientRole != null) {
84 DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
85 DmaapGrant g = new DmaapGrant(perm, clientRole);
86 int code = aafService.addGrant(g);
87 if (code != 201 && code != 409) {
88 throw new PermissionServiceException(code, format("Grant of %s|%s|%s failed for %s",
89 dmaapService.getTopicPerm(), instance, action, clientRole));
92 logger.warn("No Grant of {}|{}|{} because role is null ", dmaapService.getTopicPerm(), instance, action);
96 private void revokePermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
97 DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
98 DmaapGrant g = new DmaapGrant(perm, clientRole);
99 int code = aafService.delGrant(g);
100 if (code != 200 && code != 404) {
101 throw new PermissionServiceException(code, format("Revoke of %s|%s|%s failed for %s",
102 dmaapService.getTopicPerm(), instance, action, clientRole));
106 private ApiError handleErrorStatus(int code, MR_Client client, String message) {
107 ApiError apiError = new ApiError(code, message);
108 client.setStatus(DmaapObject_Status.INVALID);
109 logger.warn(apiError.getMessage());
113 private ApiError handleOkStatus(MR_Client client) {
114 client.setStatus(DmaapObject_Status.VALID);
115 return new ApiError(200, "OK");
118 private class PermissionServiceException extends Exception {
119 private final int code;
120 private final String message;
122 PermissionServiceException(int code, String message) {
124 this.message = message;
127 public int getCode() {
132 public String getMessage() {
138 interface PermissionUpdate {
139 void execute(String clientRole, String instance, String action) throws PermissionServiceException;