AafPermissionService refactor
[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.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;
31
32 import static java.lang.String.format;
33
34 public class AafPermissionService extends BaseLoggingClass {
35
36     private static final String INSTANCE_PREFIX = ":topic.";
37     private final AafService aafService;
38     private final DmaapService dmaapService;
39
40     public AafPermissionService() {
41         this(new AafService(AafService.ServiceType.AAF_TopicMgr), new DmaapService());
42     }
43
44     AafPermissionService(AafService aafService, DmaapService dmaapService) {
45         this.aafService = aafService;
46         this.dmaapService = dmaapService;
47     }
48
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));
55         }
56         return handleOkStatus(client);
57     }
58
59     ApiError grantClientRolePerms(MR_Client client) {
60         try {
61             String instance = INSTANCE_PREFIX + client.getFqtn();
62
63             for (String action : client.getAction()) {
64                 grantPermForClientRole(client.getClientRole(), instance, action);
65             }
66
67         } catch (PermissionServiceException e) {
68             return handleErrorStatus(e.getCode(), client, e.getMessage());
69         }
70         return handleOkStatus(client);
71     }
72
73     ApiError revokeClientPerms(MR_Client client) {
74         try {
75             String instance = INSTANCE_PREFIX + client.getFqtn();
76
77             for (String action : client.getAction()) {
78                 revokePermForClientRole(client.getClientRole(), instance, action);
79             }
80
81         } catch (PermissionServiceException e) {
82             return handleErrorStatus(e.getCode(), client, e.getMessage());
83         }
84         return handleOkStatus(client);
85     }
86
87     private void grantPermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
88         if (clientRole != null) {
89             DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
90             DmaapGrant g = new DmaapGrant(perm, clientRole);
91             int code = aafService.addGrant(g);
92             if (code != 201 && code != 409) {
93                 throw new PermissionServiceException(code, format("Grant of %s|%s|%s failed for %s",
94                         dmaapService.getTopicPerm(), instance, action, clientRole));
95             }
96         } else {
97             logger.warn("No Grant of {}|{}|{} because role is null ", dmaapService.getTopicPerm(), instance, action);
98         }
99     }
100
101     private void revokePermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
102         DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
103         DmaapGrant g = new DmaapGrant(perm, clientRole);
104         int code = aafService.delGrant(g);
105         if (code != 200 && code != 404) {
106             throw new PermissionServiceException(code, format("Revoke of %s|%s|%s failed for %s",
107                     dmaapService.getTopicPerm(), instance, action, clientRole));
108         }
109     }
110
111     private ApiError handleErrorStatus(int code, MR_Client client, String message) {
112         ApiError apiError = new ApiError(code, message);
113         client.setStatus(DmaapObject_Status.INVALID);
114         logger.warn(apiError.getMessage());
115         return apiError;
116     }
117
118     private ApiError handleOkStatus(MR_Client client) {
119         client.setStatus(DmaapObject_Status.VALID);
120         return new ApiError(200, "OK");
121     }
122
123     private class PermissionServiceException extends Exception {
124         private final int code;
125         private final String message;
126
127         PermissionServiceException(int code, String message) {
128             this.code = code;
129             this.message = message;
130         }
131
132         public int getCode() {
133             return code;
134         }
135
136         @Override
137         public String getMessage() {
138             return message;
139         }
140     }
141 }