AafService - interface was introduced
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / AafPermissionService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
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
21 package org.onap.dmaap.dbcapi.service;
22
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;
32
33 import static java.lang.String.format;
34
35 public class AafPermissionService extends BaseLoggingClass {
36
37     private static final String INSTANCE_PREFIX = ":topic.";
38     private final AafService aafService;
39     private final DmaapService dmaapService;
40
41     public AafPermissionService() {
42         this(new AafServiceImpl(AafService.ServiceType.AAF_TopicMgr), new DmaapService());
43     }
44
45     AafPermissionService(AafService aafService, DmaapService dmaapService) {
46         this.aafService = aafService;
47         this.dmaapService = dmaapService;
48     }
49
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));
56         }
57         return handleOkStatus(client);
58     }
59
60     ApiError grantClientRolePerms(MR_Client client) {
61         return forEachClientAction(client, this::grantPermForClientRole);
62     }
63
64     ApiError revokeClientPerms(MR_Client client) {
65         return forEachClientAction(client, this::revokePermForClientRole);
66     }
67
68     private ApiError forEachClientAction(MR_Client client, PermissionUpdate permissionUpdate) {
69         try {
70             String instance = INSTANCE_PREFIX + client.getFqtn();
71
72             for (String action : client.getAction()) {
73                 permissionUpdate.execute(client.getClientRole(), instance, action);
74             }
75
76         } catch (PermissionServiceException e) {
77             return handleErrorStatus(e.getCode(), client, e.getMessage());
78         }
79         return handleOkStatus(client);
80     }
81
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));
90             }
91         } else {
92             logger.warn("No Grant of {}|{}|{} because role is null ", dmaapService.getTopicPerm(), instance, action);
93         }
94     }
95
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));
103         }
104     }
105
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());
110         return apiError;
111     }
112
113     private ApiError handleOkStatus(MR_Client client) {
114         client.setStatus(DmaapObject_Status.VALID);
115         return new ApiError(200, "OK");
116     }
117
118     private class PermissionServiceException extends Exception {
119         private final int code;
120         private final String message;
121
122         PermissionServiceException(int code, String message) {
123             this.code = code;
124             this.message = message;
125         }
126
127         public int getCode() {
128             return code;
129         }
130
131         @Override
132         public String getMessage() {
133             return message;
134         }
135     }
136
137     @FunctionalInterface
138     interface PermissionUpdate {
139         void execute(String clientRole, String instance, String action) throws PermissionServiceException;
140     }
141 }