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.aaf.authentication.ApiPerms;
39 import org.onap.dmaap.dbcapi.aaf.database.DatabaseClass;
40 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
41 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
42 import org.onap.dmaap.dbcapi.model.ApiError;
43 import org.onap.dmaap.dbcapi.model.Dmaap;
44 import org.onap.dmaap.dbcapi.model.MR_Client;
45 import org.onap.dmaap.dbcapi.model.Topic;
46 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
47 import org.onap.dmaap.dbcapi.util.DmaapConfig;
48 import org.onap.dmaap.dbcapi.util.Singleton;
50 public class DmaapService extends BaseLoggingClass {
53 private Singleton<Dmaap> dmaapholder = DatabaseClass.getDmaap();
55 // TODO put these in properties file
56 String topicFactory; // = "org.openecomp.dcae.dmaap.topicFactory";
57 String topicMgrRole; // = "org.openecomp.dmaapBC.TopicMgr";
59 // TODO confirm this is equivalent to dmaap.getTopicNsRoot() so we can retire it
60 String dcaeTopicNs; // = "org.openecomp.dcae.dmaap";
63 public DmaapService() {
64 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
65 topicFactory = p.getProperty("MR.TopicFactoryNS", "MR.topicFactoryNS.not.set");
66 topicMgrRole = p.getProperty("MR.TopicMgrRole", "MR.TopicMgrRole.not.set" );
67 dcaeTopicNs = dmaapholder.get().getTopicNsRoot();
71 public Dmaap getDmaap() {
72 logger.info( "entering getDmaap()" );
73 return(dmaapholder.get());
76 public Dmaap addDmaap( Dmaap nd ) {
78 logger.info( "entering addDmaap()" );
79 Dmaap dmaap = dmaapholder.get();
80 if ( dmaap.getVersion().equals( "0")) {
83 dmaapholder.update(nd);
85 AafService aaf = new AafService( ServiceType.AAF_Admin);
86 ApiPerms p = new ApiPerms();
88 boolean anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
90 if ( anythingWrong ) {
91 dmaap.setStatus(DmaapObject_Status.INVALID);
94 dmaap.setStatus(DmaapObject_Status.VALID);
97 dmaapholder.update(dmaap);
107 public Dmaap updateDmaap( Dmaap nd ) {
108 logger.info( "entering updateDmaap()" );
110 boolean anythingWrong = false;
112 Dmaap dmaap = dmaapholder.get();
114 // some triggers for when we attempt to reprovision perms and MMA topic:
115 // - if the DMaaP Name changes
116 // - if the version is 0 (this is a handy test to force this processing by updating the DB)
117 // - if the object is invalid, reprocessing might fix it.
118 if ( ! dmaap.isStatusValid() || ! nd.getDmaapName().equals(dmaap.getDmaapName()) || dmaap.getVersion().equals( "0") ) {
120 dmaapholder.update(nd); //need to set this so the following perms will pick up any new vals.
121 ApiPerms p = new ApiPerms();
123 AafService aaf = new AafService( ServiceType.AAF_Admin);
124 anythingWrong = setTopicMgtPerms( nd, aaf ) || createMmaTopic();
127 if ( anythingWrong ) {
128 nd.setStatus(DmaapObject_Status.INVALID);
131 nd.setStatus(DmaapObject_Status.VALID);
134 dmaapholder.update(nd); // may need to update status...
135 return(dmaapholder.get());
139 public String getTopicPerm(){
140 Dmaap dmaap = dmaapholder.get();
141 return getTopicPerm( dmaap.getDmaapName() );
143 public String getTopicPerm( String val ) {
144 Dmaap dmaap = dmaapholder.get();
145 return dmaap.getTopicNsRoot() + "." + val + ".mr.topic";
148 public String getBridgeAdminFqtn(){
149 Dmaap dmaap = dmaapholder.get();
150 String topic = dmaap.getBridgeAdminTopic();
152 // check if this is already an fqtn (contains a dot)
153 // otherwise build it
154 if ( topic.indexOf('.') < 0 ) {
155 topic = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + dmaap.getBridgeAdminTopic();
160 private boolean setTopicMgtPerms( Dmaap nd, AafService aaf ){
161 String[] actions = { "create", "destroy" };
162 String instance = ":" + dcaeTopicNs + "." + nd.getDmaapName() + ".mr.topic:" + dcaeTopicNs + "." + nd.getDmaapName();
164 for( String action : actions ) {
166 DmaapPerm perm = new DmaapPerm( topicFactory, instance, action );
168 int rc = aaf.addPerm( perm );
169 if ( rc != 201 && rc != 409 ) {
170 logger.error( "unable to add perm for "+ topicFactory + "|" + instance + "|" + action );
174 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
175 rc = aaf.addGrant( grant );
176 if ( rc != 201 && rc != 409 ) {
177 logger.error( "unable to grant to " + topicMgrRole + " perm for "+ topicFactory + "|" + instance + "|" + action );
182 String t = dcaeTopicNs +"." + nd.getDmaapName() + ".mr.topic";
183 String[] s = { "view", "pub", "sub" };
187 for( String action : actions ) {
189 DmaapPerm perm = new DmaapPerm( t, instance, action );
191 int rc = aaf.addPerm( perm );
192 if ( rc != 201 && rc != 409 ) {
193 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "add perm", t + "|" + instance + "|" + action );
197 DmaapGrant grant = new DmaapGrant( perm, topicMgrRole );
198 rc = aaf.addGrant( grant );
199 if ( rc != 201 && rc != 409 ) {
200 errorLogger.error( DmaapbcLogMessageEnum.AAF_UNEXPECTED_RESPONSE, Integer.toString(rc), "grant to " + topicMgrRole + " perm ", topicFactory + "|" + instance + "|" + action );
208 // create the special topic for MMA provisioning.
209 // return true indicating a problem in topic creation,
210 // else false means it was ok (created or previously existed)
211 private boolean createMmaTopic() {
213 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
214 Dmaap dmaap = dmaapholder.get();
216 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
217 String[] actions = { "pub", "sub", "view" };
218 String centralMR = new DcaeLocationService().getCentralLocation();
219 if ( centralMR == null ) {
222 logger.info( "Location for " + dmaap.getBridgeAdminTopic() + " is " + centralMR );
224 // first client is the Role used by Bus Controller to send messages to MMA
225 String provRole = p.getProperty("MM.ProvRole");
226 MR_Client nClient = new MR_Client();
227 nClient.setAction(actions);
228 nClient.setClientRole(provRole);
229 nClient.setDcaeLocationName(centralMR);
230 clients.add( nClient );
232 // second client is the Role used by MMA to listen to messages from Bus Controller
233 String agentRole = p.getProperty("MM.AgentRole");
234 nClient = new MR_Client();
235 nClient.setAction(actions);
236 nClient.setClientRole(agentRole);
237 nClient.setDcaeLocationName(centralMR);
238 clients.add( nClient );
241 Topic mmaTopic = new Topic();
242 mmaTopic.setTopicName(dmaap.getBridgeAdminTopic());
243 mmaTopic.setClients(clients);
244 mmaTopic.setOwner("BusController");
245 mmaTopic.setTopicDescription("topic reserved for MirrorMaker Administration");
246 mmaTopic.setTnxEnabled("false");
248 ApiError err = new ApiError();
249 TopicService svc = new TopicService();
251 @SuppressWarnings("unused")
252 Topic nTopic = svc.addTopic(mmaTopic, err);
253 if ( err.is2xx() || err.getCode() == 409 ) {
256 } catch ( Exception e) {
257 errorLogger.error( DmaapbcLogMessageEnum.UNEXPECTED_CONDITION, " while adding Topic: " + e.getMessage());
259 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, dmaap.getBridgeAdminTopic(), err.getFields(), err.getFields(), err.getMessage());