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;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
29 import javax.ws.rs.core.Response.Status;
31 import org.onap.dmaap.dbcapi.aaf.AafService;
32 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
33 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
34 import org.onap.dmaap.dbcapi.database.DatabaseClass;
35 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
36 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
37 import org.onap.dmaap.dbcapi.model.ApiError;
38 import org.onap.dmaap.dbcapi.model.Dmaap;
39 import org.onap.dmaap.dbcapi.model.MR_Client;
40 import org.onap.dmaap.dbcapi.model.MR_Cluster;
41 import org.onap.dmaap.dbcapi.model.MirrorMaker;
42 import org.onap.dmaap.dbcapi.model.ReplicationType;
43 import org.onap.dmaap.dbcapi.model.Topic;
44 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
45 import org.onap.dmaap.dbcapi.util.DmaapConfig;
46 import org.onap.dmaap.dbcapi.util.Graph;
48 public class TopicService extends BaseLoggingClass {
52 // REF: https://wiki.web.att.com/pages/viewpage.action?pageId=519703122
53 private static String defaultGlobalMrHost;
55 private Map<String, Topic> mr_topics = DatabaseClass.getTopics();
56 private Map<String, MR_Cluster> clusters = DatabaseClass.getMr_clusters();
58 private static DmaapService dmaapSvc = new DmaapService();
59 private static Dmaap dmaap = new DmaapService().getDmaap();
60 private MR_ClientService clientService = new MR_ClientService();
61 private MirrorMakerService bridge = new MirrorMakerService();
63 public TopicService(){
64 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
65 defaultGlobalMrHost = p.getProperty("MR.globalHost", "global.host.not.set");
68 public Map<String, Topic> getTopics() {
72 public List<Topic> getAllTopics() {
73 ArrayList<Topic> topics = new ArrayList<Topic>(mr_topics.values());
74 for( Topic topic: topics ) {
75 topic.setClients( clientService.getAllMrClients(topic.getFqtn()));
81 public Topic getTopic( String key, ApiError apiError ) {
82 logger.info( "getTopic: key=" + key);
83 Topic t = mr_topics.get( key );
85 apiError.setCode(Status.NOT_FOUND.getStatusCode());
86 apiError.setFields( "fqtn");
87 apiError.setMessage("topic with fqtn " + key + " not found");
90 t.setClients( clientService.getAllMrClients( key ));
91 apiError.setCode(Status.OK.getStatusCode());
95 public Topic addTopic( Topic topic, ApiError err ) {
96 logger.info( "Entry: addTopic");
97 logger.info( "Topic name=" + topic.getTopicName() + " fqtnStyle=" + topic.getFqtnStyle() );
98 String nFqtn = topic.genFqtn();
99 logger.info( "FQTN=" + nFqtn );
100 if ( getTopic( nFqtn, err ) != null ) {
101 String t = "topic already exists: " + nFqtn;
104 err.setFields( "fqtn");
105 err.setCode(Status.CONFLICT.getStatusCode());
108 err.reset(); // err filled with NOT_FOUND is expected case, but don't want to litter...
110 topic.setFqtn( nFqtn );
112 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
113 String t = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + ".mr.topic";
114 String instance = ":topic." + topic.getFqtn();
116 String[] actions = { "pub", "sub", "view" };
117 for ( String action : actions ){
118 DmaapPerm perm = new DmaapPerm( t, instance, action );
119 int rc = aaf.addPerm( perm );
120 if ( rc != 201 && rc != 409 ) {
122 err.setMessage("Unexpected response from AAF:" + rc );
123 err.setFields("t="+t + " instance="+ instance + " action="+ action);
129 if ( topic.getNumClients() > 0 ) {
130 ArrayList<MR_Client> clients = new ArrayList<MR_Client>(topic.getClients());
133 ArrayList<MR_Client> clients2 = new ArrayList<MR_Client>();
134 for ( Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
135 MR_Client c = it.next();
137 logger.info( "c fqtn=" + c.getFqtn() + " ID=" + c.getMrClientId() + " url=" + c.getTopicURL());
138 MR_Client nc = new MR_Client( c.getDcaeLocationName(), topic.getFqtn(), c.getClientRole(), c.getAction());
139 nc.setFqtn(topic.getFqtn());
140 logger.info( "nc fqtn=" + nc.getFqtn() + " ID=" + nc.getMrClientId() + " url=" + nc.getTopicURL());
141 clients2.add( clientService.addMr_Client(nc, topic, err));
142 if ( ! err.is2xx()) {
147 topic.setClients(clients2);
149 if ( topic.getReplicationCase().involvesGlobal() ) {
150 if ( topic.getGlobalMrURL() == null ) {
151 topic.setGlobalMrURL(defaultGlobalMrHost);
154 Topic ntopic = checkForBridge( topic, err );
155 if ( ntopic == null ) {
156 topic.setStatus( DmaapObject_Status.INVALID);
160 mr_topics.put( nFqtn, ntopic );
162 err.setCode(Status.OK.getStatusCode());
167 public Topic updateTopic( Topic topic, ApiError err ) {
168 logger.info( "Entry: updateTopic");
169 if ( topic.getFqtn().isEmpty()) {
172 Topic ntopic = checkForBridge( topic, err );
173 if ( ntopic == null ) {
174 topic.setStatus( DmaapObject_Status.INVALID);
177 mr_topics.put( ntopic.getFqtn(), ntopic );
178 err.setCode(Status.OK.getStatusCode());
182 public Topic removeTopic( String pubId, ApiError apiError ) {
183 Topic topic = mr_topics.get(pubId);
184 if ( topic == null ) {
185 apiError.setCode(Status.NOT_FOUND.getStatusCode());
186 apiError.setMessage("Topic " + pubId + " does not exist");
187 apiError.setFields("fqtn");
190 ArrayList<MR_Client> clients = new ArrayList<MR_Client>(clientService.getAllMrClients( pubId ));
191 for ( Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
192 MR_Client c = it.next();
195 clientService.removeMr_Client(c.getMrClientId(), false, apiError);
196 if ( ! apiError.is2xx()) {
200 apiError.setCode(Status.OK.getStatusCode());
201 return mr_topics.remove(pubId);
203 public static ApiError setBridgeClientPerms( MR_Cluster node ) {
204 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
205 String mmProvRole = p.getProperty("MM.ProvRole");
206 String mmAgentRole = p.getProperty("MM.AgentRole");
207 String[] Roles = { mmProvRole, mmAgentRole };
208 String[] actions = { "view", "pub", "sub" };
209 Topic bridgeAdminTopic = new Topic();
210 bridgeAdminTopic.setTopicName( dmaapSvc.getBridgeAdminFqtn() );
211 bridgeAdminTopic.setTopicDescription( "RESERVED topic for MirroMaker Provisioning");
212 bridgeAdminTopic.setOwner( "DBC" );
213 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
214 for( String role: Roles ) {
215 MR_Client client = new MR_Client();
216 client.setAction(actions);
217 client.setClientRole(role);
218 client.setDcaeLocationName( node.getDcaeLocationName());
219 clients.add( client );
221 bridgeAdminTopic.setClients(clients);
223 TopicService ts = new TopicService();
224 ApiError err = new ApiError();
225 ts.addTopic(bridgeAdminTopic, err);
227 if ( err.is2xx() || err.getCode() == 409 ){
232 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, bridgeAdminTopic.getFqtn(), Integer.toString(err.getCode()), err.getFields(), err.getMessage());
237 public Topic checkForBridge( Topic topic, ApiError err ) {
239 if ( topic.getReplicationCase() == ReplicationType.REPLICATION_NONE ) {
240 topic.setStatus( DmaapObject_Status.VALID);
244 boolean anythingWrong = false;
245 String centralFqdn = new String();
246 Graph graph = new Graph( topic.getClients(), true );
248 if ( graph.isHasCentral() ) {
249 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
250 centralFqdn = p.getProperty("MR.CentralCname");
251 logger.info( "CentralCname=" + centralFqdn );
253 logger.warn( "Topic " + topic.getFqtn() + " wants to be " + topic.getReplicationCase() + " but has no cental clients");
255 Collection<String> locations = graph.getKeys();
256 for( String loc : locations ) {
257 logger.info( "loc=" + loc );
258 MR_Cluster cluster = clusters.get(loc);
259 logger.info( "cluster=" + cluster );
263 String source = null;
264 String target = null;
266 * all replication rules have 1 bridge...
268 switch( topic.getReplicationCase() ) {
269 case REPLICATION_EDGE_TO_CENTRAL:
270 case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL: // NOTE: this is for E2C portion only
271 if ( graph.isHasCentral() && graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
274 source = cluster.getFqdn();
275 target = centralFqdn;
277 case REPLICATION_CENTRAL_TO_EDGE:
278 if ( graph.isHasCentral() && graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
281 source = centralFqdn;
282 target = cluster.getFqdn();
284 case REPLICATION_CENTRAL_TO_GLOBAL:
285 if ( graph.isHasCentral() && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
288 source = centralFqdn;
289 target = topic.getGlobalMrURL();
291 case REPLICATION_GLOBAL_TO_CENTRAL:
292 case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE: // NOTE: this is for G2C portion only
293 if ( graph.isHasCentral() && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
296 source = topic.getGlobalMrURL();
297 target = centralFqdn;
300 logger.error( "Unexpected value for ReplicationType ("+ topic.getReplicationCase() + ") for topic " + topic.getFqtn() );
301 anythingWrong = true;
304 if ( source != null && target != null ) {
306 logger.info( "Create a MM from " + source + " to " + target );
307 MirrorMaker mm = bridge.getMirrorMaker( source, target);
309 mm = new MirrorMaker(source, target);
311 mm.addTopic(topic.getFqtn());
312 bridge.updateMirrorMaker(mm);
313 } catch ( Exception ex ) {
315 err.setFields( "mirror_maker.topic");
316 err.setMessage("Unexpected condition: " + ex );
317 anythingWrong = true;
324 * some replication rules have a 2nd bridge!
326 source = target = null;
327 switch( topic.getReplicationCase() ) {
328 case REPLICATION_EDGE_TO_CENTRAL:
329 case REPLICATION_CENTRAL_TO_EDGE:
330 case REPLICATION_CENTRAL_TO_GLOBAL:
331 case REPLICATION_GLOBAL_TO_CENTRAL:
333 case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL: // NOTE: this is for C2G portion only
334 if ( graph.isHasCentral() && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
337 source = centralFqdn;
338 target = topic.getGlobalMrURL();
341 case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE: // NOTE: this is for C2E portion only
342 if ( graph.isHasCentral() && graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
345 source = centralFqdn;
346 target = cluster.getFqdn();
349 logger.error( "Unexpected value for ReplicationType ("+ topic.getReplicationCase() + ") for topic " + topic.getFqtn() );
350 anythingWrong = true;
353 if ( source != null && target != null ) {
355 logger.info( "Create a MM from " + source + " to " + target );
356 MirrorMaker mm = bridge.getMirrorMaker( source, target);
358 mm = new MirrorMaker(source, target);
360 mm.addTopic(topic.getFqtn());
361 bridge.updateMirrorMaker(mm);
362 } catch ( Exception ex ) {
364 err.setFields( "mirror_maker.topic");
365 err.setMessage("Unexpected condition: " + ex );
366 anythingWrong = true;
372 if ( anythingWrong ) {
373 topic.setStatus( DmaapObject_Status.INVALID);
377 topic.setStatus( DmaapObject_Status.VALID);
382 * Prior to 1707, we only supported EDGE_TO_CENTRAL replication.
383 * This was determined automatically based on presence of edge publishers and central subscribers.
384 * The following method is a modification of that original logic, to preserve some backwards compatibility,
385 * i.e. to be used when no ReplicationType is specified.
387 public ReplicationType reviewTopic( Topic topic ) {
390 if ( topic.getNumClients() > 1 ) {
391 Graph graph = new Graph( topic.getClients(), false );
393 String centralFqdn = new String();
394 if ( graph.isHasCentral() ) {
395 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
396 centralFqdn = p.getProperty("MR.CentralCname");
399 Collection<String> locations = graph.getKeys();
400 for( String loc : locations ) {
401 logger.info( "loc=" + loc );
402 MR_Cluster cluster = clusters.get(loc);
403 if ( cluster == null ) {
404 logger.info( "No MR cluster for location " + loc );
407 if ( graph.isHasCentral() && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
408 logger.info( "Detected case for EDGE_TO_CENTRAL from " + cluster.getFqdn() + " to " + centralFqdn );
409 return ReplicationType.REPLICATION_EDGE_TO_CENTRAL;
416 return ReplicationType.REPLICATION_NONE;