aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/dbcapi/service/AafTopicSetupService.java
blob: 29389aa0b64e14e70055a87cb94b4f61ca2d19e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dmaap.dbcapi.service;

import org.onap.dmaap.dbcapi.aaf.AafNamespace;
import org.onap.dmaap.dbcapi.aaf.AafRole;
import org.onap.dmaap.dbcapi.aaf.AafService;
import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
import org.onap.dmaap.dbcapi.model.ApiError;
import org.onap.dmaap.dbcapi.model.Topic;
import org.onap.dmaap.dbcapi.util.DmaapConfig;

import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.isNumeric;

class AafTopicSetupService extends BaseLoggingClass {

    private final AafService aafService;
    private final DmaapService dmaapService;
    private final DmaapConfig dmaapConfig;

    AafTopicSetupService(AafService aafService, DmaapService dmaapService, DmaapConfig dmaapConfig) {
        this.aafService = aafService;
        this.dmaapService = dmaapService;
        this.dmaapConfig = dmaapConfig;
    }

    ApiError aafTopicSetup(Topic topic) {

        try {
            String instance = ":topic." + topic.getFqtn();
            String topicPerm = dmaapService.getTopicPerm();
            DmaapPerm pubPerm = createPermission(topicPerm, instance, "pub");
            DmaapPerm subPerm = createPermission(topicPerm, instance, "sub");
            DmaapPerm viewPerm = createPermission(topicPerm, instance, "view");

            // creating Topic Roles was not an original feature.
            // For backwards compatibility, only do this if the feature is enabled.
            // Also, if the namespace of the topic is a foreign namespace, (i.e. not the same as our root ns)
            // then we likely don't have permission to create sub-ns and Roles so don't try.
            if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
                createNamespace(topic);

                AafRole pubRole = createRole(topic, "publisher");
                topic.setPublisherRole(pubRole.getFullyQualifiedRole());

                AafRole subRole = createRole(topic, "subscriber");
                topic.setSubscriberRole(subRole.getFullyQualifiedRole());

                grantPermToRole(pubRole, pubPerm);
                grantPermToRole(pubRole, viewPerm);

                grantPermToRole(subRole, subPerm);
                grantPermToRole(subRole, viewPerm);
            }

        } catch (TopicSetupException ex) {
            return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
        }
        return okStatus();
    }

    ApiError aafTopicCleanup(Topic topic) {
        try {
            if (performCleanup()) {
                String instance = ":topic." + topic.getFqtn();
                String topicPerm = dmaapService.getTopicPerm();
                removePermission(topicPerm, instance, "pub");
                removePermission(topicPerm, instance, "sub");
                removePermission(topicPerm, instance, "view");

                if (createTopicRoles() && topic.getFqtn().startsWith(getTopicsNsRoot())) {
                    removeNamespace(topic);
                }
            }
        } catch (TopicSetupException ex) {
            return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
        }
        return okStatus();
    }

    private String getTopicsNsRoot() throws TopicSetupException {
        String nsr = dmaapService.getDmaap().getTopicNsRoot();
        if (nsr == null) {
            throw new TopicSetupException(500,
                    "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
        }
        return nsr;
    }

    private DmaapPerm createPermission(String permission, String instance, String action) throws TopicSetupException {
        DmaapPerm perm = new DmaapPerm(permission, instance, action);
        int rc = aafService.addPerm(perm);
        if (rc != 201 && rc != 409) {
            throw new TopicSetupException(500,
                    format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
                            rc, perm, instance, action));
        }
        return perm;
    }

    private void grantPermToRole(AafRole aafRole, DmaapPerm perm) throws TopicSetupException {
        DmaapGrant g = new DmaapGrant(perm, aafRole.getFullyQualifiedRole());
        int rc = aafService.addGrant(g);
        if (rc != 201 && rc != 409) {
            String message = format("Grant of %s failed for %s", perm.toString(), aafRole.getFullyQualifiedRole());
            logger.warn(message);
            throw new TopicSetupException(rc, message);
        }
    }

    private void createNamespace(Topic topic) throws TopicSetupException {
        AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
        int rc = aafService.addNamespace(ns);
        if (rc != 201 && rc != 409) {
            throw new TopicSetupException(500,
                    format("Unexpected response from AAF: %d namespace=%s identity=%s",
                            rc, topic.getFqtn(), aafService.getIdentity()));
        }
    }

    private AafRole createRole(Topic topic, String roleName) throws TopicSetupException {
        AafRole role = new AafRole(topic.getFqtn(), roleName);
        int rc = aafService.addRole(role);
        if (rc != 201 && rc != 409) {
            throw new TopicSetupException(500,
                    format("Unexpected response from AAF: %d topic=%s role=%s",
                            rc, topic.getFqtn(), roleName));
        }
        return role;
    }

    private void removePermission(String permission, String instance, String action) throws TopicSetupException {
        DmaapPerm perm = new DmaapPerm(permission, instance, action);
        int rc = aafService.delPerm(perm, true);
        if (rc != 200 && rc != 404) {
            throw new TopicSetupException(500,
                    format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
                            rc, perm, instance, action));
        }
    }

    private void removeNamespace(Topic topic) throws TopicSetupException {
        AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
        int rc = aafService.delNamespace(ns, true);
        if (rc != 200 && rc != 404) {
            throw new TopicSetupException(500,
                    format("Unexpected response from AAF: %d namespace=%s identity=%s",
                            rc, topic.getFqtn(), aafService.getIdentity()));
        }
    }

    private ApiError okStatus() {
        return new ApiError(200, "OK");
    }

    private boolean createTopicRoles() {
        return "true".equalsIgnoreCase(dmaapConfig.getProperty("aaf.CreateTopicRoles", "true"));
    }

    private boolean performCleanup() {
        String deleteLevel = dmaapConfig.getProperty("MR.ClientDeleteLevel", "0");
        if (!isNumeric(deleteLevel)) {
            return false;
        }
        return Integer.valueOf(deleteLevel) >= 2;
    }

    private class TopicSetupException extends Exception {

        private final int code;
        private final String message;
        private final String fields;

        TopicSetupException(int code, String message) {
            this(code, message, "");
        }

        TopicSetupException(int code, String message, String fields) {
            this.code = code;
            this.message = message;
            this.fields = fields;
        }

        public int getCode() {
            return code;
        }

        @Override
        public String getMessage() {
            return message;
        }

        public String getFields() {
            return fields;
        }
    }
}