d9dd4fd31b9a1234a99e8e8c906b0ea7299b898b
[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
31 import static java.lang.String.format;
32
33 class AafTopicSetupService extends BaseLoggingClass {
34
35     private final AafService aafService;
36     private final DmaapService dmaapService;
37     private final boolean createTopicRoles;
38
39     AafTopicSetupService(AafService aafService, DmaapService dmaapService, boolean createTopicRoles) {
40         this.aafService = aafService;
41         this.dmaapService = dmaapService;
42         this.createTopicRoles = createTopicRoles;
43     }
44
45     ApiError aafTopicSetup(Topic topic) {
46
47         try {
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");
53
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);
60
61                 AafRole pubRole = createRole(topic, "publisher");
62                 topic.setPublisherRole(pubRole.getFullyQualifiedRole());
63
64                 AafRole subRole = createRole(topic, "subscriber");
65                 topic.setSubscriberRole(subRole.getFullyQualifiedRole());
66
67                 grantPermToRole(pubRole, pubPerm);
68                 grantPermToRole(pubRole, viewPerm);
69
70                 grantPermToRole(subRole, subPerm);
71                 grantPermToRole(subRole, viewPerm);
72             }
73
74         } catch (TopicSetupException ex) {
75             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
76         }
77         return okStatus();
78     }
79
80     ApiError aafTopicCleanup(Topic topic) {
81         try {
82
83             String instance = ":topic." + topic.getFqtn();
84             String topicPerm = dmaapService.getTopicPerm();
85             removePermission(topicPerm, instance, "pub");
86             removePermission(topicPerm, instance, "sub");
87             removePermission(topicPerm, instance, "view");
88
89             if (createTopicRoles && topic.getFqtn().startsWith(getTopicsNsRoot())) {
90                 removeNamespace(topic);
91             }
92
93         } catch (TopicSetupException ex) {
94             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
95         }
96         return okStatus();
97     }
98
99     private String getTopicsNsRoot() throws TopicSetupException {
100         String nsr = dmaapService.getDmaap().getTopicNsRoot();
101         if (nsr == null) {
102             throw new TopicSetupException(500,
103                     "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
104         }
105         return nsr;
106     }
107
108     private DmaapPerm createPermission(String permission, String instance, String action) throws TopicSetupException {
109         DmaapPerm perm = new DmaapPerm(permission, instance, action);
110         int rc = aafService.addPerm(perm);
111         if (rc != 201 && rc != 409) {
112             throw new TopicSetupException(500,
113                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
114                             rc, perm, instance, action));
115         }
116         return perm;
117     }
118
119     private void grantPermToRole(AafRole aafRole, DmaapPerm perm) throws TopicSetupException {
120         DmaapGrant g = new DmaapGrant(perm, aafRole.getFullyQualifiedRole());
121         int rc = aafService.addGrant(g);
122         if (rc != 201 && rc != 409) {
123             String message = format("Grant of %s failed for %s", perm.toString(), aafRole.getFullyQualifiedRole());
124             logger.warn(message);
125             throw new TopicSetupException(rc, message);
126         }
127     }
128
129     private void createNamespace(Topic topic) throws TopicSetupException {
130         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
131         int rc = aafService.addNamespace(ns);
132         if (rc != 201 && rc != 409) {
133             throw new TopicSetupException(500,
134                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
135                             rc, topic.getFqtn(), aafService.getIdentity()));
136         }
137     }
138
139     private AafRole createRole(Topic topic, String roleName) throws TopicSetupException {
140         AafRole role = new AafRole(topic.getFqtn(), roleName);
141         int rc = aafService.addRole(role);
142         if (rc != 201 && rc != 409) {
143             throw new TopicSetupException(500,
144                     format("Unexpected response from AAF: %d topic=%s role=%s",
145                             rc, topic.getFqtn(), roleName));
146         }
147         return role;
148     }
149
150     private void removePermission(String permission, String instance, String action) throws TopicSetupException {
151         DmaapPerm perm = new DmaapPerm(permission, instance, action);
152         int rc = aafService.delPerm(perm);
153         if (rc != 200 && rc != 404) {
154             throw new TopicSetupException(500,
155                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
156                             rc, perm, instance, action));
157         }
158     }
159
160     private void removeNamespace(Topic topic) throws TopicSetupException {
161         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
162         int rc = aafService.delNamespace(ns);
163         if (rc != 200 && rc != 404) {
164             throw new TopicSetupException(500,
165                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
166                             rc, topic.getFqtn(), aafService.getIdentity()));
167         }
168     }
169
170     private ApiError okStatus() {
171         return new ApiError(200, "OK");
172     }
173
174     private class TopicSetupException extends Exception {
175         private final int code;
176         private final String message;
177         private final String fields;
178
179         TopicSetupException(int code, String message) {
180             this(code, message, "");
181         }
182
183         TopicSetupException(int code, String message, String fields) {
184             this.code = code;
185             this.message = message;
186             this.fields = fields;
187         }
188
189         public int getCode() {
190             return code;
191         }
192
193         @Override
194         public String getMessage() {
195             return message;
196         }
197
198         public String getFields() {
199             return fields;
200         }
201     }
202 }