7e4078f813b8b4484f5f6324b9a2336a30c61ff3
[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 java.io.BufferedReader;
28 import java.io.FileOutputStream;
29 import java.io.FileReader;
30 import java.io.OutputStream;
31 import java.util.Hashtable;
32 import java.util.Timer;
33
34 /**
35  * Track redirections of subscriptions
36  */
37 public class RedirManager {
38
39     private Hashtable<String, String> sid2primary = new Hashtable<String, String>();
40     private Hashtable<String, String> sid2secondary = new Hashtable<String, String>();
41     private String redirfile;
42     RateLimitedOperation op;
43
44     /**
45      * Create a mechanism for maintaining subscription redirections.
46      *
47      * @param redirfile The file to store the redirection information.
48      * @param mininterval The minimum number of milliseconds between writes to the redirection
49      * information file.
50      * @param timer The timer thread used to run delayed file writes.
51      */
52     public RedirManager(String redirfile, long mininterval, Timer timer) {
53         this.redirfile = redirfile;
54         op = new RateLimitedOperation(mininterval, timer) {
55             public void run() {
56                 try {
57                     StringBuffer sb = new StringBuffer();
58                     for (String s : sid2primary.keySet()) {
59                         sb.append(s).append(' ').append(sid2primary.get(s)).append(' ')
60                             .append(sid2secondary.get(s)).append('\n');
61                     }
62                     try (OutputStream os = new FileOutputStream(RedirManager.this.redirfile)) {
63                         os.write(sb.toString().getBytes());
64                     }
65                 } catch (Exception e) {
66                 }
67             }
68         };
69         try {
70             String s;
71             try (BufferedReader br = new BufferedReader(new FileReader(redirfile))) {
72                 while ((s = br.readLine()) != null) {
73                     s = s.trim();
74                     String[] sx = s.split(" ");
75                     if (s.startsWith("#") || sx.length != 3) {
76                         continue;
77                     }
78                     sid2primary.put(sx[0], sx[1]);
79                     sid2secondary.put(sx[0], sx[2]);
80                 }
81             }
82         } catch (Exception e) {
83             // missing file is normal
84         }
85     }
86
87     /**
88      * Set up redirection.  If a request is to be sent to subscription ID sid, and that is
89      * configured to go to URL 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
103      * primary URL.
104      *
105      * @param sid The subscription ID to remove from the table.
106      */
107     public synchronized void forget(String sid) {
108         sid2primary.remove(sid);
109         sid2secondary.remove(sid);
110         op.request();
111     }
112
113     /**
114      * Look up where to send a subscription.  If the primary has changed or there is no redirection,
115      * use the primary.  Otherwise, redirect to the secondary URL.
116      *
117      * @param sid The subscription ID to look up.
118      * @param primary The configured primary URL.
119      * @return The destination URL to really use.
120      */
121     public synchronized String lookup(String sid, String primary) {
122         String oprim = sid2primary.get(sid);
123         if (primary.equals(oprim)) {
124             return (sid2secondary.get(sid));
125         } else if (oprim != null) {
126             forget(sid);
127         }
128         return (primary);
129     }
130
131     /**
132      * Is a subscription redirected?
133      */
134     public synchronized boolean isRedirected(String sid) {
135         return (sid != null && sid2secondary.get(sid) != null);
136     }
137 }