[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / service / AafTopicSetupService.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 package org.onap.dmaap.dbcapi.service;
21
22 import org.onap.dmaap.dbcapi.aaf.AafNamespace;
23 import org.onap.dmaap.dbcapi.aaf.AafRole;
24 import org.onap.dmaap.dbcapi.aaf.AafService;
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.Topic;
30 import org.onap.dmaap.dbcapi.util.DmaapConfig;
31
32 import static java.lang.String.format;
33 import static org.apache.commons.lang3.StringUtils.isNumeric;
34
35 class AafTopicSetupService extends BaseLoggingClass {
36
37     private final AafService aafService;
38     private final DmaapService dmaapService;
39     private final DmaapConfig dmaapConfig;
40
41     AafTopicSetupService(AafService aafService, DmaapService dmaapService, DmaapConfig dmaapConfig) {
42         this.aafService = aafService;
43         this.dmaapService = dmaapService;
44         this.dmaapConfig = dmaapConfig;
45     }
46
47     ApiError aafTopicSetup(Topic topic) {
48
49         try {
50             String instance = ":topic." + topic.getFqtn();
51             String topicPerm = dmaapService.getTopicPerm();
52             DmaapPerm pubPerm = createPermission(topicPerm, instance, "pub");
53             DmaapPerm subPerm = createPermission(topicPerm, instance, "sub");
54             DmaapPerm viewPerm = createPermission(topicPerm, instance, "view");
55
56             // creating Topic Roles was not an original feature.
57             // For backwards compatibility, only do this if the feature is enabled.
58             // Also, if the namespace of the topic is a foreign namespace, (i.e. not the same as our root ns)
59             // then we likely don't have permission to create sub-ns and Roles so don't try.
60             if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
61                 createNamespace(topic);
62
63                 AafRole pubRole = createRole(topic, "publisher");
64                 topic.setPublisherRole(pubRole.getFullyQualifiedRole());
65
66                 AafRole subRole = createRole(topic, "subscriber");
67                 topic.setSubscriberRole(subRole.getFullyQualifiedRole());
68
69                 grantPermToRole(pubRole, pubPerm);
70                 grantPermToRole(pubRole, viewPerm);
71
72                 grantPermToRole(subRole, subPerm);
73                 grantPermToRole(subRole, viewPerm);
74             }
75
76         } catch (TopicSetupException ex) {
77             logger.error("Exception in topic setup {}", ex.getMessage());
78             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
79         }
80         return okStatus();
81     }
82
83     ApiError aafTopicCleanup(Topic topic) {
84         try {
85             if (performCleanup()) {
86                 String instance = ":topic." + topic.getFqtn();
87                 String topicPerm = dmaapService.getTopicPerm();
88                 removePermission(topicPerm, instance, "pub");
89                 removePermission(topicPerm, instance, "sub");
90                 removePermission(topicPerm, instance, "view");
91
92                 if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
93                     removeNamespace(topic);
94                 }
95             }
96         } catch (TopicSetupException ex) {
97             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
98         }
99         return okStatus();
100     }
101
102     private String getTopicsNsRoot() throws TopicSetupException {
103         String nsr = dmaapService.getDmaap().getTopicNsRoot();
104         if (nsr == null) {
105             throw new TopicSetupException(500,
106                     "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
107         }
108         return nsr;
109     }
110
111     private DmaapPerm createPermission(String permission, String instance, String action) throws TopicSetupException {
112         DmaapPerm perm = new DmaapPerm(permission, instance, action);
113         int rc = aafService.addPerm(perm);
114         if (rc != 201 && rc != 409) {
115             throw new TopicSetupException(500,
116                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
117                             rc, perm, instance, action));
118         }
119         return perm;
120     }
121
122     private void grantPermToRole(AafRole aafRole, DmaapPerm perm) throws TopicSetupException {
123         DmaapGrant g = new DmaapGrant(perm, aafRole.getFullyQualifiedRole());
124         int rc = aafService.addGrant(g);
125         if (rc != 201 && rc != 409) {
126             String message = format("Grant of %s failed for %s", perm.toString(), aafRole.getFullyQualifiedRole());
127             logger.warn(message);
128             throw new TopicSetupException(rc, message);
129         }
130     }
131
132     private void createNamespace(Topic topic) throws TopicSetupException {
133         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
134         int rc = aafService.addNamespace(ns);
135         if (rc != 201 && rc != 409) {
136             throw new TopicSetupException(500,
137                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
138                             rc, topic.getFqtn(), aafService.getIdentity()));
139         }
140     }
141
142     private AafRole createRole(Topic topic, String roleName) throws TopicSetupException {
143         AafRole role = new AafRole(topic.getFqtn(), roleName);
144         int rc = aafService.addRole(role);
145         if (rc != 201 && rc != 409) {
146             throw new TopicSetupException(500,
147                     format("Unexpected response from AAF: %d topic=%s role=%s",
148                             rc, topic.getFqtn(), roleName));
149         }
150         return role;
151     }
152
153     private void removePermission(String permission, String instance, String action) throws TopicSetupException {
154         DmaapPerm perm = new DmaapPerm(permission, instance, action);
155         int rc = aafService.delPerm(perm, true);
156         if (rc != 200 && rc != 404) {
157             throw new TopicSetupException(500,
158                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
159                             rc, perm, instance, action));
160         }
161     }
162
163     private void removeNamespace(Topic topic) throws TopicSetupException {
164         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
165         int rc = aafService.delNamespace(ns, true);
166         if (rc != 200 && rc != 404) {
167             throw new TopicSetupException(500,
168                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
169                             rc, topic.getFqtn(), aafService.getIdentity()));
170         }
171     }
172
173     private ApiError okStatus() {
174         return new ApiError(200, "OK");
175     }
176
177     private boolean createTopicRoles() {
178         return "true".equalsIgnoreCase(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true"));
179     }
180
181     private boolean performCleanup() {
182         String deleteLevel = dmaapConfig.getProperty("MR.ClientDeleteLevel", "0");
183         if (!isNumeric(deleteLevel)) {
184             return false;
185         }
186         return Integer.valueOf(deleteLevel) >= 2;
187     }
188
189     private class TopicSetupException extends Exception {
190
191         private final int code;
192         private final String message;
193         private final String fields;
194
195         TopicSetupException(int code, String message) {
196             this(code, message, "");
197         }
198
199         TopicSetupException(int code, String message, String fields) {
200             this.code = code;
201             this.message = message;
202             this.fields = fields;
203         }
204
205         public int getCode() {
206             return code;
207         }
208
209         @Override
210         public String getMessage() {
211             return message;
212         }
213
214         public String getFields() {
215             return fields;
216         }
217     }
218 }