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;
31 import static java.lang.String.format;
33 class AafTopicSetupService extends BaseLoggingClass {
35 private final AafService aafService;
36 private final DmaapService dmaapService;
37 private final boolean createTopicRoles;
39 AafTopicSetupService(AafService aafService, DmaapService dmaapService, boolean createTopicRoles) {
40 this.aafService = aafService;
41 this.dmaapService = dmaapService;
42 this.createTopicRoles = createTopicRoles;
45 ApiError aafTopicSetup(Topic topic) {
48 String instance = ":topic." + topic.getFqtn();
49 String topicPerm = dmaapService.getTopicPerm();
50 DmaapPerm pubPerm = createPermission(topicPerm, instance, "pub");
51 DmaapPerm subPerm = createPermission(topicPerm, instance, "sub");
52 DmaapPerm viewPerm = createPermission(topicPerm, instance, "view");
54 // creating Topic Roles was not an original feature.
55 // For backwards compatibility, only do this if the feature is enabled.
56 // Also, if the namespace of the topic is a foreign namespace, (i.e. not the same as our root ns)
57 // then we likely don't have permission to create sub-ns and Roles so don't try.
58 if (createTopicRoles && topic.getFqtn().startsWith(getTopicsNsRoot())) {
59 createNamespace(topic);
61 AafRole pubRole = createRole(topic, "publisher");
62 topic.setPublisherRole(pubRole.getFullyQualifiedRole());
64 AafRole subRole = createRole(topic, "subscriber");
65 topic.setSubscriberRole(subRole.getFullyQualifiedRole());
67 grantPermToRole(pubRole, pubPerm);
68 grantPermToRole(pubRole, viewPerm);
70 grantPermToRole(subRole, subPerm);
71 grantPermToRole(subRole, viewPerm);
74 } catch (TopicSetupException ex) {
75 return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
80 ApiError aafTopicCleanup(Topic topic) {
84 private String getTopicsNsRoot() throws TopicSetupException {
85 String nsr = dmaapService.getDmaap().getTopicNsRoot();
87 throw new TopicSetupException(500,
88 "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
93 private DmaapPerm createPermission(String permission, String instance, String action) throws TopicSetupException {
94 DmaapPerm perm = new DmaapPerm(permission, instance, action);
95 int rc = aafService.addPerm(perm);
96 if (rc != 201 && rc != 409) {
97 throw new TopicSetupException(500,
98 format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
99 rc, perm, instance, action));
104 private void grantPermToRole(AafRole aafRole, DmaapPerm perm) throws TopicSetupException {
105 DmaapGrant g = new DmaapGrant(perm, aafRole.getFullyQualifiedRole());
106 int rc = aafService.addGrant(g);
107 if (rc != 201 && rc != 409) {
108 String message = format("Grant of %s failed for %s", perm.toString(), aafRole.getFullyQualifiedRole());
109 logger.warn(message);
110 throw new TopicSetupException(rc, message);
114 private void createNamespace(Topic topic) throws TopicSetupException {
115 AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
116 int rc = aafService.addNamespace(ns);
117 if (rc != 201 && rc != 409) {
118 throw new TopicSetupException(500,
119 format("Unexpected response from AAF: %d namespace=%s identity=%s",
120 rc, topic.getFqtn(), aafService.getIdentity()));
124 private AafRole createRole(Topic topic, String roleName) throws TopicSetupException {
126 AafRole role = new AafRole(topic.getFqtn(), roleName);
127 rc = aafService.addRole(role);
128 if (rc != 201 && rc != 409) {
129 throw new TopicSetupException(500,
130 format("Unexpected response from AAF: %d topic=%s role=%s",
131 rc, topic.getFqtn(), roleName));
136 private ApiError okStatus() {
137 return new ApiError(200, "OK");
140 private class TopicSetupException extends Exception {
141 private final int code;
142 private final String message;
143 private final String fields;
145 TopicSetupException(int code, String message) {
146 this(code, message, "");
149 TopicSetupException(int code, String message, String fields) {
151 this.message = message;
152 this.fields = fields;
155 public int getCode() {
160 public String getMessage() {
164 public String getFields() {