2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.service;
25 import java.util.ArrayList;
26 import org.onap.dmaap.dbcapi.aaf.AafService;
27 import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
28 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
29 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
30 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
31 import org.onap.dmaap.dbcapi.authentication.ApiPerms;
32 import org.onap.dmaap.dbcapi.authentication.ApiPolicy;
33 import org.onap.dmaap.dbcapi.database.DatabaseClass;
34 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
35 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
36 import org.onap.dmaap.dbcapi.model.ApiError;
37 import org.onap.dmaap.dbcapi.model.Dmaap;
38 import org.onap.dmaap.dbcapi.model.MR_Client;
39 import org.onap.dmaap.dbcapi.model.Topic;
40 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
41 import org.onap.dmaap.dbcapi.util.DmaapConfig;
42 import org.onap.dmaap.dbcapi.util.Singleton;
44 public class DmaapService extends BaseLoggingClass {
47 private Singleton<Dmaap> dmaapholder = DatabaseClass.getDmaap();
48 private static String noEnvironmentPrefix;
51 String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
52 String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
54 private boolean multiSite;
57 public DmaapService() {
58 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
59 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
60 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
62 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
63 noEnvironmentPrefix = p.getProperty( "AAF.NoEnvironmentPrefix", "org.onap");
65 logger.info( "DmaapService settings: " +
66 " topicFactory=" + topicFactory +
67 " topicMgrRole=" + topicMgrRole +
69 " multisite=" + multiSite +
70 " noEnvironmentPrefix=" + noEnvironmentPrefix
75 public Dmaap getDmaap() {
76 logger.info( "entering getDmaap()" );
77 return(dmaapholder.get());
80 public Dmaap addDmaap( Dmaap nd ) {
82 logger.info( "entering addDmaap()" );
83 Dmaap dmaap = dmaapholder.get();
84 if ( dmaap.getVersion().equals( "0")) {
87 dmaapholder.update(nd);
89 AafService aaf = new AafServiceImpl( ServiceType.AAF_Admin);
90 ApiPolicy apiPolicy = new ApiPolicy();
91 if ( apiPolicy.isPermissionClassSet() ) {
92 ApiPerms p = new ApiPerms();
95 boolean anythingWrong = false;
98 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
101 if ( anythingWrong ) {
102 dmaap.setStatus(DmaapObject_Status.INVALID);
105 dmaap.setStatus(DmaapObject_Status.VALID);
108 dmaapholder.update(dmaap);
118 public Dmaap updateDmaap( Dmaap nd ) {
119 logger.info( "entering updateDmaap()" );
121 boolean anythingWrong = false;
123 Dmaap dmaap = dmaapholder.get();
125 // some triggers for when we attempt to reprovision perms and MMA topic:
126 // - if the DMaaP Name changes
127 // - if the version is 0 (this is a handy test to force this processing by updating the DB)
128 // - if the object is invalid, reprocessing might fix it.
129 if ( ! dmaap.isStatusValid() || ! nd.getDmaapName().equals(dmaap.getDmaapName()) || dmaap.getVersion().equals( "0") ) {
131 dmaapholder.update(nd); //need to set this so the following perms will pick up any new vals.
132 //dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
133 ApiPolicy apiPolicy = new ApiPolicy();
134 if ( apiPolicy.isPermissionClassSet()) {
135 ApiPerms p = new ApiPerms();
138 AafService aaf = new AafServiceImpl( ServiceType.AAF_Admin);
140 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
144 if ( anythingWrong ) {
145 nd.setStatus(DmaapObject_Status.INVALID);
148 nd.setStatus(DmaapObject_Status.VALID);
151 dmaapholder.update(nd); // may need to update status...
152 return(dmaapholder.get());
156 public String getTopicPerm(){
157 Dmaap dmaap = dmaapholder.get();
158 return getTopicPerm( dmaap.getDmaapName() );
160 public String getTopicPerm( String val ) {
161 Dmaap dmaap = dmaapholder.get();
162 String nsRoot = dmaap.getTopicNsRoot();
163 if ( nsRoot == null ) { return null; }
166 // in ONAP Casablanca, we assume no distinction of environments reflected in topic namespace
167 if ( nsRoot.startsWith(noEnvironmentPrefix) ) {
168 t = nsRoot + ".mr.topic";
170 t = nsRoot + "." + val + ".mr.topic";
175 public String getBridgeAdminFqtn(){
176 Dmaap dmaap = dmaapholder.get();
177 String topic = dmaap.getBridgeAdminTopic();
179 // check if this is already an fqtn (contains a dot)
180 // otherwise build it
181 if ( topic.indexOf('.') < 0 ) {
182 topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
187 private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
188 String[] actions = { "create", "destroy" };
189 String instance = ":" + nd.getTopicNsRoot() + "." + nd.getDmaapName() + ".mr.topic:" + nd.getTopicNsRoot() + "." + nd.getDmaapName();
191 for( String action : actions ) {
193 DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
195 int rc = aaf.addPerm( perm );
196 if ( rc != 201 && rc != 409 ) {
197 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
201 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
202 rc = aaf.addGrant( grant );
203 if ( rc != 201 && rc != 409 ) {
204 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
209 String t = nd.getTopicNsRoot() +"." + nd.getDmaapName() + ".mr.topic";
210 String[] s = { "view", "pub", "sub" };
214 for( String action : actions ) {
216 DmaapPerm perm = new DmaapPerm( t, instance, action );
218 int rc = aaf.addPerm( perm );
219 if ( rc != 201 && rc != 409 ) {
220 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
224 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
225 rc = aaf.addGrant( grant );
226 if ( rc != 201 && rc != 409 ) {
227 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
235 public boolean testCreateMmaTopic() {
237 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
238 String unit_test = p.getProperty( "UnitTest", "No" );
239 if ( unit_test.equals( "Yes" ) ) {
240 return createMmaTopic();
245 // create the special topic for MMA provisioning.
246 // return true indicating a problem in topic creation,
247 // else false means it was ok (created or previously existed)
248 private boolean createMmaTopic() {
250 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
251 Dmaap dmaap = dmaapholder.get();
253 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
254 String[] actions = { "pub", "sub", "view" };
255 String centralMR = new DcaeLocationService().getCentralLocation();
256 if ( centralMR == null ) {
259 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
261 // first client is the Role used by Bus Controller to send messages to MMA
262 String provRole = p.getProperty("MM.ProvRole");
263 MR_Client nClient = new MR_Client();
264 nClient.setAction(actions);
265 nClient.setClientRole(provRole);
266 nClient.setDcaeLocationName(centralMR);
267 clients.add( nClient );
269 // second client is the Role used by MMA to listen to messages from Bus Controller
270 String agentRole = p.getProperty("MM.AgentRole");
271 nClient = new MR_Client();
272 nClient.setAction(actions);
273 nClient.setClientRole(agentRole);
274 nClient.setDcaeLocationName(centralMR);
275 clients.add( nClient );
278 Topic mmaTopic = new Topic().init();
279 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
280 mmaTopic.setClients(clients);
281 mmaTopic.setOwner("BusController");
282 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
283 mmaTopic.setTnxEnabled("false");
284 mmaTopic.setPartitionCount("1"); // a single partition should guarantee message order
287 ApiError err = new ApiError();
288 TopicService svc = new TopicService();
290 @SuppressWarnings("unused")
291 Topic nTopic = svc.addTopic(mmaTopic, err, true);
292 if ( err.is2xx() || err.getCode() == 409 ) {
295 } catch ( Exception e) {
296 errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
298 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());