aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/RouteSpec.java
blob: f987826c834706bc31ef449f57968a02566fb3c3 (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
// 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.ArrayList;
import java.util.List;

/**
 * Along with the {@link RoutingSpec}, {@link RoutingTableSpec} and {@link HopSpec}, this holds the routing
 * specifications for all protocols. The only way a client can configure or alter the settings of a message bus instance
 * is through these classes.
 * <p>
 * This class contains the spec for a single route.
 *
 * @author Simon Thoresen Hult
 */
public class RouteSpec {

    private final String name;
    private final List<String> hops = new ArrayList<String>();
    private final boolean verify;

    /**
     * Creates a new named route specification.
     *
     * @param name A protocol-unique name for this route.
     */
    public RouteSpec(String name) {
        this(name, true);
    }

    /**
     * Creates a new named route specification.
     *
     * @param name   A protocol-unique name for this route.
     * @param verify Whether or not this should be verified.
     */
    public RouteSpec(String name, boolean verify) {
        this.name = name;
        this.verify = verify;
    }

    /**
     * Implements the copy constructor.
     *
     * @param obj The object to copy.
     */
    public RouteSpec(RouteSpec obj) {
        this.name = obj.name;
        this.verify = obj.verify;
        for (String hop : obj.hops) {
            hops.add(hop);
        }
    }

    /**
     * Returns the protocol-unique name of this route.
     *
     * @return The name.
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the hop name at the given index.
     *
     * @param i The index of the hop to return.
     * @return The hop at the given index.
     */
    public String getHop(int i) {
        return hops.get(i);
    }

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

    /**
     * Returns the number of hops that make up this route.
     *
     * @return The number of hops.
     */
    public int getNumHops() {
        return hops.size();
    }

    /**
     * Adds the given hop name to this.
     *
     * @param hop The hop to add.
     * @return This, to allow chaining.
     */
    public RouteSpec addHop(String hop) {
        hops.add(hop);
        return this;
    }

    /**
     * Adds the given hop names to this.
     *
     * @param hops The hops to add.
     * @return This, to allow chaining.
     */
    public RouteSpec addHops(List<String> hops) {
        this.hops.addAll(hops);
        return this;
    }

    /**
     * Sets the hop name for a given index.
     *
     * @param i   The index of the hop to set.
     * @param hop The hop to set.
     * @return This, to allow chaining.
     */
    public RouteSpec setHop(int i, String hop) {
        hops.set(i, hop);
        return this;
    }

    /**
     * Removes the hop name at the given index.
     *
     * @param i The index of the hop to remove.
     * @return The removed hop.
     */
    public String removeHop(int i) {
        return hops.remove(i);
    }

    /**
     * Clears the list of hops that make up this route.
     *
     * @return This, to allow chaining.
     */
    public RouteSpec clearHops() {
        hops.clear();
        return this;
    }

    /**
     * Verifies the content of this against the given application.
     *
     * @param app    The application to verify against.
     * @param table  The routing table to verify against.
     * @param errors The list of errors found.
     * @return True if no errors where found.
     */
    public boolean verify(ApplicationSpec app, RoutingTableSpec table, List<String> errors) {
        if (verify) {
            String protocol = table.getProtocol();
            int numHops = hops.size();
            if (numHops == 0) {
                errors.add("Route '" + name + "' in routing table '" + protocol + "' has no hops.");
            } else {
                for (int i = 0; i < numHops; ++i) {
                    HopSpec.verify(app, table, null, null, hops.get(i), errors,
                                   "hop " + (i + 1) + " in route '" + name + "' in routing table '" + protocol + "'");
                }
            }
        }
        return errors.isEmpty();
    }

    /**
     * Appends the content of this to the given config string builder.
     *
     * @param cfg    The config to add to.
     * @param prefix The prefix to use for each add.
     */
    public void toConfig(StringBuilder cfg, String prefix) {
        cfg.append(prefix).append("name ").append(RoutingSpec.toConfigString(name)).append("\n");
        int numHops = hops.size();
        if (numHops > 0) {
            cfg.append(prefix).append("hop[").append(numHops).append("]\n");
            for (int i = 0; i < numHops; ++i) {
                cfg.append(prefix).append("hop[").append(i).append("] ");
                cfg.append(RoutingSpec.toConfigString(hops.get(i))).append("\n");
            }
        }
    }

    // Overrides Object.
    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        toConfig(ret, "");
        return ret.toString();
    }

    // Overrides Object.
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof RouteSpec)) {
            return false;
        }
        RouteSpec rhs = (RouteSpec)obj;
        if (!name.equals(rhs.name)) {
            return false;
        }
        if (!hops.equals(rhs.hops)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (hops != null ? hops.hashCode() : 0);
        result = 31 * result + (verify ? 1 : 0);
        return result;
    }
}