AafPermissionService implementation
[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 public class AafPermissionService extends BaseLoggingClass {
33
34     private final AafService aafService;
35     private final DmaapService dmaapService;
36
37     public AafPermissionService() {
38         this(new AafService(AafService.ServiceType.AAF_TopicMgr), new DmaapService());
39     }
40
41     AafPermissionService(AafService aafService, DmaapService dmaapService) {
42         this.aafService = aafService;
43         this.dmaapService = dmaapService;
44     }
45
46     void assignIdentityToRole(MR_Client client, String role, ApiError err) {
47         okStatus(err);
48         AafUserRole ur = new AafUserRole(client.getClientIdentity(), role);
49         client.setStatus(DmaapObject_Status.VALID);
50         int rc = aafService.addUserRole(ur);
51         if (rc != 201 && rc != 409) {
52             client.setStatus(DmaapObject_Status.INVALID);
53             assignClientToRoleError(err, rc, client.getClientIdentity(), role);
54         }
55     }
56
57     void grantClientRolePerms(MR_Client client, ApiError err) {
58
59         okStatus(err);
60         String instance = ":topic." + client.getFqtn();
61         client.setStatus(DmaapObject_Status.VALID);
62
63         for (String action : client.getAction()) {
64             if (client.getClientRole() != null) {
65                 int rc = grantPermForClientRole(client.getClientRole(), instance, action);
66                 if (rc != 201 && rc != 409) {
67                     client.setStatus(DmaapObject_Status.INVALID);
68                     grantPermsError(err, rc, dmaapService.getTopicPerm(), instance, action, client.getClientRole());
69                 }
70
71             } else {
72                 logger.warn("No Grant of " + permissionFullName(dmaapService.getTopicPerm(), instance, action) + " because role is null ");
73             }
74         }
75     }
76
77     void revokeClientPerms(MR_Client client, ApiError err) {
78         okStatus(err);
79         String instance = ":topic." + client.getFqtn();
80         client.setStatus(DmaapObject_Status.VALID);
81
82         for (String action : client.getAction()) {
83
84             int rc = revokePermForClientRole(client.getClientRole(), instance, action);
85
86             if (rc != 200 && rc != 404) {
87                 client.setStatus(DmaapObject_Status.INVALID);
88                 revokePermsError(err, rc, dmaapService.getTopicPerm(), instance, action, client.getClientRole());
89             }
90         }
91
92     }
93
94     private int grantPermForClientRole(String clientRole, String instance, String action) {
95         DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
96         DmaapGrant g = new DmaapGrant(perm, clientRole);
97         return aafService.addGrant(g);
98     }
99
100     private int revokePermForClientRole(String clientRole, String instance, String action) {
101         DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
102         DmaapGrant g = new DmaapGrant(perm, clientRole);
103         return aafService.delGrant(g);
104     }
105
106     private void assignClientToRoleError(ApiError err, int code, String clientIdentity, String role) {
107         err.setCode(code);
108         err.setMessage("Failed to add user " + clientIdentity + "  to " + role);
109         logger.warn(err.getMessage());
110     }
111
112     private void grantPermsError(ApiError err, int code, String permission, String instance, String action, String role) {
113         err.setCode(code);
114         err.setMessage("Grant of " + permissionFullName(permission, instance, action) + " failed for " + role);
115         logger.warn(err.getMessage());
116     }
117
118     private void revokePermsError(ApiError err, int code, String permission, String instance, String action, String role) {
119         err.setCode(code);
120         err.setMessage("Revoke of " + permissionFullName(permission, instance, action) + " failed for " + role);
121         logger.warn(err.getMessage());
122     }
123
124     private String permissionFullName(String permission, String instance, String action) {
125         return permission + "|" + instance + "|" + action;
126     }
127
128     private void okStatus(ApiError err) {
129         err.setCode(200);
130         err.setMessage("OK");
131     }
132
133 }