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 return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
82 ApiError aafTopicCleanup(Topic topic) {
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");
91 if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
92 removeNamespace(topic);
95 } catch (TopicSetupException ex) {
96 return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
101 private String getTopicsNsRoot() throws TopicSetupException {
102 String nsr = dmaapService.getDmaap().getTopicNsRoot();
104 throw new TopicSetupException(500,
105 "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
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));
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);
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()));
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));
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));
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()));
172 private ApiError okStatus() {
173 return new ApiError(200, "OK");
176 private boolean createTopicRoles() {
177 return "true".equalsIgnoreCase(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true"));
180 private boolean performCleanup() {
181 String deleteLevel = dmaapConfig.getProperty("MR.ClientDeleteLevel", "0");
182 if (!isNumeric(deleteLevel)) {
185 return Integer.valueOf(deleteLevel) >= 2;
188 private class TopicSetupException extends Exception {
190 private final int code;
191 private final String message;
192 private final String fields;
194 TopicSetupException(int code, String message) {
195 this(code, message, "");
198 TopicSetupException(int code, String message, String fields) {
200 this.message = message;
201 this.fields = fields;
204 public int getCode() {
209 public String getMessage() {
213 public String getFields() {