Remove datarouter-node critical code smells
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / RedirManager.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.node;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.BufferedReader;
30 import java.io.FileOutputStream;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.util.Hashtable;
35 import java.util.Timer;
36
37 /**
38  * Track redirections of subscriptions
39  */
40 public class RedirManager {
41     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(RedirManager.class);
42     private Hashtable<String, String> sid2primary = new Hashtable<String, String>();
43     private Hashtable<String, String> sid2secondary = new Hashtable<String, String>();
44     private String redirfile;
45     RateLimitedOperation op;
46
47     /**
48      * Create a mechanism for maintaining subscription redirections.
49      *
50      * @param redirfile The file to store the redirection information.
51      * @param mininterval The minimum number of milliseconds between writes to the redirection
52      * information file.
53      * @param timer The timer thread used to run delayed file writes.
54      */
55     public RedirManager(String redirfile, long mininterval, Timer timer) {
56         this.redirfile = redirfile;
57         op = new RateLimitedOperation(mininterval, timer) {
58             public void run() {
59                 try {
60                     StringBuffer sb = new StringBuffer();
61                     for (String s : sid2primary.keySet()) {
62                         sb.append(s).append(' ').append(sid2primary.get(s)).append(' ')
63                             .append(sid2secondary.get(s)).append('\n');
64                     }
65                     try (OutputStream os = new FileOutputStream(RedirManager.this.redirfile)) {
66                         os.write(sb.toString().getBytes());
67                     }
68                 } catch (Exception e) {
69                     eelfLogger.error("Exception", e);
70                 }
71             }
72         };
73         try {
74             String s;
75             try (BufferedReader br = new BufferedReader(new FileReader(redirfile))) {
76                 while ((s = br.readLine()) != null) {
77                     s = s.trim();
78                     String[] sx = s.split(" ");
79                     if (s.startsWith("#") || sx.length != 3) {
80                         continue;
81                     }
82                     sid2primary.put(sx[0], sx[1]);
83                     sid2secondary.put(sx[0], sx[2]);
84                 }
85             }
86         } catch (Exception e) {
87             eelfLogger.error("Missing file is normal", e);
88         }
89     }
90
91     /**
92      * Set up redirection.  If a request is to be sent to subscription ID sid, and that is
93      * configured to go to URL primary, instead, go to secondary.
94      *
95      * @param sid The subscription ID to be redirected
96      * @param primary The URL associated with that subscription ID
97      * @param secondary The replacement URL to use instead
98      */
99     public synchronized void redirect(String sid, String primary, String secondary) {
100         sid2primary.put(sid, primary);
101         sid2secondary.put(sid, secondary);
102         op.request();
103     }
104
105     /**
106      * Cancel redirection.  If a request is to be sent to subscription ID sid, send it to its
107      * primary URL.
108      *
109      * @param sid The subscription ID to remove from the table.
110      */
111     public synchronized void forget(String sid) {
112         sid2primary.remove(sid);
113         sid2secondary.remove(sid);
114         op.request();
115     }
116
117     /**
118      * Look up where to send a subscription.  If the primary has changed or there is no redirection,
119      * use the primary.  Otherwise, redirect to the secondary URL.
120      *
121      * @param sid The subscription ID to look up.
122      * @param primary The configured primary URL.
123      * @return The destination URL to really use.
124      */
125     public synchronized String lookup(String sid, String primary) {
126         String oprim = sid2primary.get(sid);
127         if (primary.equals(oprim)) {
128             return (sid2secondary.get(sid));
129         } else if (oprim != null) {
130             forget(sid);
131         }
132         return (primary);
133     }
134
135     /**
136      * Is a subscription redirected?
137      */
138     public synchronized boolean isRedirected(String sid) {
139         return (sid != null && sid2secondary.get(sid) != null);
140     }
141 }