[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-node / src / main / java / com / att / research / datarouter / node / RedirManager.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package com.att.research.datarouter.node;\r
26 \r
27 import java.util.*;\r
28 import java.io.*;\r
29 \r
30 /**\r
31  *      Track redirections of subscriptions\r
32  */\r
33 public class RedirManager       {\r
34         private Hashtable<String, String> sid2primary = new Hashtable<String, String>();\r
35         private Hashtable<String, String> sid2secondary = new Hashtable<String, String>();\r
36         private String  redirfile;\r
37         RateLimitedOperation    op;\r
38         /**\r
39          *      Create a mechanism for maintaining subscription redirections.\r
40          *      @param redirfile        The file to store the redirection information.\r
41          *      @param mininterval      The minimum number of milliseconds between writes to the redirection information file.\r
42          *      @param timer    The timer thread used to run delayed file writes.\r
43          */\r
44         public RedirManager(String redirfile, long mininterval, Timer timer) {\r
45                 this.redirfile = redirfile;\r
46                 op = new RateLimitedOperation(mininterval, timer) {\r
47                         public void run() {\r
48                                 try {\r
49                                         StringBuffer sb = new StringBuffer();\r
50                                         for (String s: sid2primary.keySet()) {\r
51                                                 sb.append(s).append(' ').append(sid2primary.get(s)).append(' ').append(sid2secondary.get(s)).append('\n');\r
52                                         }\r
53                                         OutputStream os = new FileOutputStream(RedirManager.this.redirfile);\r
54                                         os.write(sb.toString().getBytes());\r
55                                         os.close();\r
56                                 } catch (Exception e) {\r
57                                 }\r
58                         }\r
59                 };\r
60                 try {\r
61                         String s;\r
62                         BufferedReader br = new BufferedReader(new FileReader(redirfile));\r
63                         while ((s = br.readLine()) != null) {\r
64                                 s = s.trim();\r
65                                 String[] sx = s.split(" ");\r
66                                 if (s.startsWith("#") || sx.length != 3) {\r
67                                         continue;\r
68                                 }\r
69                                 sid2primary.put(sx[0], sx[1]);\r
70                                 sid2secondary.put(sx[0], sx[2]);\r
71                         }\r
72                         br.close();\r
73                 } catch (Exception e) {\r
74                         // missing file is normal\r
75                 }\r
76         }\r
77         /**\r
78          *      Set up redirection.  If a request is to be sent to subscription ID sid, and that is configured to go to URL primary, instead, go to secondary.\r
79          *      @param sid      The subscription ID to be redirected\r
80          *      @param primary  The URL associated with that subscription ID\r
81          *      @param secondary        The replacement URL to use instead\r
82          */\r
83         public synchronized void redirect(String sid, String primary, String secondary) {\r
84                 sid2primary.put(sid, primary);\r
85                 sid2secondary.put(sid, secondary);\r
86                 op.request();\r
87         }\r
88         /**\r
89          *      Cancel redirection.  If a request is to be sent to subscription ID sid, send it to its primary URL.\r
90          *      @param  sid     The subscription ID to remove from the table.\r
91          */\r
92         public synchronized void forget(String sid) {\r
93                 sid2primary.remove(sid);\r
94                 sid2secondary.remove(sid);\r
95                 op.request();\r
96         }\r
97         /**\r
98          *      Look up where to send a subscription.  If the primary has changed or there is no redirection, use the primary.  Otherwise, redirect to the secondary URL.\r
99          *      @param  sid     The subscription ID to look up.\r
100          *      @param  primary The configured primary URL.\r
101          *      @return The destination URL to really use.\r
102          */\r
103         public synchronized String lookup(String sid, String primary) {\r
104                 String oprim = sid2primary.get(sid);\r
105                 if (primary.equals(oprim)) {\r
106                         return(sid2secondary.get(sid));\r
107                 } else if (oprim != null) {\r
108                         forget(sid);\r
109                 }       \r
110                 return(primary);\r
111         }\r
112         /**\r
113          *      Is a subscription redirected?\r
114          */\r
115         public synchronized boolean isRedirected(String sid) {\r
116                 return(sid != null && sid2secondary.get(sid) != null);\r
117         }\r
118 }\r