2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.service;
23 import java.util.ArrayList;
24 import java.util.List;
35 //import org.openecomp.dmaapbc.aaf.AndrewDecryptor;
36 import org.onap.dmaap.dbcapi.aaf.AafDecrypt;
37 import org.onap.dmaap.dbcapi.client.MrTopicConnection;
38 import org.onap.dmaap.dbcapi.database.DatabaseClass;
39 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
40 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
41 import org.onap.dmaap.dbcapi.model.ApiError;
42 import org.onap.dmaap.dbcapi.model.MR_Cluster;
43 import org.onap.dmaap.dbcapi.model.MirrorMaker;
44 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
45 import org.onap.dmaap.dbcapi.util.DmaapConfig;
46 import org.onap.dmaap.dbcapi.util.RandomInteger;
48 public class MirrorMakerService extends BaseLoggingClass {
50 private Map<String, MirrorMaker> mirrors = DatabaseClass.getMirrorMakers();
51 private static MrTopicConnection prov;
52 private static AafDecrypt decryptor;
54 private static String provUser;
55 private static String provUserPwd;
56 private static String defaultProducerPort;
57 private static String defaultConsumerPort;
58 private static String centralFqdn;
59 private int maxTopicsPerMM;
61 public MirrorMakerService() {
63 decryptor = new AafDecrypt();
64 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
65 provUser = p.getProperty("MM.ProvUserMechId");
66 provUserPwd = decryptor.decrypt(p.getProperty( "MM.ProvUserPwd", "notSet" ));
67 defaultProducerPort = p.getProperty( "MR.SourceReplicationPort", "9092");
68 defaultConsumerPort = p.getProperty( "MR.TargetReplicationPort", "2181");
69 centralFqdn = p.getProperty("MR.CentralCname", "notSet");
70 maxTopicsPerMM = Integer.valueOf( p.getProperty( "MaxTopicsPerMM", "5"));
73 // will create a MM on MMagent if needed
74 // will update the MMagent whitelist with all topics for this MM
75 public MirrorMaker updateMirrorMaker( MirrorMaker mm ) {
76 logger.info( "updateMirrorMaker");
78 prov = new MrTopicConnection( provUser, provUserPwd );
80 DmaapService dmaap = new DmaapService();
81 MR_ClusterService clusters = new MR_ClusterService();
83 // in 1610, MM should only exist for edge-to-central
84 // we use a cname for the central MR cluster that is active, and provision on agent topic on that target
85 // but only send 1 message so MM Agents can read it relying on kafka delivery
86 for( MR_Cluster central: clusters.getCentralClusters() ) {
87 prov.makeTopicConnection(central, dmaap.getBridgeAdminFqtn(), centralFqdn );
88 ApiError resp = prov.doPostMessage(mm.createMirrorMaker( defaultConsumerPort, defaultProducerPort ));
89 if ( ! resp.is2xx() ) {
91 errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR, "create MM", Integer.toString(resp.getCode()), resp.getMessage());
92 mm.setStatus(DmaapObject_Status.INVALID);
94 prov.makeTopicConnection(central, dmaap.getBridgeAdminFqtn(), centralFqdn );
95 resp = prov.doPostMessage(mm.getWhitelistUpdateJSON());
96 if ( ! resp.is2xx()) {
97 errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR,"MR Bridge", Integer.toString(resp.getCode()), resp.getMessage());
98 mm.setStatus(DmaapObject_Status.INVALID);
100 mm.setStatus(DmaapObject_Status.VALID);
104 // we only want to send one message even if there are multiple central clusters
112 return mirrors.put( mm.getMmName(), mm);
114 public MirrorMaker getMirrorMaker( String part1, String part2, int index ) {
117 // original mm names did not have any index, so leave off index 0 for
118 // backwards compatibility
122 targetPart = part2 + "_" + index;
124 logger.info( "getMirrorMaker using " + part1 + " and " + targetPart );
125 return mirrors.get(MirrorMaker.genKey(part1, targetPart));
127 public MirrorMaker getMirrorMaker( String part1, String part2 ) {
128 logger.info( "getMirrorMaker using " + part1 + " and " + part2 );
129 return mirrors.get(MirrorMaker.genKey(part1, part2));
131 public MirrorMaker getMirrorMaker( String key ) {
132 logger.info( "getMirrorMaker using " + key);
133 return mirrors.get(key);
137 public void delMirrorMaker( MirrorMaker mm ) {
138 logger.info( "delMirrorMaker");
139 mirrors.remove(mm.getMmName());
142 // TODO: this should probably return sequential values or get replaced by the MM client API
143 // but it should be sufficient for initial 1610 development
144 public static String genTransactionId() {
145 RandomInteger ri = new RandomInteger(100000);
146 int randomInt = ri.next();
147 return Integer.toString(randomInt);
149 public List<String> getAllMirrorMakers() {
150 List<String> ret = new ArrayList<String>();
151 for( String key: mirrors.keySet()) {
158 public MirrorMaker getNextMM( String source, String target, String fqtn ) {
160 MirrorMaker mm = null;
161 while( mm == null ) {
163 mm = this.getMirrorMaker( source, target, i);
165 mm = new MirrorMaker(source, target, i);
167 if ( mm.getTopics().contains(fqtn) ) {
170 if ( mm.getTopicCount() >= maxTopicsPerMM ) {
171 logger.info( "getNextMM: MM " + mm.getMmName() + " has " + mm.getTopicCount() + " topics. Moving to next MM");
181 public MirrorMaker splitMM( MirrorMaker orig ) {
183 String source = orig.getSourceCluster();
184 String target = orig.getTargetCluster();
187 ArrayList<String> whitelist = orig.getTopics();
188 while( whitelist.size() > maxTopicsPerMM ) {
190 int last = whitelist.size() - 1;
191 String topic = whitelist.get(last);
192 whitelist.remove(last);
193 MirrorMaker mm = this.getNextMM( source, target, "aValueThatShouldNotMatchAnything" );
195 this.updateMirrorMaker(mm);
198 orig.setTopics(whitelist);