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