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.AafServiceFactory;
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
73 Dmaap dmaap = dmaapholder.get();
74 logger.info( "DmaapService object values: " +
75 " dmaapName=" + dmaap.getDmaapName() +
76 " drProvURL=" + dmaap.getDrProvUrl() +
77 " version="+ dmaap.getVersion()
82 public Dmaap getDmaap() {
83 logger.info( "entering getDmaap()" );
84 return(dmaapholder.get());
87 public Dmaap addDmaap( Dmaap nd ) {
89 logger.info( "entering addDmaap()" );
90 Dmaap dmaap = dmaapholder.get();
91 if ( dmaap.getVersion().equals( "0")) {
94 dmaapholder.update(nd);
96 AafService aaf = new AafServiceFactory().initAafService(ServiceType.AAF_Admin);
97 ApiPolicy apiPolicy = new ApiPolicy();
98 if ( apiPolicy.isPermissionClassSet() ) {
99 ApiPerms p = new ApiPerms();
102 boolean anythingWrong = false;
105 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
108 if ( anythingWrong ) {
109 dmaap.setStatus(DmaapObject_Status.INVALID);
112 dmaap.setStatus(DmaapObject_Status.VALID);
115 dmaapholder.update(dmaap);
125 public Dmaap updateDmaap( Dmaap nd ) {
126 logger.info( "entering updateDmaap()" );
128 boolean anythingWrong = false;
130 Dmaap dmaap = dmaapholder.get();
132 // some triggers for when we attempt to reprovision perms and MMA topic:
133 // - if the DMaaP Name changes
134 // - if the version is 0 (this is a handy test to force this processing by updating the DB)
135 // - if the object is invalid, reprocessing might fix it.
136 if ( ! dmaap.isStatusValid() || ! nd.getDmaapName().equals(dmaap.getDmaapName()) || dmaap.getVersion().equals( "0") ) {
138 dmaapholder.update(nd); //need to set this so the following perms will pick up any new vals.
139 //dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
140 ApiPolicy apiPolicy = new ApiPolicy();
141 if ( apiPolicy.isPermissionClassSet()) {
142 ApiPerms p = new ApiPerms();
145 AafService aaf = new AafServiceFactory().initAafService(ServiceType.AAF_Admin);
147 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
151 if ( anythingWrong ) {
152 nd.setStatus(DmaapObject_Status.INVALID);
155 nd.setStatus(DmaapObject_Status.VALID);
158 dmaapholder.update(nd); // may need to update status...
159 return(dmaapholder.get());
163 public String getTopicPerm(){
164 Dmaap dmaap = dmaapholder.get();
165 return getTopicPerm( dmaap.getDmaapName() );
167 public String getTopicPerm( String val ) {
168 Dmaap dmaap = dmaapholder.get();
169 String nsRoot = dmaap.getTopicNsRoot();
170 if ( nsRoot == null ) { return null; }
173 // in ONAP Casablanca, we assume no distinction of environments reflected in topic namespace
174 if ( nsRoot.startsWith(noEnvironmentPrefix) ) {
175 t = nsRoot + ".mr.topic";
177 t = nsRoot + "." + val + ".mr.topic";
182 public String getBridgeAdminFqtn(){
183 Dmaap dmaap = dmaapholder.get();
184 String topic = dmaap.getBridgeAdminTopic();
186 // check if this is already an fqtn (contains a dot)
187 // otherwise build it
188 if ( topic.indexOf('.') < 0 ) {
189 topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
194 private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
195 String[] actions = { "create", "destroy" };
196 String instance = ":" + nd.getTopicNsRoot() + "." + nd.getDmaapName() + ".mr.topic:" + nd.getTopicNsRoot() + "." + nd.getDmaapName();
198 for( String action : actions ) {
200 DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
202 int rc = aaf.addPerm( perm );
203 if ( rc != 201 && rc != 409 ) {
204 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
208 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
209 rc = aaf.addGrant( grant );
210 if ( rc != 201 && rc != 409 ) {
211 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
216 String t = nd.getTopicNsRoot() +"." + nd.getDmaapName() + ".mr.topic";
217 String[] s = { "view", "pub", "sub" };
221 for( String action : actions ) {
223 DmaapPerm perm = new DmaapPerm( t, instance, action );
225 int rc = aaf.addPerm( perm );
226 if ( rc != 201 && rc != 409 ) {
227 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
231 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
232 rc = aaf.addGrant( grant );
233 if ( rc != 201 && rc != 409 ) {
234 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
242 public boolean testCreateMmaTopic() {
244 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
245 String unit_test = p.getProperty( "UnitTest", "No" );
246 if ( unit_test.equals( "Yes" ) ) {
247 return createMmaTopic();
252 // create the special topic for MMA provisioning.
253 // return true indicating a problem in topic creation,
254 // else false means it was ok (created or previously existed)
255 private boolean createMmaTopic() {
257 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
258 Dmaap dmaap = dmaapholder.get();
260 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
261 String[] actions = { "pub", "sub", "view" };
262 String centralMR = new DcaeLocationService().getCentralLocation();
263 if ( centralMR == null ) {
266 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
268 // first client is the Role used by Bus Controller to send messages to MMA
269 String provRole = p.getProperty("MM.ProvRole");
270 MR_Client nClient = new MR_Client();
271 nClient.setAction(actions);
272 nClient.setClientRole(provRole);
273 nClient.setDcaeLocationName(centralMR);
274 clients.add( nClient );
276 // second client is the Role used by MMA to listen to messages from Bus Controller
277 String agentRole = p.getProperty("MM.AgentRole");
278 nClient = new MR_Client();
279 nClient.setAction(actions);
280 nClient.setClientRole(agentRole);
281 nClient.setDcaeLocationName(centralMR);
282 clients.add( nClient );
285 Topic mmaTopic = new Topic().init();
286 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
287 mmaTopic.setClients(clients);
288 mmaTopic.setOwner("BusController");
289 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
290 mmaTopic.setTnxEnabled("false");
291 mmaTopic.setPartitionCount("1"); // a single partition should guarantee message order
294 ApiError err = new ApiError();
295 TopicService svc = new TopicService();
297 @SuppressWarnings("unused")
298 Topic nTopic = svc.addTopic(mmaTopic, err, true);
299 if ( err.is2xx() || err.getCode() == 409 ) {
302 } catch ( Exception e) {
303 errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
305 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());