2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dmaap.dbcapi.service;
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;
32 import static java.lang.String.format;
33 import static org.apache.commons.lang3.StringUtils.isNumeric;
35 class AafTopicSetupService extends BaseLoggingClass {
37 private final AafService aafService;
38 private final DmaapService dmaapService;
39 private final DmaapConfig dmaapConfig;
41 AafTopicSetupService(AafService aafService, DmaapService dmaapService, DmaapConfig dmaapConfig) {
42 this.aafService = aafService;
43 this.dmaapService = dmaapService;
44 this.dmaapConfig = dmaapConfig;
47 ApiError aafTopicSetup(Topic topic) {
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");
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);
63 AafRole pubRole = createRole(topic, "publisher");
64 topic.setPublisherRole(pubRole.getFullyQualifiedRole());
66 AafRole subRole = createRole(topic, "subscriber");
67 topic.setSubscriberRole(subRole.getFullyQualifiedRole());
69 grantPermToRole(pubRole, pubPerm);
70 grantPermToRole(pubRole, viewPerm);
72 grantPermToRole(subRole, subPerm);
73 grantPermToRole(subRole, viewPerm);
76 } catch (TopicSetupException ex) {
77 logger.error("Exception in topic setup {}", ex.getMessage());
78 return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
83 ApiError aafTopicCleanup(Topic topic) {
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");
92 if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
93 removeNamespace(topic);
96 } catch (TopicSetupException ex) {
97 return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
102 private String getTopicsNsRoot() throws TopicSetupException {
103 String nsr = dmaapService.getDmaap().getTopicNsRoot();
105 throw new TopicSetupException(500,
106 "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
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));
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);
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()));
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));
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));
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()));
173 private ApiError okStatus() {
174 return new ApiError(200, "OK");
177 private boolean createTopicRoles() {
178 return "true".equalsIgnoreCase(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true"));
181 private boolean performCleanup() {
182 String deleteLevel = dmaapConfig.getProperty("MR.ClientDeleteLevel", "0");
183 if (!isNumeric(deleteLevel)) {
186 return Integer.valueOf(deleteLevel) >= 2;
189 private class TopicSetupException extends Exception {
191 private final int code;
192 private final String message;
193 private final String fields;
195 TopicSetupException(int code, String message) {
196 this(code, message, "");
199 TopicSetupException(int code, String message, String fields) {
201 this.message = message;
202 this.fields = fields;
205 public int getCode() {
210 public String getMessage() {
214 public String getFields() {