Limit number of topics per 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
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         public MirrorMaker(String source, String target, int i) {
45                 initMM( source, target );
46                 // original mm names did not have any index, so leave off index 0 for
47                 // backwards compatibility
48                 if ( i != 0 ) {
49                         String n = this.getMmName() + "_" + i;
50                         this.setMmName(n);
51                 }
52         }
53         public MirrorMaker(String source, String target) {
54                 initMM( source, target );
55         }
56         
57         private void initMM(String source, String target) {
58                 sourceCluster = source;
59                 targetCluster = target;
60                 mmName = genKey(source, target);
61                 topics = new ArrayList<String>();
62
63         }
64         
65         public String getMmName() {
66                 return mmName;
67         }
68
69         public void setMmName(String mmName) {
70                 this.mmName = mmName;
71         }
72
73         
74         // returns the JSON for MM message containing which Topics to replicate
75         /* 
76          * example:
77          * 
78                         {
79                             "messageID":"12349",
80                             "updateWhiteList":
81                                 {
82                                     "name":"Global1ToGlobal3",
83                                     "whitelist":"org.openecomp.dcae.topic1,org.openecomp.dcae.topic2"
84                                 }
85                         }   
86          */
87         public String getWhitelistUpdateJSON() {
88                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"updateWhiteList\": {"  );
89                 str.append( " \"name\": \"" + this.getMmName() + "\", \"whitelist\": \"" );
90                 int numTargets = 0;
91
92                 //for (ReplicationVector rv: vectors) {
93                 for (String rv: topics) {
94                         if ( numTargets > 0 ) {
95                                 str.append( ",");
96                         }
97                         //str.append(  rv.getFqtn() );
98                         str.append( rv );
99                         numTargets++;
100                 }
101                 str.append( "\" } }" );
102                 
103                 return str.toString();
104         }
105         
106         // returns the JSON for MM message indicating that a MM agent is needed between two clusters
107         // example:
108         /*
109          * 
110                         {
111                             "messageID":"12345"
112                             "createMirrorMaker":
113                                 {
114                                     "name":"Global1ToGlobal2",
115                                     "consumer":"192.168.0.1:2181",
116                                     "producer":"192.168.0.2:9092"
117                                 }
118                         }
119          */
120         public String createMirrorMaker( String consumerPort, String producerPort ) {
121                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"createMirrorMaker\": {"  );
122                 str.append( " \"name\": \"" + this.getMmName() + "\", " );
123                 str.append( " \"consumer\": \"" + this.sourceCluster + ":" + consumerPort + "\", " );
124                 str.append( " \"producer\": \"" + this.targetCluster + ":" + producerPort + "\", ");
125                 
126                 str.append( " \"numStreams\": \"10\" } }" );
127                 
128                 return str.toString();
129         }
130
131
132         public String getSourceCluster() {
133                 return sourceCluster;
134         }
135
136         public void setSourceCluster(String sourceCluster) {
137                 this.sourceCluster = sourceCluster;
138         }
139
140         public String getTargetCluster() {
141                 return targetCluster;
142         }
143
144         public void setTargetCluster(String targetCluster) {
145                 this.targetCluster = targetCluster;
146         }
147
148
149         public ArrayList<String> getTopics() {
150                 return topics;
151         }
152
153         
154         public void setTopics(ArrayList<String> topics) {
155                 this.topics = topics;
156         }
157
158
159         public static String genKey( String s, String t) {
160                 StringBuilder str = new StringBuilder();
161                 str.append(s);
162                 str.append("-To-");
163                 str.append(t);
164                 return str.toString();
165         }
166
167
168         
169         public void addTopic( String topic ) {
170                 if ( ! topics.contains(topic)) {        
171                         topics.add(topic);
172                 }
173                 logger.info( "Mirrormaker.addTopic: topic=" + topic + " . Now have " + topics.size() + " topics" );
174         }
175         
176         public int getTopicCount() {
177                 return topics.size();
178         }
179 }