Alternative MR replication method
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / model / MirrorMaker.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.model;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.apache.log4j.Logger;
28 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
29 import org.onap.dmaap.dbcapi.service.MirrorMakerService;
30
31 public class MirrorMaker extends DmaapObject {
32         static final Logger logger = Logger.getLogger(MirrorMaker.class);
33
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
38         
39
40         
41         public MirrorMaker(){
42                 
43         }
44
45         public MirrorMaker(String source, String target) {
46                 sourceCluster = source;
47                 targetCluster = target;
48                 mmName = genKey(source, target);
49                 topics = new ArrayList<String>();
50
51         }
52         
53         public String getMmName() {
54                 return mmName;
55         }
56
57         public void setMmName(String mmName) {
58                 this.mmName = mmName;
59         }
60
61         
62         // returns the JSON for MM message containing which Topics to replicate
63         /* 
64          * example:
65          * 
66                         {
67                             "messageID":"12349",
68                             "updateWhiteList":
69                                 {
70                                     "name":"Global1ToGlobal3",
71                                     "whitelist":"org.openecomp.dcae.topic1,org.openecomp.dcae.topic2"
72                                 }
73                         }   
74          */
75         public String updateWhiteList() {
76                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"updateWhiteList\": {"  );
77                 str.append( " \"name\": \"" + this.getMmName() + "\", \"whitelist\": \"" );
78                 int numTargets = 0;
79
80                 //for (ReplicationVector rv: vectors) {
81                 for (String rv: topics) {
82                         if ( numTargets > 0 ) {
83                                 str.append( ",");
84                         }
85                         //str.append(  rv.getFqtn() );
86                         str.append( rv );
87                         numTargets++;
88                 }
89                 str.append( "\" } }" );
90                 
91                 return str.toString();
92         }
93         
94         // returns the JSON for MM message indicating that a MM agent is needed between two clusters
95         // example:
96         /*
97          * 
98                         {
99                             "messageID":"12345"
100                             "createMirrorMaker":
101                                 {
102                                     "name":"Global1ToGlobal2",
103                                     "consumer":"192.168.0.1:2181",
104                                     "producer":"192.168.0.2:9092"
105                                 }
106                         }
107          */
108         public String createMirrorMaker( String consumerPort, String producerPort ) {
109                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"createMirrorMaker\": {"  );
110                 str.append( " \"name\": \"" + this.getMmName() + "\", " );
111                 str.append( " \"consumer\": \"" + this.sourceCluster + ":" + consumerPort + "\", " );
112                 str.append( " \"producer\": \"" + this.targetCluster + ":" + producerPort + "\" ");
113                 
114                 str.append( " } }" );
115                 
116                 return str.toString();
117         }
118
119
120         public String getSourceCluster() {
121                 return sourceCluster;
122         }
123
124         public void setSourceCluster(String sourceCluster) {
125                 this.sourceCluster = sourceCluster;
126         }
127
128         public String getTargetCluster() {
129                 return targetCluster;
130         }
131
132         public void setTargetCluster(String targetCluster) {
133                 this.targetCluster = targetCluster;
134         }
135
136
137         public ArrayList<String> getTopics() {
138                 return topics;
139         }
140
141         
142         public void setTopics(ArrayList<String> topics) {
143                 this.topics = topics;
144         }
145
146
147         public static String genKey( String s, String t) {
148                 StringBuilder str = new StringBuilder();
149                 str.append(s);
150                 str.append("-To-");
151                 str.append(t);
152                 return str.toString();
153         }
154
155
156         
157         public void addTopic( String topic ) {
158                 if ( ! topics.contains(topic)) {        
159                         topics.add(topic);
160                 }
161                 logger.info( "Mirrormaker.addTopic: topic=" + topic + " . Now have " + topics.size() + " topics" );
162         }
163         
164         public int getTopicCount() {
165                 return topics.size();
166         }
167 }