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 org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
26 import org.onap.dmaap.dbcapi.aaf.AafServiceFactory;
27 import org.onap.dmaap.dbcapi.database.DatabaseClass;
28 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
29 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
30 import org.onap.dmaap.dbcapi.model.ApiError;
31 import org.onap.dmaap.dbcapi.model.DcaeLocation;
32 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
33 import org.onap.dmaap.dbcapi.model.MR_Client;
34 import org.onap.dmaap.dbcapi.model.MR_Cluster;
35 import org.onap.dmaap.dbcapi.model.MirrorMaker;
36 import org.onap.dmaap.dbcapi.model.ReplicationType;
37 import org.onap.dmaap.dbcapi.model.Topic;
38 import org.onap.dmaap.dbcapi.util.DmaapConfig;
39 import org.onap.dmaap.dbcapi.util.Fqdn;
40 import org.onap.dmaap.dbcapi.util.Graph;
42 import javax.ws.rs.core.Response.Status;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.Iterator;
46 import java.util.List;
50 public class TopicService extends BaseLoggingClass {
53 // REF: https://wiki.web.att.com/pages/viewpage.action?pageId=519703122
54 private static String defaultGlobalMrHost;
56 private Map<String, Topic> mr_topics;
58 private static DmaapService dmaapSvc = new DmaapService();
59 private MR_ClientService clientService;
60 private MR_ClusterService clusters;
61 private DcaeLocationService locations;
62 private MirrorMakerService bridge;
63 private AafTopicSetupService aafTopicSetupService;
65 private static String centralCname;
66 private boolean strictGraph = true;
67 private boolean mmPerMR;
70 public TopicService() {
71 this(DatabaseClass.getTopics(), new MR_ClientService(), (DmaapConfig) DmaapConfig.getConfig(),
72 new MR_ClusterService(), new DcaeLocationService(), new MirrorMakerService(),
73 new AafTopicSetupService(
74 new AafServiceFactory().initAafService(ServiceType.AAF_TopicMgr),
75 dmaapSvc, (DmaapConfig) DmaapConfig.getConfig()));
78 TopicService(Map<String, Topic> mr_topics, MR_ClientService clientService, DmaapConfig p,
79 MR_ClusterService clusters, DcaeLocationService locations, MirrorMakerService bridge, AafTopicSetupService aafTopicSetupService) {
80 this.mr_topics = mr_topics;
81 this.clientService = clientService;
82 defaultGlobalMrHost = p.getProperty("MR.globalHost", "global.host.not.set");
83 centralCname = p.getProperty("MR.CentralCname");
84 String unit_test = p.getProperty("UnitTest", "No");
85 if ("Yes".equals(unit_test)) {
88 mmPerMR = "true".equalsIgnoreCase(p.getProperty("MirrorMakerPerMR", "true"));
89 logger.info("TopicService properties: CentralCname=" + centralCname +
90 " defaultGlobarlMrHost=" + defaultGlobalMrHost +
91 " mmPerMR=" + mmPerMR);
92 this.clusters = clusters;
93 this.locations = locations;
95 this.aafTopicSetupService = aafTopicSetupService;
98 public Map<String, Topic> getTopics() {
102 public List<Topic> getAllTopics() {
103 return getAllTopics(true);
106 public List<Topic> getAllTopicsWithoutClients() {
107 return getAllTopics(false);
110 private List<Topic> getAllTopics(Boolean withClients) {
111 ArrayList<Topic> topics = new ArrayList<>(mr_topics.values());
113 for (Topic topic : topics) {
114 topic.setClients(clientService.getAllMrClients(topic.getFqtn()));
120 public Topic getTopic(String key, ApiError apiError) {
121 logger.info("getTopic: key=" + key);
122 Topic t = mr_topics.get(key);
124 apiError.setCode(Status.NOT_FOUND.getStatusCode());
125 apiError.setFields("fqtn");
126 apiError.setMessage("topic with fqtn " + key + " not found");
129 t.setClients(clientService.getAllMrClients(key));
130 apiError.setCode(Status.OK.getStatusCode());
134 public Topic addTopic(Topic topic, ApiError err, Boolean useExisting) {
135 logger.info("Entry: addTopic");
136 logger.info("Topic name=" + topic.getTopicName() + " fqtnStyle=" + topic.getFqtnStyle());
137 String nFqtn = topic.genFqtn();
138 logger.info("FQTN=" + nFqtn);
139 Topic pTopic = getTopic(nFqtn, err);
140 if (pTopic != null) {
141 String t = "topic already exists: " + nFqtn;
144 err.setCode(Status.OK.getStatusCode());
148 err.setFields("fqtn");
149 err.setCode(Status.CONFLICT.getStatusCode());
152 err.reset(); // err filled with NOT_FOUND is expected case, but don't want to litter...
154 topic.setFqtn(nFqtn);
156 ApiError topicSetupError = aafTopicSetupService.aafTopicSetup(topic);
157 updateApiError(err, topicSetupError);
158 if (err.getCode() >= 400) {
162 if (topic.getReplicationCase().involvesGlobal()) {
163 if (topic.getGlobalMrURL() == null) {
164 topic.setGlobalMrURL(defaultGlobalMrHost);
166 if (!Fqdn.isValid(topic.getGlobalMrURL())) {
167 logger.error("GlobalMR FQDN not valid: " + topic.getGlobalMrURL());
168 topic.setStatus(DmaapObject_Status.INVALID);
170 err.setMessage("Value is not a valid FQDN:" + topic.getGlobalMrURL());
171 err.setFields("globalMrURL");
178 if (topic.getNumClients() > 0) {
179 ArrayList<MR_Client> clients = new ArrayList<MR_Client>(topic.getClients());
182 ArrayList<MR_Client> clients2 = new ArrayList<MR_Client>();
183 for (Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
184 MR_Client c = it.next();
186 logger.info("c fqtn=" + c.getFqtn() + " ID=" + c.getMrClientId() + " url=" + c.getTopicURL());
187 MR_Client nc = new MR_Client(c.getDcaeLocationName(), topic.getFqtn(), c.getClientRole(), c.getAction());
188 nc.setFqtn(topic.getFqtn());
189 nc.setClientIdentity(c.getClientIdentity());
190 logger.info("nc fqtn=" + nc.getFqtn() + " ID=" + nc.getMrClientId() + " url=" + nc.getTopicURL());
191 clients2.add(clientService.addMr_Client(nc, topic, err));
197 topic.setClients(clients2);
200 Topic ntopic = checkForBridge(topic, err);
201 if (ntopic == null) {
202 topic.setStatus(DmaapObject_Status.INVALID);
209 mr_topics.put(nFqtn, ntopic);
211 err.setCode(Status.OK.getStatusCode());
216 public Topic updateTopic(Topic topic, ApiError err) {
217 logger.info("updateTopic: entry");
218 logger.info("updateTopic: topic=" + topic);
219 logger.info("updateTopic: fqtn=" + topic.getFqtn());
220 if (topic.getFqtn().isEmpty()) {
223 logger.info("updateTopic: call checkForBridge");
224 Topic ntopic = checkForBridge(topic, err);
225 if (ntopic == null) {
226 topic.setStatus(DmaapObject_Status.INVALID);
231 if (ntopic != null) {
232 logger.info("updateTopic: call put");
233 mr_topics.put(ntopic.getFqtn(), ntopic);
235 err.setCode(Status.OK.getStatusCode());
239 public Topic removeTopic(String pubId, ApiError apiError) {
240 Topic topic = mr_topics.get(pubId);
242 apiError.setCode(Status.NOT_FOUND.getStatusCode());
243 apiError.setMessage("Topic " + pubId + " does not exist");
244 apiError.setFields("fqtn");
248 ApiError topicSetupError = aafTopicSetupService.aafTopicCleanup(topic);
249 updateApiError(apiError, topicSetupError);
250 if (apiError.getCode() >= 400) {
254 ArrayList<MR_Client> clients = new ArrayList<MR_Client>(clientService.getAllMrClients(pubId));
255 for (Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
256 MR_Client c = it.next();
257 clientService.removeMr_Client(c.getMrClientId(), false, apiError);
258 if (!apiError.is2xx()) {
262 apiError.setCode(Status.OK.getStatusCode());
263 return mr_topics.remove(pubId);
266 public static ApiError setBridgeClientPerms(MR_Cluster node) {
267 DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
268 String mmProvRole = p.getProperty("MM.ProvRole");
269 String mmAgentRole = p.getProperty("MM.AgentRole");
270 String[] Roles = {mmProvRole, mmAgentRole};
271 String[] actions = {"view", "pub", "sub"};
272 Topic bridgeAdminTopic = new Topic().init();
273 bridgeAdminTopic.setTopicName(dmaapSvc.getBridgeAdminFqtn());
274 bridgeAdminTopic.setTopicDescription("RESERVED topic for MirroMaker Provisioning");
275 bridgeAdminTopic.setOwner("DBC");
277 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
278 for (String role : Roles) {
279 MR_Client client = new MR_Client();
280 client.setAction(actions);
281 client.setClientRole(role);
282 client.setDcaeLocationName(node.getDcaeLocationName());
285 bridgeAdminTopic.setClients(clients);
287 TopicService ts = new TopicService();
288 ApiError err = new ApiError();
289 ts.addTopic(bridgeAdminTopic, err, true);
291 if (err.is2xx() || err.getCode() == 409) {
296 errorLogger.error(DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, bridgeAdminTopic.getFqtn(), Integer.toString(err.getCode()), err.getFields(), err.getMessage());
301 public Topic checkForBridge(Topic topic, ApiError err) {
302 logger.info("checkForBridge: entry");
303 logger.info("fqtn=" + topic.getFqtn() + " replicatonType=" + topic.getReplicationCase());
304 if (topic.getReplicationCase() == ReplicationType.REPLICATION_NONE) {
305 topic.setStatus(DmaapObject_Status.VALID);
309 boolean anythingWrong = false;
311 Set<String> groups = clusters.getGroups();
312 for (String g : groups) {
313 logger.info("buildBridge for " + topic.getFqtn() + " on group" + g);
314 anythingWrong |= buildBridge(topic, err, g);
317 topic.setStatus(DmaapObject_Status.INVALID);
322 topic.setStatus(DmaapObject_Status.VALID);
327 private boolean buildBridge(Topic topic, ApiError err, String group) {
328 logger.info("buildBridge: entry");
329 boolean anythingWrong = false;
331 logger.info("buildBridge: strictGraph=" + strictGraph);
332 if (group == null || group.isEmpty()) {
333 graph = new Graph(topic.getClients(), strictGraph);
335 graph = new Graph(topic.getClients(), strictGraph, group);
337 logger.info("buildBridge: graph=" + graph);
338 MR_Cluster groupCentralCluster = null;
341 if (graph.isEmpty()) {
342 logger.info("buildBridge: graph is empty. return false");
344 } else if (group == null && topic.getReplicationCase().involvesFQDN()) {
345 logger.info("buildBridge: group is null and replicationCaseInvolvesFQDN. return false");
347 } else if (!graph.hasCentral()) {
348 logger.warn("Topic " + topic.getFqtn() + " wants to be " + topic.getReplicationCase() + " but has no central clients");
351 groupCentralCluster = clusters.getMr_ClusterByLoc(graph.getCentralLoc());
353 Collection<String> clientLocations = graph.getKeys();
354 for (String loc : clientLocations) {
355 logger.info("loc=" + loc);
356 DcaeLocation location = locations.getDcaeLocation(loc);
357 MR_Cluster cluster = clusters.getMr_ClusterByLoc(loc);
358 logger.info("cluster=" + cluster + " at " + cluster.getDcaeLocationName());
359 logger.info("location.isCentral()=" + location.isCentral() + " getCentralLoc()=" + graph.getCentralLoc());
362 String source = null;
363 String target = null;
366 * Provision Edge to Central bridges...
368 if (!location.isCentral() && !graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
369 switch (topic.getReplicationCase()) {
370 case REPLICATION_EDGE_TO_CENTRAL:
371 case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL: // NOTE: this is for E2C portion only
372 source = cluster.getFqdn();
373 target = (mmPerMR) ? groupCentralCluster.getFqdn() : centralCname;
374 logger.info("REPLICATION_EDGE_TO_CENTRAL: source=" + source + " target=" + target);
376 case REPLICATION_CENTRAL_TO_EDGE:
377 case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE: // NOTE: this is for C2E portion only
378 source = (mmPerMR) ? groupCentralCluster.getFqdn() : centralCname;
379 target = cluster.getFqdn();
381 case REPLICATION_CENTRAL_TO_GLOBAL:
382 case REPLICATION_GLOBAL_TO_CENTRAL:
383 case REPLICATION_FQDN_TO_GLOBAL:
384 case REPLICATION_GLOBAL_TO_FQDN:
387 case REPLICATION_EDGE_TO_FQDN:
388 case REPLICATION_EDGE_TO_FQDN_TO_GLOBAL: // NOTE: this is for E2C portion only
389 source = cluster.getFqdn();
390 target = groupCentralCluster.getFqdn();
392 case REPLICATION_FQDN_TO_EDGE:
393 case REPLICATION_GLOBAL_TO_FQDN_TO_EDGE: // NOTE: this is for F2E portion only
394 source = groupCentralCluster.getFqdn();
395 target = cluster.getFqdn();
399 logger.error("Unexpected value for ReplicationType (" + topic.getReplicationCase() + ") for topic " + topic.getFqtn());
400 anythingWrong = true;
402 err.setFields("topic=" + topic.genFqtn() + " replicationCase="
403 + topic.getReplicationCase());
404 err.setMessage("Unexpected value for ReplicationType");
408 } else if (location.isCentral() && graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
410 * Provision Central to Global bridges
412 switch (topic.getReplicationCase()) {
414 case REPLICATION_CENTRAL_TO_GLOBAL:
415 case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL:
416 source = centralCname;
417 target = topic.getGlobalMrURL();
419 case REPLICATION_GLOBAL_TO_CENTRAL:
420 case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE: // NOTE: this is for G2C portion only
421 source = topic.getGlobalMrURL();
422 target = centralCname;
425 case REPLICATION_EDGE_TO_FQDN_TO_GLOBAL: // NOTE: this is for E2F portion only
426 source = groupCentralCluster.getFqdn();
427 target = topic.getGlobalMrURL();
430 case REPLICATION_FQDN_TO_GLOBAL:
431 source = groupCentralCluster.getFqdn();
432 target = topic.getGlobalMrURL();
435 case REPLICATION_GLOBAL_TO_FQDN:
436 case REPLICATION_GLOBAL_TO_FQDN_TO_EDGE: // NOTE: this is for G2F portion only
437 source = topic.getGlobalMrURL();
438 target = groupCentralCluster.getFqdn();
441 case REPLICATION_FQDN_TO_EDGE:
442 case REPLICATION_EDGE_TO_FQDN:
443 case REPLICATION_EDGE_TO_CENTRAL:
444 case REPLICATION_CENTRAL_TO_EDGE:
447 logger.error("Unexpected value for ReplicationType (" + topic.getReplicationCase() + ") for topic " + topic.getFqtn());
448 anythingWrong = true;
450 err.setFields("topic=" + topic.genFqtn() + " replicationCase="
451 + topic.getReplicationCase());
452 err.setMessage("Unexpected value for ReplicationType");
456 logger.warn("dcaeLocation " + loc + " is neither Edge nor Central so no mmagent provisioning was done");
457 anythingWrong = true;
460 if (source != null && target != null) {
462 logger.info("Create a MM from " + source + " to " + target);
463 MirrorMaker mm = bridge.findNextMM(source, target, topic.getFqtn());
464 mm.addTopic(topic.getFqtn());
465 bridge.updateMirrorMaker(mm);
466 } catch (Exception ex) {
468 err.setFields("mirror_maker.topic");
469 err.setMessage("Unexpected condition: " + ex);
470 anythingWrong = true;
477 return anythingWrong;
483 * Prior to 1707, we only supported EDGE_TO_CENTRAL replication.
484 * This was determined automatically based on presence of edge publishers and central subscribers.
485 * The following method is a modification of that original logic, to preserve some backwards compatibility,
486 * i.e. to be used when no ReplicationType is specified.
489 public ReplicationType reviewTopic(Topic topic) {
492 if (topic.getNumClients() > 1) {
493 Graph graph = new Graph(topic.getClients(), false);
495 String centralFqdn = new String();
496 if (graph.hasCentral()) {
497 DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
498 centralFqdn = p.getProperty("MR.CentralCname");
501 Collection<String> locations = graph.getKeys();
502 for (String loc : locations) {
503 logger.info("loc=" + loc);
504 MR_Cluster cluster = clusters.getMr_ClusterByLoc(loc);
505 if (cluster == null) {
506 logger.info("No MR cluster for location " + loc);
509 if (graph.hasCentral() && !graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
510 logger.info("Detected case for EDGE_TO_CENTRAL from " + cluster.getFqdn() + " to " + centralFqdn);
511 return ReplicationType.REPLICATION_EDGE_TO_CENTRAL;
518 return ReplicationType.REPLICATION_NONE;
521 private void updateApiError(ApiError err, ApiError topicSetupError) {
522 err.setCode(topicSetupError.getCode());
523 err.setMessage(topicSetupError.getMessage());
524 err.setFields(topicSetupError.getFields());