blob: c21a516f599cb39caee6891646fab68ca35ed17d [file] [log] [blame]
wei wei89ddc322015-03-22 16:29:04 -05001package org.onosproject.net.tunnel;
2
3import static com.google.common.base.MoreObjects.toStringHelper;
4import static com.google.common.base.Preconditions.checkState;
5
6import java.util.Objects;
7
8import org.onosproject.core.IdGenerator;
9import org.onosproject.net.AbstractModel;
10import org.onosproject.net.Annotations;
11import org.onosproject.net.provider.ProviderId;
12import org.onosproject.net.resource.Bandwidth;
13
14/**
15 * Default tunnel model implementation.
16 */
17public final class DefaultTunnel extends AbstractModel implements Tunnel {
18 private final TunnelId id;
19 private final Label src;
20 private final Label dst;
21 private final Type type;
22 private final State state;
23 private final boolean isDurable;
24 private final boolean isBidirectional;
25 private final Bandwidth bandwidth;
26
27 /**
28 * Constructs an tunnel using the builder pattern.
29 *
30 * @param providerId provider identity, can be null if comes from the NB
31 * @param builder tunnelBuilder
32 * @param annotations optional key/value annotations
33 * @return
34 */
35 private DefaultTunnel(ProviderId providerId, TunnelBuilder builder, Annotations... annotations) {
36 super(providerId, annotations);
37 this.id = builder.id;
38 this.src = builder.src;
39 this.dst = builder.dst;
40 this.type = builder.type;
41 this.state = builder.state;
42 this.isDurable = builder.isDurable;
43 this.isBidirectional = builder.isBidirectional;
44 this.bandwidth = builder.bandwidth;
45 }
46
47 @Override
48 public TunnelId id() {
49 return id;
50 }
51
52 @Override
53 public Label src() {
54 return src;
55 }
56
57 @Override
58 public Label dst() {
59 return dst;
60 }
61
62 @Override
63 public Type type() {
64 return type;
65 }
66
67 @Override
68 public State state() {
69 return state;
70 }
71
72 @Override
73 public boolean isDurable() {
74 return isDurable;
75 }
76
77 @Override
78 public boolean isBidirectional() {
79 return isBidirectional;
80 }
81
82 @Override
83 public Bandwidth bandwidth() {
84 return bandwidth;
85 }
86
87 @Override
88 public int hashCode() {
89 return Objects.hash(id);
90 }
91
92 /**
93 * {@inheritDoc}
94 * Note that only TunnelId is considered on equality check.
95 */
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
101 if (obj instanceof DefaultTunnel) {
102 final DefaultTunnel other = (DefaultTunnel) obj;
103 return Objects.equals(this.id, other.id);
104 }
105 return false;
106 }
107
108 @Override
109 public String toString() {
110 return toStringHelper(this)
111 .add("tunnelId", id)
112 .add("src", src)
113 .add("dst", dst)
114 .add("type", type)
115 .add("state", state)
116 .add("durable", isDurable)
117 .add("isBidirectional", isBidirectional)
118 .add("bandwidth", bandwidth)
119 .toString();
120 }
121
122 public static class TunnelBuilder {
123 private TunnelId id = null;
124 private Label src = null;
125 private Label dst = null;
126 private Type type = null;
127 private State state = null;
128 private boolean isDurable = false;
129 private boolean isBidirectional = false;
130 private Bandwidth bandwidth = null;
131
132 private static IdGenerator idGenerator;
133
134 public TunnelBuilder labelSrcDst(Label src, Label dst) {
135 this.src = src;
136 this.dst = dst;
137 return this;
138 }
139
140 public TunnelBuilder state(State state) {
141 this.state = state;
142 return this;
143 }
144
145 public TunnelBuilder isDurable(boolean isDurable) {
146 this.isDurable = isDurable;
147 return this;
148 }
149
150 public TunnelBuilder isBidirectional(boolean isBidirectional) {
151 this.isBidirectional = isBidirectional;
152 return this;
153 }
154
155 public TunnelBuilder bandwidth(Bandwidth bandwidth) {
156 this.bandwidth = bandwidth;
157 return this;
158 }
159
160 public DefaultTunnel build(ProviderId providerId, Annotations... annotations) {
161 checkState(idGenerator != null, "Id generator is not bound.");
162 this.id = TunnelId.valueOf(idGenerator.getNewId());
163 return new DefaultTunnel(providerId, this, annotations);
164 }
165
166 }
167
168}