No topic dups in mmagent whitelist
[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         private Set<ReplicationVector> vectors;
40
41         
42         public MirrorMaker(){
43                 
44         }
45
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>();
52
53         }
54         
55         public String getMmName() {
56                 return mmName;
57         }
58
59         public void setMmName(String mmName) {
60                 this.mmName = mmName;
61         }
62
63         
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 );
68                 }
69                 vectors.add(new ReplicationVector( fqtn, source, target ));
70         }
71         
72         public void delVector( String fqtn, String source, String target ) {
73                 vectors.remove(new ReplicationVector( fqtn, source, target));
74         }
75
76         
77         
78         public String toJSON() {
79                 StringBuilder str = new StringBuilder( "{ \"source\": " + sourceCluster + ",\"topics\": ["  );
80                 int numTargets = 0;
81                 for (ReplicationVector rv: vectors) {
82                         if ( numTargets > 0 ) {
83                                 str.append( ",");
84                         }
85                         str.append( " \"target\": " + rv.getTargetCluster() + ", \"topic\": " + rv.getFqtn());
86                         numTargets++;
87                 }
88                 str.append( "] }" );
89                 
90                 return str.toString();
91         }
92                 
93         
94         // returns the JSON for MM message containing which Topics to replicate
95         /* 
96          * example:
97          * 
98                         {
99                             "messageID":"12349",
100                             "updateWhiteList":
101                                 {
102                                     "name":"Global1ToGlobal3",
103                                     "whitelist":"org.openecomp.dcae.topic1,org.openecomp.dcae.topic2"
104                                 }
105                         }   
106          */
107         public String updateWhiteList() {
108                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"updateWhiteList\": {"  );
109                 str.append( " \"name\": \"" + this.getMmName() + "\", \"whitelist\": \"" );
110                 int numTargets = 0;
111
112                 //for (ReplicationVector rv: vectors) {
113                 for (String rv: topics) {
114                         if ( numTargets > 0 ) {
115                                 str.append( ",");
116                         }
117                         //str.append(  rv.getFqtn() );
118                         str.append( rv );
119                         numTargets++;
120                 }
121                 str.append( "\" } }" );
122                 
123                 return str.toString();
124         }
125         
126         // returns the JSON for MM message indicating that a MM agent is needed between two clusters
127         // example:
128         /*
129          * 
130                         {
131                             "messageID":"12345"
132                             "createMirrorMaker":
133                                 {
134                                     "name":"Global1ToGlobal2",
135                                     "consumer":"192.168.0.1:2181",
136                                     "producer":"192.168.0.2:9092"
137                                 }
138                         }
139          */
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 + "\" ");
145                 
146                 str.append( " } }" );
147                 
148                 return str.toString();
149         }
150
151
152         public String getSourceCluster() {
153                 return sourceCluster;
154         }
155
156         public void setSourceCluster(String sourceCluster) {
157                 this.sourceCluster = sourceCluster;
158         }
159
160         public String getTargetCluster() {
161                 return targetCluster;
162         }
163
164         public void setTargetCluster(String targetCluster) {
165                 this.targetCluster = targetCluster;
166         }
167
168
169         public Set<ReplicationVector> getVectors() {
170                 return vectors;
171         }
172
173         public void setVectors(Set<ReplicationVector> vectors) {
174                 this.vectors = vectors;
175         }
176         public ArrayList<String> getTopics() {
177                 return topics;
178         }
179
180         //public void setVectors(Set<ReplicationVector> vectors) {
181         public void setTopics(ArrayList<String> topics) {
182                 this.topics = topics;
183         }
184
185
186         public static String genKey( String s, String t) {
187                 StringBuilder str = new StringBuilder();
188                 str.append(s);
189                 str.append("-To-");
190                 str.append(t);
191                 return str.toString();
192         }
193
194
195         
196         public void addTopic( String topic ) {
197                 if ( ! topics.contains(topic)) {        
198                         topics.add(topic);
199                 }
200                 logger.info( "Mirrormaker.addTopic: topic=" + topic + " . Now have " + topics.size() + " topics" );
201         }
202         
203         public int getTopicCount() {
204                 return topics.size();
205         }
206 }