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.model;
23 import java.util.ArrayList;
24 import java.util.HashSet;
27 import org.apache.log4j.Logger;
28 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
29 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
31 public class MirrorMaker extends DmaapObject {
32 static final Logger logger = Logger.getLogger(MirrorMaker.class);
34 private String sourceCluster;
35 private String targetCluster;
36 private String mmName;
37 private ArrayList<String> topics; //re-using this var name for backwards DB compatibility
39 private Set<ReplicationVector> vectors;
46 public MirrorMaker(String source, String target) {
47 sourceCluster = source;
48 targetCluster = target;
49 mmName = genKey(source, target);
50 vectors = new HashSet<ReplicationVector>();
51 topics = new ArrayList<String>();
55 public String getMmName() {
59 public void setMmName(String mmName) {
64 public void addVector( String fqtn, String source, String target ) {
65 logger.info( "addVector: fqtn=" + fqtn + " source=" + source + " target=" + target );
66 if ( ! sourceCluster.equals( source ) ){
67 errorLogger.error( DmaapbcLogMessageEnum.MM_CIRCULAR_REF, source, sourceCluster );
69 vectors.add(new ReplicationVector( fqtn, source, target ));
72 public void delVector( String fqtn, String source, String target ) {
73 vectors.remove(new ReplicationVector( fqtn, source, target));
78 public String toJSON() {
79 StringBuilder str = new StringBuilder( "{ \"source\": " + sourceCluster + ",\"topics\": [" );
81 for (ReplicationVector rv: vectors) {
82 if ( numTargets > 0 ) {
85 str.append( " \"target\": " + rv.getTargetCluster() + ", \"topic\": " + rv.getFqtn());
90 return str.toString();
94 // returns the JSON for MM message containing which Topics to replicate
102 "name":"Global1ToGlobal3",
103 "whitelist":"org.openecomp.dcae.topic1,org.openecomp.dcae.topic2"
107 public String updateWhiteList() {
108 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"updateWhiteList\": {" );
109 str.append( " \"name\": \"" + this.getMmName() + "\", \"whitelist\": \"" );
112 //for (ReplicationVector rv: vectors) {
113 for (String rv: topics) {
114 if ( numTargets > 0 ) {
117 //str.append( rv.getFqtn() );
121 str.append( "\" } }" );
123 return str.toString();
126 // returns the JSON for MM message indicating that a MM agent is needed between two clusters
134 "name":"Global1ToGlobal2",
135 "consumer":"192.168.0.1:2181",
136 "producer":"192.168.0.2:9092"
140 public String createMirrorMaker( String consumerPort, String producerPort ) {
141 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"createMirrorMaker\": {" );
142 str.append( " \"name\": \"" + this.getMmName() + "\", " );
143 str.append( " \"consumer\": \"" + this.sourceCluster + ":" + consumerPort + "\", " );
144 str.append( " \"producer\": \"" + this.targetCluster + ":" + producerPort + "\" ");
146 str.append( " } }" );
148 return str.toString();
152 public String getSourceCluster() {
153 return sourceCluster;
156 public void setSourceCluster(String sourceCluster) {
157 this.sourceCluster = sourceCluster;
160 public String getTargetCluster() {
161 return targetCluster;
164 public void setTargetCluster(String targetCluster) {
165 this.targetCluster = targetCluster;
169 public Set<ReplicationVector> getVectors() {
173 public void setVectors(Set<ReplicationVector> vectors) {
174 this.vectors = vectors;
176 public ArrayList<String> getTopics() {
180 //public void setVectors(Set<ReplicationVector> vectors) {
181 public void setTopics(ArrayList<String> topics) {
182 this.topics = topics;
186 public static String genKey( String s, String t) {
187 StringBuilder str = new StringBuilder();
191 return str.toString();
196 public void addTopic( String topic ) {
197 if ( ! topics.contains(topic)) {
200 logger.info( "Mirrormaker.addTopic: topic=" + topic + " . Now have " + topics.size() + " topics" );
203 public int getTopicCount() {
204 return topics.size();