Remove major and minor code smells in dr-node
[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.OutputStream;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Timer;
36
37 /**
38  * Track redirections of subscriptions
39  */
40 public class RedirManager {
41
42     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(RedirManager.class);
43     RateLimitedOperation op;
44     private HashMap<String, String> sid2primary = new HashMap<>();
45     private HashMap<String, String> sid2secondary = new HashMap<>();
46     private String redirfile;
47
48     /**
49      * Create a mechanism for maintaining subscription redirections.
50      *
51      * @param redirfile The file to store the redirection information.
52      * @param mininterval The minimum number of milliseconds between writes to the redirection 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                     StringBuilder sb = new StringBuilder();
61                     for (Map.Entry<String, String> entry : sid2primary.entrySet()) {
62                         String s = entry.getKey();
63                         String value = entry.getValue();
64                         sb.append(s).append(' ').append(value).append(' ')
65                                 .append(sid2secondary.get(s)).append('\n');
66                     }
67                     try (OutputStream os = new FileOutputStream(RedirManager.this.redirfile)) {
68                         os.write(sb.toString().getBytes());
69                     }
70                 } catch (Exception e) {
71                     eelfLogger.error("Exception", e);
72                 }
73             }
74         };
75         try {
76             String s;
77             try (BufferedReader br = new BufferedReader(new FileReader(redirfile))) {
78                 while ((s = br.readLine()) != null) {
79                     addSubRedirInfo(s);
80                 }
81             }
82         } catch (Exception e) {
83             eelfLogger.debug("Missing file is normal", e);
84         }
85     }
86
87     /**
88      * Set up redirection.  If a request is to be sent to subscription ID sid, and that is configured to go to URL
89      * primary, instead, go to secondary.
90      *
91      * @param sid The subscription ID to be redirected
92      * @param primary The URL associated with that subscription ID
93      * @param secondary The replacement URL to use instead
94      */
95     public synchronized void redirect(String sid, String primary, String secondary) {
96         sid2primary.put(sid, primary);
97         sid2secondary.put(sid, secondary);
98         op.request();
99     }
100
101     /**
102      * Cancel redirection.  If a request is to be sent to subscription ID sid, send it to its primary URL.
103      *
104      * @param sid The subscription ID to remove from the table.
105      */
106     public synchronized void forget(String sid) {
107         sid2primary.remove(sid);
108         sid2secondary.remove(sid);
109         op.request();
110     }
111
112     /**
113      * Look up where to send a subscription.  If the primary has changed or there is no redirection, use the primary.
114      * Otherwise, redirect to the secondary URL.
115      *
116      * @param sid The subscription ID to look up.
117      * @param primary The configured primary URL.
118      * @return The destination URL to really use.
119      */
120     public synchronized String lookup(String sid, String primary) {
121         String oprim = sid2primary.get(sid);
122         if (primary.equals(oprim)) {
123             return (sid2secondary.get(sid));
124         } else if (oprim != null) {
125             forget(sid);
126         }
127         return (primary);
128     }
129
130     /**
131      * Is a subscription redirected?
132      */
133     public synchronized boolean isRedirected(String sid) {
134         return (sid != null && sid2secondary.get(sid) != null);
135     }
136
137     private void addSubRedirInfo(String s) {
138         s = s.trim();
139         String[] sx = s.split(" ");
140         if (s.startsWith("#") || sx.length != 3) {
141             return;
142         }
143         sid2primary.put(sx[0], sx[1]);
144         sid2secondary.put(sx[0], sx[2]);
145     }
146 }