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.DmaapGrant;
28 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
29 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
30 import org.onap.dmaap.dbcapi.authentication.ApiPerms;
31 import org.onap.dmaap.dbcapi.authentication.ApiPolicy;
32 import org.onap.dmaap.dbcapi.database.DatabaseClass;
33 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
35 import org.onap.dmaap.dbcapi.model.ApiError;
36 import org.onap.dmaap.dbcapi.model.Dmaap;
37 import org.onap.dmaap.dbcapi.model.MR_Client;
38 import org.onap.dmaap.dbcapi.model.Topic;
39 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
40 import org.onap.dmaap.dbcapi.util.DmaapConfig;
41 import org.onap.dmaap.dbcapi.util.Singleton;
43 public class DmaapService extends BaseLoggingClass {
46 private Singleton<Dmaap> dmaapholder = DatabaseClass.getDmaap();
47 private static String noEnvironmentPrefix;
50 String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
51 String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
53 private boolean multiSite;
56 public DmaapService() {
57 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
58 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
59 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
61 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
62 noEnvironmentPrefix = p.getProperty( "AAF.NoEnvironmentPrefix", "org.onap");
64 logger.info( "DmaapService settings: " +
65 " topicFactory=" + topicFactory +
66 " topicMgrRole=" + topicMgrRole +
68 " multisite=" + multiSite +
69 " noEnvironmentPrefix=" + noEnvironmentPrefix
74 public Dmaap getDmaap() {
75 logger.info( "entering getDmaap()" );
76 return(dmaapholder.get());
79 public Dmaap addDmaap( Dmaap nd ) {
81 logger.info( "entering addDmaap()" );
82 Dmaap dmaap = dmaapholder.get();
83 if ( dmaap.getVersion().equals( "0")) {
86 dmaapholder.update(nd);
88 AafService aaf = new AafService( ServiceType.AAF_Admin);
89 ApiPolicy apiPolicy = new ApiPolicy();
90 if ( apiPolicy.getUseAuthClass() ) {
91 ApiPerms p = new ApiPerms();
94 boolean anythingWrong = false;
97 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
100 if ( anythingWrong ) {
101 dmaap.setStatus(DmaapObject_Status.INVALID);
104 dmaap.setStatus(DmaapObject_Status.VALID);
107 dmaapholder.update(dmaap);
117 public Dmaap updateDmaap( Dmaap nd ) {
118 logger.info( "entering updateDmaap()" );
120 boolean anythingWrong = false;
122 Dmaap dmaap = dmaapholder.get();
124 // some triggers for when we attempt to reprovision perms and MMA topic:
125 // - if the DMaaP Name changes
126 // - if the version is 0 (this is a handy test to force this processing by updating the DB)
127 // - if the object is invalid, reprocessing might fix it.
128 if ( ! dmaap.isStatusValid() || ! nd.getDmaapName().equals(dmaap.getDmaapName()) || dmaap.getVersion().equals( "0") ) {
130 dmaapholder.update(nd); //need to set this so the following perms will pick up any new vals.
131 //dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
132 ApiPolicy apiPolicy = new ApiPolicy();
133 if ( apiPolicy.getUseAuthClass()) {
134 ApiPerms p = new ApiPerms();
137 AafService aaf = new AafService( ServiceType.AAF_Admin);
139 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
143 if ( anythingWrong ) {
144 nd.setStatus(DmaapObject_Status.INVALID);
147 nd.setStatus(DmaapObject_Status.VALID);
150 dmaapholder.update(nd); // may need to update status...
151 return(dmaapholder.get());
155 public String getTopicPerm(){
156 Dmaap dmaap = dmaapholder.get();
157 return getTopicPerm( dmaap.getDmaapName() );
159 public String getTopicPerm( String val ) {
160 Dmaap dmaap = dmaapholder.get();
161 String nsRoot = dmaap.getTopicNsRoot();
162 if ( nsRoot == null ) { return null; }
165 // in ONAP Casablanca, we assume no distinction of environments reflected in topic namespace
166 if ( nsRoot.startsWith(noEnvironmentPrefix) ) {
167 t = nsRoot + ".mr.topic";
169 t = nsRoot + "." + val + ".mr.topic";
174 public String getBridgeAdminFqtn(){
175 Dmaap dmaap = dmaapholder.get();
176 String topic = dmaap.getBridgeAdminTopic();
178 // check if this is already an fqtn (contains a dot)
179 // otherwise build it
180 if ( topic.indexOf('.') < 0 ) {
181 topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
186 private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
187 String[] actions = { "create", "destroy" };
188 String instance = ":" + nd.getTopicNsRoot() + "." + nd.getDmaapName() + ".mr.topic:" + nd.getTopicNsRoot() + "." + nd.getDmaapName();
190 for( String action : actions ) {
192 DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
194 int rc = aaf.addPerm( perm );
195 if ( rc != 201 && rc != 409 ) {
196 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
200 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
201 rc = aaf.addGrant( grant );
202 if ( rc != 201 && rc != 409 ) {
203 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
208 String t = nd.getTopicNsRoot() +"." + nd.getDmaapName() + ".mr.topic";
209 String[] s = { "view", "pub", "sub" };
213 for( String action : actions ) {
215 DmaapPerm perm = new DmaapPerm( t, instance, action );
217 int rc = aaf.addPerm( perm );
218 if ( rc != 201 && rc != 409 ) {
219 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
223 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
224 rc = aaf.addGrant( grant );
225 if ( rc != 201 && rc != 409 ) {
226 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
234 public boolean testCreateMmaTopic() {
236 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
237 String unit_test = p.getProperty( "UnitTest", "No" );
238 if ( unit_test.equals( "Yes" ) ) {
239 return createMmaTopic();
244 // create the special topic for MMA provisioning.
245 // return true indicating a problem in topic creation,
246 // else false means it was ok (created or previously existed)
247 private boolean createMmaTopic() {
249 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
250 Dmaap dmaap = dmaapholder.get();
252 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
253 String[] actions = { "pub", "sub", "view" };
254 String centralMR = new DcaeLocationService().getCentralLocation();
255 if ( centralMR == null ) {
258 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
260 // first client is the Role used by Bus Controller to send messages to MMA
261 String provRole = p.getProperty("MM.ProvRole");
262 MR_Client nClient = new MR_Client();
263 nClient.setAction(actions);
264 nClient.setClientRole(provRole);
265 nClient.setDcaeLocationName(centralMR);
266 clients.add( nClient );
268 // second client is the Role used by MMA to listen to messages from Bus Controller
269 String agentRole = p.getProperty("MM.AgentRole");
270 nClient = new MR_Client();
271 nClient.setAction(actions);
272 nClient.setClientRole(agentRole);
273 nClient.setDcaeLocationName(centralMR);
274 clients.add( nClient );
277 Topic mmaTopic = new Topic().init();
278 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
279 mmaTopic.setClients(clients);
280 mmaTopic.setOwner("BusController");
281 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
282 mmaTopic.setTnxEnabled("false");
283 mmaTopic.setPartitionCount("1"); // a single partition should guarantee message order
286 ApiError err = new ApiError();
287 TopicService svc = new TopicService();
289 @SuppressWarnings("unused")
290 Topic nTopic = svc.addTopic(mmaTopic, err, true);
291 if ( err.is2xx() || err.getCode() == 409 ) {
294 } catch ( Exception e) {
295 errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
297 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());