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