76085573e1cd1510c952c2dca7ea213b5dbc1c3a
[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             logger.error("Exception in topic setup {}", ex.getMessage());
76             return new ApiError(ex.getCode(), ex.getMessage(), ex.getFields());
77         }
78         return okStatus();
79     }
80
81     private String getTopicsNsRoot() throws TopicSetupException {
82         String nsr = dmaapService.getDmaap().getTopicNsRoot();
83         if (nsr == null) {
84             throw new TopicSetupException(500,
85                     "Unable to establish AAF namespace root: (check /dmaap object)", "topicNsRoot");
86         }
87         return nsr;
88     }
89
90     private DmaapPerm createPermission(String permission, String instance, String action) throws TopicSetupException {
91         DmaapPerm perm = new DmaapPerm(permission, instance, action);
92         int rc = aafService.addPerm(perm);
93         if (rc != 201 && rc != 409) {
94             throw new TopicSetupException(500,
95                     format("Unexpected response from AAF: %d permission=%s instance=%s action=%s",
96                             rc, perm, instance, action));
97         }
98         return perm;
99     }
100
101     private void grantPermToRole(AafRole aafRole, DmaapPerm perm) throws TopicSetupException {
102         DmaapGrant g = new DmaapGrant(perm, aafRole.getFullyQualifiedRole());
103         int rc = aafService.addGrant(g);
104         if (rc != 201 && rc != 409) {
105             String message = format("Grant of %s failed for %s", perm.toString(), aafRole.getFullyQualifiedRole());
106             logger.warn(message);
107             throw new TopicSetupException(rc, message);
108         }
109     }
110
111     private void createNamespace(Topic topic) throws TopicSetupException {
112         AafNamespace ns = new AafNamespace(topic.getFqtn(), aafService.getIdentity());
113         int rc = aafService.addNamespace(ns);
114         if (rc != 201 && rc != 409) {
115             throw new TopicSetupException(500,
116                     format("Unexpected response from AAF: %d namespace=%s identity=%s",
117                             rc, topic.getFqtn(), aafService.getIdentity()));
118         }
119     }
120
121     private AafRole createRole(Topic topic, String roleName) throws TopicSetupException {
122         int rc;
123         AafRole role = new AafRole(topic.getFqtn(), roleName);
124         rc = aafService.addRole(role);
125         if (rc != 201 && rc != 409) {
126             throw new TopicSetupException(500,
127                     format("Unexpected response from AAF: %d topic=%s role=%s",
128                             rc, topic.getFqtn(), roleName));
129         }
130         return role;
131     }
132
133     private ApiError okStatus() {
134         return new ApiError(200, "OK");
135     }
136
137     private class TopicSetupException extends Exception {
138         private final int code;
139         private final String message;
140         private final String fields;
141
142         TopicSetupException(int code, String message) {
143             this(code, message, "");
144         }
145
146         TopicSetupException(int code, String message, String fields) {
147             this.code = code;
148             this.message = message;
149             this.fields = fields;
150         }
151
152         public int getCode() {
153             return code;
154         }
155
156         @Override
157         public String getMessage() {
158             return message;
159         }
160
161         public String getFields() {
162             return fields;
163         }
164     }
165 }