use delete level property in AafTopicSetupService
[dmaap/dbcapi.git] / 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             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
78         }
79         return okStatus();
80     }
81
82     ApiError aafTopicCleanup(Topic topic) {
83         try {
84             if (performCleanup()) {
85                 String instance = ":topic." + topic.getFqtn();
86                 String topicPerm = dmaapService.getTopicPerm();
87                 removePermission(topicPerm, instance, "pub");
88                 removePermission(topicPerm, instance, "sub");
89                 removePermission(topicPerm, instance, "view");
90
91                 if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
92                     removeNamespace(topic);
93                 }
94             }
95         } catch (TopicSetupException ex) {
96             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
97         }
98         return okStatus();
99     }
100
101     private String getTopicsNsRoot() throws TopicSetupException {
102         String nsr = dmaapService.getDmaap().getTopicNsRoot();
103         if (nsr == null) {
104             throw new TopicSetupException(500,
105                     "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
106         }
107         return nsr;
108     }
109
110     private DmaapPerm createPermission(String permission, String instance, String action) throws TopicSetupException {
111         DmaapPerm perm = new DmaapPerm(permission, instance, action);
112         int rc = aafService.addPerm(perm);
113         if (rc != 201 && rc != 409) {
114             throw new TopicSetupException(500,
115                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
116                             rc, perm, instance, action));
117         }
118         return perm;
119     }
120
121     private void grantPermToRole(AafRole aafRole, DmaapPerm perm) throws TopicSetupException {
122         DmaapGrant g = new DmaapGrant(perm, aafRole.getFullyQualifiedRole());
123         int rc = aafService.addGrant(g);
124         if (rc != 201 && rc != 409) {
125             String message = format("Grant of %s failed for %s", perm.toString(), aafRole.getFullyQualifiedRole());
126             logger.warn(message);
127             throw new TopicSetupException(rc, message);
128         }
129     }
130
131     private void createNamespace(Topic topic) throws TopicSetupException {
132         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
133         int rc = aafService.addNamespace(ns);
134         if (rc != 201 && rc != 409) {
135             throw new TopicSetupException(500,
136                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
137                             rc, topic.getFqtn(), aafService.getIdentity()));
138         }
139     }
140
141     private AafRole createRole(Topic topic, String roleName) throws TopicSetupException {
142         AafRole role = new AafRole(topic.getFqtn(), roleName);
143         int rc = aafService.addRole(role);
144         if (rc != 201 && rc != 409) {
145             throw new TopicSetupException(500,
146                     format("Unexpected response from AAF: %d topic=%s role=%s",
147                             rc, topic.getFqtn(), roleName));
148         }
149         return role;
150     }
151
152     private void removePermission(String permission, String instance, String action) throws TopicSetupException {
153         DmaapPerm perm = new DmaapPerm(permission, instance, action);
154         int rc = aafService.delPerm(perm, true);
155         if (rc != 200 && rc != 404) {
156             throw new TopicSetupException(500,
157                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
158                             rc, perm, instance, action));
159         }
160     }
161
162     private void removeNamespace(Topic topic) throws TopicSetupException {
163         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
164         int rc = aafService.delNamespace(ns, true);
165         if (rc != 200 && rc != 404) {
166             throw new TopicSetupException(500,
167                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
168                             rc, topic.getFqtn(), aafService.getIdentity()));
169         }
170     }
171
172     private ApiError okStatus() {
173         return new ApiError(200, "OK");
174     }
175
176     private boolean createTopicRoles() {
177         return "true".equalsIgnoreCase(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true"));
178     }
179
180     private boolean performCleanup() {
181         String deleteLevel = dmaapConfig.getProperty("MR.ClientDeleteLevel", "0");
182         if (!isNumeric(deleteLevel)) {
183             return false;
184         }
185         return Integer.valueOf(deleteLevel) >= 2;
186     }
187
188     private class TopicSetupException extends Exception {
189
190         private final int code;
191         private final String message;
192         private final String fields;
193
194         TopicSetupException(int code, String message) {
195             this(code, message, "");
196         }
197
198         TopicSetupException(int code, String message, String fields) {
199             this.code = code;
200             this.message = message;
201             this.fields = fields;
202         }
203
204         public int getCode() {
205             return code;
206         }
207
208         @Override
209         public String getMessage() {
210             return message;
211         }
212
213         public String getFields() {
214             return fields;
215         }
216     }
217 }