Handling of MirrorMaker properties
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / MirrorMakerService.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.service;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27
28
29
30
31
32
33
34
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;
47
48 public class MirrorMakerService extends BaseLoggingClass {
49         
50         private Map<String, MirrorMaker> mirrors = DatabaseClass.getMirrorMakers();
51         private static MrTopicConnection prov;
52         private static AafDecrypt decryptor;
53         
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         
60         public MirrorMakerService() {
61                 super();
62                 decryptor = new AafDecrypt();
63                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
64                 provUser = p.getProperty("MM.ProvUserMechId");
65                 provUserPwd = decryptor.decrypt(p.getProperty( "MM.ProvUserPwd", "notSet" ));
66                 defaultProducerPort = p.getProperty( "MR.SourceReplicationPort", "9092");
67                 defaultConsumerPort = p.getProperty( "MR.TargetReplicationPort", "2181");       
68                 centralFqdn = p.getProperty("MR.CentralCname", "notSet");
69         }
70
71         // will create a MM on MMagent if needed
72         // will update the MMagent whitelist with all topics for this MM
73         public MirrorMaker updateMirrorMaker( MirrorMaker mm ) {
74                 logger.info( "updateMirrorMaker");
75         
76                 prov = new MrTopicConnection( provUser, provUserPwd );
77         
78                 DmaapService dmaap = new DmaapService();
79                 MR_ClusterService clusters = new MR_ClusterService();
80         
81                 // in 1610, MM should only exist for edge-to-central
82                 //  we use a cname for the central MR cluster that is active, and provision on agent topic on that target
83                 // but only send 1 message so MM Agents can read it relying on kafka delivery
84                 for( MR_Cluster central: clusters.getCentralClusters() ) {
85                         prov.makeTopicConnection(central, dmaap.getBridgeAdminFqtn(), centralFqdn  );
86                         ApiError resp = prov.doPostMessage(mm.createMirrorMaker( defaultConsumerPort, defaultProducerPort ));
87                         if ( ! resp.is2xx() ) {
88         
89                                 errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR, "create MM", Integer.toString(resp.getCode()), resp.getMessage());
90                                 mm.setStatus(DmaapObject_Status.INVALID);
91                         } else {
92                                 prov.makeTopicConnection(central, dmaap.getBridgeAdminFqtn(), centralFqdn );
93                                 resp = prov.doPostMessage(mm.updateWhiteList());
94                                 if ( ! resp.is2xx()) {
95                                         errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR,"MR Bridge", Integer.toString(resp.getCode()), resp.getMessage());
96                                         mm.setStatus(DmaapObject_Status.INVALID);
97                                 } else {
98                                         mm.setStatus(DmaapObject_Status.VALID);
99                                 }
100                         }
101                         
102                         // we only want to send one message even if there are multiple central clusters
103                         break;
104                 
105                 } 
106                 
107
108
109                 mm.setLastMod();
110                 return mirrors.put( mm.getMmName(), mm);
111         }
112         public MirrorMaker getMirrorMaker( String part1, String part2 ) {
113                 logger.info( "getMirrorMaker using " + part1 + " and " + part2 );
114                 return mirrors.get(MirrorMaker.genKey(part1, part2));
115         }       
116         public MirrorMaker getMirrorMaker( String key ) {
117                 logger.info( "getMirrorMaker using " + key);
118                 return mirrors.get(key);
119         }
120         
121         
122         public void delMirrorMaker( MirrorMaker mm ) {
123                 logger.info( "delMirrorMaker");
124                 mirrors.remove(mm.getMmName());
125         }
126         
127         // TODO: this should probably return sequential values or get replaced by the MM client API
128         // but it should be sufficient for initial 1610 development
129         public static String genTransactionId() {
130                 RandomInteger ri = new RandomInteger(100000);
131             int randomInt = ri.next();
132             return Integer.toString(randomInt);
133         }
134         public List<String> getAllMirrorMakers() {
135                 List<String> ret = new ArrayList<String>();
136                 for( String key: mirrors.keySet()) {
137                         ret.add( key );
138                 }
139                 
140                 return ret;
141         }
142
143 }