[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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 org.onap.dmaap.dbcapi.service.MirrorMakerService;
24
25 import java.util.ArrayList;
26
27 public class MirrorMaker extends DmaapObject {
28
29         private String  sourceCluster;
30         private String  targetCluster;
31         private String  mmName;
32         private ArrayList<String> topics;  //re-using this var name for backwards DB compatibility
33
34         public MirrorMaker(){
35                 
36         }
37
38         public MirrorMaker(String source, String target, int i) {
39                 initMM( source, target );
40                 // original mm names did not have any index, so leave off index 0 for
41                 // backwards compatibility
42                 if ( i != 0 ) {
43                         String n = this.getMmName() + "_" + i;
44                         this.setMmName(n);
45                 }
46         }
47
48         public MirrorMaker(String source, String target) {
49                 initMM( source, target );
50         }
51         
52         private void initMM(String source, String target) {
53                 sourceCluster = source;
54                 targetCluster = target;
55                 mmName = genKey(source, target);
56                 topics = new ArrayList<>();
57
58         }
59         
60         public String getMmName() {
61                 return mmName;
62         }
63
64         public void setMmName(String mmName) {
65                 this.mmName = mmName;
66         }
67
68         // returns the JSON for MM message containing which Topics to replicate
69         /* 
70          * example:
71          * 
72                         {
73                             "messageID":"12349",
74                             "updateWhiteList":
75                                 {
76                                     "name":"Global1ToGlobal3",
77                                     "whitelist":"org.openecomp.dcae.topic1,org.openecomp.dcae.topic2"
78                                 }
79                         }   
80          */
81         public String getWhitelistUpdateJSON() {
82                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"updateWhiteList\": {"  );
83                 str.append( " \"name\": \"" + this.getMmName() + "\", \"whitelist\": \"" );
84                 int numTargets = 0;
85
86                 for (String rv: topics) {
87                         if ( numTargets > 0 ) {
88                                 str.append( ",");
89                         }
90                         str.append( rv );
91                         numTargets++;
92                 }
93                 str.append( "\" } }" );
94                 
95                 return str.toString();
96         }
97         
98         // returns the JSON for MM message indicating that a MM agent is needed between two clusters
99         // example:
100         /*
101          * 
102                         {
103                             "messageID":"12345"
104                             "createMirrorMaker":
105                                 {
106                                     "name":"Global1ToGlobal2",
107                                     "consumer":"192.168.0.1:2181",
108                                     "producer":"192.168.0.2:9092"
109                                 }
110                         }
111          */
112         public String createMirrorMaker( String consumerPort, String producerPort ) {
113                 StringBuilder str = new StringBuilder( "{ \"messageID\": \"" + MirrorMakerService.genTransactionId() + "\", \"createMirrorMaker\": {"  );
114                 str.append( " \"name\": \"" + this.getMmName() + "\", " );
115                 str.append( " \"consumer\": \"" + this.sourceCluster + ":" + consumerPort + "\", " );
116                 str.append( " \"producer\": \"" + this.targetCluster + ":" + producerPort + "\", ");
117                 
118                 str.append( " \"numStreams\": \"10\" } }" );
119                 
120                 return str.toString();
121         }
122
123         public String getSourceCluster() {
124                 return sourceCluster;
125         }
126
127         public void setSourceCluster(String sourceCluster) {
128                 this.sourceCluster = sourceCluster;
129         }
130
131         public String getTargetCluster() {
132                 return targetCluster;
133         }
134
135         public void setTargetCluster(String targetCluster) {
136                 this.targetCluster = targetCluster;
137         }
138
139         public ArrayList<String> getTopics() {
140                 return topics;
141         }
142
143         public void setTopics(ArrayList<String> topics) {
144                 this.topics = topics;
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         public void addTopic( String topic ) {
156                 if ( ! topics.contains(topic)) {        
157                         topics.add(topic);
158                 }
159                 logger.info( "Mirrormaker.addTopic: topic=" + topic + " . Now have " + topics.size() + " topics" );
160         }
161
162         public int getTopicCount() {
163                 return topics.size();
164         }
165 }