[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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 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     AafPermissionService(AafService aafService, DmaapService dmaapService) {
41         this.aafService = aafService;
42         this.dmaapService = dmaapService;
43     }
44
45     ApiError assignClientToRole(MR_Client client, String role) {
46         AafUserRole ur = new AafUserRole(client.getClientIdentity(), role);
47         int rc = aafService.addUserRole(ur);
48         if (rc != 201 && rc != 409) {
49             return handleErrorStatus(rc, client,
50                     format("Failed to add user %s to role %s", client.getClientIdentity(), role));
51         }
52         return handleOkStatus(client);
53     }
54
55     ApiError grantClientRolePerms(MR_Client client) {
56         return forEachClientAction(client, this::grantPermForClientRole);
57     }
58
59     private ApiError forEachClientAction(MR_Client client, PermissionUpdate permissionUpdate) {
60         try {
61             String instance = INSTANCE_PREFIX + client.getFqtn();
62
63             for (String action : client.getAction()) {
64                 permissionUpdate.execute(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     private void grantPermForClientRole(String clientRole, String instance, String action) throws PermissionServiceException {
74         if (clientRole != null) {
75             DmaapPerm perm = new DmaapPerm(dmaapService.getTopicPerm(), instance, action);
76             DmaapGrant g = new DmaapGrant(perm, clientRole);
77             int code = aafService.addGrant(g);
78             if (code != 201 && code != 409) {
79                 throw new PermissionServiceException(code, format("Grant of %s|%s|%s failed for %s",
80                         dmaapService.getTopicPerm(), instance, action, clientRole));
81             }
82         } else {
83             logger.warn("No Grant of {}|{}|{} because role is null ", dmaapService.getTopicPerm(), instance, action);
84         }
85     }
86
87     private ApiError handleErrorStatus(int code, MR_Client client, String message) {
88         ApiError apiError = new ApiError(code, message);
89         client.setStatus(DmaapObject_Status.INVALID);
90         logger.warn(apiError.getMessage());
91         return apiError;
92     }
93
94     private ApiError handleOkStatus(MR_Client client) {
95         client.setStatus(DmaapObject_Status.VALID);
96         return new ApiError(200, "OK");
97     }
98
99     private class PermissionServiceException extends Exception {
100         private final int code;
101         private final String message;
102
103         PermissionServiceException(int code, String message) {
104             this.code = code;
105             this.message = message;
106         }
107
108         public int getCode() {
109             return code;
110         }
111
112         @Override
113         public String getMessage() {
114             return message;
115         }
116     }
117
118     @FunctionalInterface
119     interface PermissionUpdate {
120         void execute(String clientRole, String instance, String action) throws PermissionServiceException;
121     }
122 }