2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
23 import java.util.ArrayList;
34 import org.onap.dmaap.dbcapi.aaf.AafService;
35 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
36 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
37 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
38 import org.onap.dmaap.dbcapi.authentication.ApiPerms;
39 import org.onap.dmaap.dbcapi.authentication.ApiPolicy;
40 import org.onap.dmaap.dbcapi.database.DatabaseClass;
41 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
42 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
43 import org.onap.dmaap.dbcapi.model.ApiError;
44 import org.onap.dmaap.dbcapi.model.Dmaap;
45 import org.onap.dmaap.dbcapi.model.MR_Client;
46 import org.onap.dmaap.dbcapi.model.Topic;
47 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
48 import org.onap.dmaap.dbcapi.util.DmaapConfig;
49 import org.onap.dmaap.dbcapi.util.Singleton;
51 public class DmaapService extends BaseLoggingClass {
54 private Singleton<Dmaap> dmaapholder = DatabaseClass.getDmaap();
55 private static String noEnvironmentPrefix;
58 String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
59 String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
60 String dcaeTopicNs; // = "org.openecomp.dcae.dmaap";
61 private boolean multiSite;
64 public DmaapService() {
65 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
66 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
67 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
68 dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
69 multiSite = "true".equalsIgnoreCase(p.getProperty("MR.multisite", "true"));
70 noEnvironmentPrefix = p.getProperty( "AAF.NoEnvironmentPrefix", "org.onap");
72 logger.info( "DmaapService settings: " +
73 " topicFactory=" + topicFactory +
74 " topicMgrRole=" + topicMgrRole +
75 " dcaeTopicNs=" + dcaeTopicNs +
76 " multisite=" + multiSite +
77 " noEnvironmentPrefix=" + noEnvironmentPrefix
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 AafService( ServiceType.AAF_Admin);
97 ApiPolicy apiPolicy = new ApiPolicy();
98 if ( apiPolicy.getUseAuthClass() ) {
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 ApiPolicy apiPolicy = new ApiPolicy();
140 if ( apiPolicy.getUseAuthClass()) {
141 ApiPerms p = new ApiPerms();
144 AafService aaf = new AafService( ServiceType.AAF_Admin);
146 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
150 if ( anythingWrong ) {
151 nd.setStatus(DmaapObject_Status.INVALID);
154 nd.setStatus(DmaapObject_Status.VALID);
157 dmaapholder.update(nd); // may need to update status...
158 return(dmaapholder.get());
162 public String getTopicPerm(){
163 Dmaap dmaap = dmaapholder.get();
164 return getTopicPerm( dmaap.getDmaapName() );
166 public String getTopicPerm( String val ) {
167 Dmaap dmaap = dmaapholder.get();
168 String nsRoot = dmaap.getTopicNsRoot();
170 // in ONAP Casablanca, we assume no distinction of environments reflected in topic namespace
171 if ( nsRoot.startsWith(noEnvironmentPrefix) ) {
172 t = nsRoot + ".mr.topic";
174 t = nsRoot + "." + val + ".mr.topic";
179 public String getBridgeAdminFqtn(){
180 Dmaap dmaap = dmaapholder.get();
181 String topic = dmaap.getBridgeAdminTopic();
183 // check if this is already an fqtn (contains a dot)
184 // otherwise build it
185 if ( topic.indexOf('.') < 0 ) {
186 topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
191 private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
192 String[] actions = { "create", "destroy" };
193 String instance = ":" + dcaeTopicNs + "." + nd.getDmaapName() + ".mr.topic:" + dcaeTopicNs + "." + nd.getDmaapName();
195 for( String action : actions ) {
197 DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
199 int rc = aaf.addPerm( perm );
200 if ( rc != 201 && rc != 409 ) {
201 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
205 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
206 rc = aaf.addGrant( grant );
207 if ( rc != 201 && rc != 409 ) {
208 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
213 String t = dcaeTopicNs +"." + nd.getDmaapName() + ".mr.topic";
214 String[] s = { "view", "pub", "sub" };
218 for( String action : actions ) {
220 DmaapPerm perm = new DmaapPerm( t, instance, action );
222 int rc = aaf.addPerm( perm );
223 if ( rc != 201 && rc != 409 ) {
224 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
228 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
229 rc = aaf.addGrant( grant );
230 if ( rc != 201 && rc != 409 ) {
231 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
239 public boolean testCreateMmaTopic() {
241 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
242 String unit_test = p.getProperty( "UnitTest", "No" );
243 if ( unit_test.equals( "Yes" ) ) {
244 return createMmaTopic();
249 // create the special topic for MMA provisioning.
250 // return true indicating a problem in topic creation,
251 // else false means it was ok (created or previously existed)
252 private boolean createMmaTopic() {
254 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
255 Dmaap dmaap = dmaapholder.get();
257 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
258 String[] actions = { "pub", "sub", "view" };
259 String centralMR = new DcaeLocationService().getCentralLocation();
260 if ( centralMR == null ) {
263 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
265 // first client is the Role used by Bus Controller to send messages to MMA
266 String provRole = p.getProperty("MM.ProvRole");
267 MR_Client nClient = new MR_Client();
268 nClient.setAction(actions);
269 nClient.setClientRole(provRole);
270 nClient.setDcaeLocationName(centralMR);
271 clients.add( nClient );
273 // second client is the Role used by MMA to listen to messages from Bus Controller
274 String agentRole = p.getProperty("MM.AgentRole");
275 nClient = new MR_Client();
276 nClient.setAction(actions);
277 nClient.setClientRole(agentRole);
278 nClient.setDcaeLocationName(centralMR);
279 clients.add( nClient );
282 Topic mmaTopic = new Topic();
283 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
284 mmaTopic.setClients(clients);
285 mmaTopic.setOwner("BusController");
286 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
287 mmaTopic.setTnxEnabled("false");
289 ApiError err = new ApiError();
290 TopicService svc = new TopicService();
292 @SuppressWarnings("unused")
293 Topic nTopic = svc.addTopic(mmaTopic, err, true);
294 if ( err.is2xx() || err.getCode() == 409 ) {
297 } catch ( Exception e) {
298 errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
300 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());