aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/RoutingTable.java
blob: 74ae46353c5fb9b90b0d43f9b654965a28c760d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * At any time there may only ever be zero or one routing table registered in message bus for each protocol. This class
 * contains a list of named hops and routes that may be used to substitute references to these during route resolving.
 *
 * @author Simon Thoresen Hult
 */
public class RoutingTable {

    private final Map<String, HopBlueprint> hops = new LinkedHashMap<>();
    private final Map<String, Route> routes = new LinkedHashMap<>();

    /**
     * Creates a new routing table based on a given specification. This also verifies the integrity of the table.
     *
     * @param spec The specification to use.
     */
    public RoutingTable(RoutingTableSpec spec) {
        for (int i = 0; i < spec.getNumHops(); ++i) {
            HopSpec hopSpec = spec.getHop(i);
            hops.put(hopSpec.getName(), new HopBlueprint(hopSpec));
        }
        for (int i = 0; i < spec.getNumRoutes(); ++i) {
            RouteSpec routeSpec = spec.getRoute(i);
            Route route = new Route();
            for (int j = 0; j < routeSpec.getNumHops(); ++j) {
                route.addHop(Hop.parse(routeSpec.getHop(j)));
            }
            routes.put(routeSpec.getName(), route);
        }
    }

    /**
     * Returns whether or not there are any hops in this routing table.
     *
     * @return True if there is at least one hop.
     */
    public boolean hasHops() {
        return !hops.isEmpty();
    }

    /**
     * Returns the number of hops that are contained in this.
     *
     * @return The number of hops.
     */
    public int getNumHops() {
        return hops.size();
    }

    /**
     * Returns an iterator for the hops of this table.
     *
     * @return An iterator.
     */
    public HopIterator getHopIterator() {
        return new HopIterator(hops);
    }

    /**
     * Returns an iterator for the routes of this table.
     *
     * @return An iterator.
     */
    public RouteIterator getRouteIterator() {
        return new RouteIterator(routes);
    }

    /**
     * Returns whether or not there are any routes in this routing table.
     *
     * @return True if there is at least one route.
     */
    public boolean hasRoutes() {
        return !routes.isEmpty();
    }

    /**
     * Returns the number of routes that are contained in this.
     *
     * @return The number of routes.
     */
    public int getNumRoutes() {
        return routes.size();
    }

    /**
     * Returns whether or not there exists a named hop in this.
     *
     * @param name The name of the hop to look for.
     * @return True if the named hop exists.
     */
    public boolean hasHop(String name) {
        return hops.containsKey(name);
    }

    /**
     * Returns the named hop, may be null.
     *
     * @param name The name of the hop to return.
     * @return The hop implementation object.
     */
    public HopBlueprint getHop(String name) {
        return hops.get(name);
    }

    /**
     * Returns whether or not there exists a named route in this.
     *
     * @param name The name of the route to look for.
     * @return True if the named route exists.
     */
    public boolean hasRoute(String name) {
        return routes.containsKey(name);
    }

    /**
     * Returns the named route, may be null.
     *
     * @param name The name of the route to return.
     * @return The route implementation object.
     */
    public Route getRoute(String name) {
        return routes.get(name);
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder("RoutingTable(hops = { ");
        int i = 0;
        for (String name : hops.keySet()) {
            ret.append("'").append(name).append("' : ").append(hops.get(name));
            if (i++ < hops.size() - 1) {
                ret.append(", ");
            }
        }
        ret.append(" }, routes = { ");
        i = 0;
        for (String name : routes.keySet()) {
            ret.append("'").append(name).append("' : ").append(routes.get(name));
            if (i++ < routes.size()) {
                ret.append(", ");
            }
        }
        ret.append(" })");
        return ret.toString();
    }

    /**
     * Implements an iterator for the hops of this. Use {@link RoutingTable#getHopIterator()}
     * to retrieve an instance of this.
     */
    public static class HopIterator {

        private Iterator<Map.Entry<String, HopBlueprint>> it;
        private Map.Entry<String, HopBlueprint> entry;

        /**
         * Constructs a new iterator based on a given map. This is private so that only a {@link RoutingTable} can
         * create one.
         *
         * @param hops the map to iterate through
         */
        private HopIterator(Map<String, HopBlueprint> hops) {
            it = hops.entrySet().iterator();
            next();
        }

        /** Steps to the next hop in the map. */
        public void next() {
            entry = it.hasNext() ? it.next() : null;
        }

        /** Returns whether this iterator is valid. */
        public boolean isValid() {
            return entry != null;
        }

        /** Returns the name of the current hop. */
        public String getName() {
            return entry.getKey();
        }

        /** Returns the current hop. */
        public HopBlueprint getHop() {
            return entry.getValue();
        }
    }

    /**
     * Implements an iterator for the routes of this. Use {@link RoutingTable#getRouteIterator()}
     * to retrieve an instance of this.
     */
    public static class RouteIterator {

        private Iterator<Map.Entry<String, Route>> it;
        private Map.Entry<String, Route> entry;

        /**
         * Constructs a new iterator based on a given map. This is private so that only a {@link RoutingTable} can
         * create one.
         *
         * @param routes the map to iterate through
         */
        private RouteIterator(Map<String, Route> routes) {
            it = routes.entrySet().iterator();
            next();
        }

        /** Steps to the next route in the map. */
        public void next() {
            entry = it.hasNext() ? it.next() : null;
        }

        /** Returns whether this iterator is valid. */
        public boolean isValid() {
            return entry != null;
        }

        /** Returns the name of the current route. */
        public String getName() {
            return entry.getKey();
        }

        /** Returns the current route. */
        public Route getRoute() {
            return entry.getValue();
        }
    }
}