blob: 0daa8b58b0b6a9115f0109ce7094640f2ce4c35c [file] [log] [blame]
Brian Stanke0e5c94e2016-03-08 11:20:04 -05001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Brian Stanke0e5c94e2016-03-08 11:20:04 -05003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.incubator.net.virtual;
18
19import org.onosproject.incubator.net.tunnel.TunnelId;
20import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.DefaultLink;
23import org.onosproject.net.provider.ProviderId;
24
25import java.util.Objects;
26
27import static com.google.common.base.MoreObjects.toStringHelper;
Brian Stanke9a108972016-04-11 15:25:17 -040028import static com.google.common.base.Preconditions.checkNotNull;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050029
30/**
31 * Default representation of a virtual link.
32 */
33public final class DefaultVirtualLink extends DefaultLink implements VirtualLink {
34
Brian Stanke9a108972016-04-11 15:25:17 -040035 private static final String VIRTUAL = "virtualLink";
36 public static final ProviderId PID = new ProviderId(VIRTUAL, VIRTUAL);
Brian Stanke0e5c94e2016-03-08 11:20:04 -050037
38 private final NetworkId networkId;
39 private final TunnelId tunnelId;
40
41 /**
Brian Stanke9a108972016-04-11 15:25:17 -040042 * Private constructor for a default virtual link.
Brian Stanke0e5c94e2016-03-08 11:20:04 -050043 *
44 * @param networkId network identifier
45 * @param src source connection point
46 * @param dst destination connection point
Brian Stanke612cebf2016-05-02 10:21:33 -040047 * @param state link state
Brian Stanke0e5c94e2016-03-08 11:20:04 -050048 * @param tunnelId tunnel identifier
49 */
Brian Stanke612cebf2016-05-02 10:21:33 -040050 private DefaultVirtualLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst,
51 State state, TunnelId tunnelId) {
52 super(PID, src, dst, Type.VIRTUAL, state, DefaultAnnotations.builder().build());
Brian Stanke0e5c94e2016-03-08 11:20:04 -050053 this.networkId = networkId;
54 this.tunnelId = tunnelId;
55 }
56
57 @Override
58 public NetworkId networkId() {
59 return networkId;
60 }
61
62 /**
63 * Returns the tunnel identifier.
64 *
65 * @return tunnel identifier.
66 */
67 public TunnelId tunnelId() {
68 return tunnelId;
69 }
70
71 @Override
72 public int hashCode() {
73 return Objects.hash(networkId, tunnelId);
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81 if (obj instanceof DefaultVirtualLink) {
82 DefaultVirtualLink that = (DefaultVirtualLink) obj;
83 return super.equals(that) &&
84 Objects.equals(this.networkId, that.networkId) &&
85 Objects.equals(this.tunnelId, that.tunnelId);
86 }
87 return false;
88 }
89
90 @Override
91 public String toString() {
92 return toStringHelper(this).add("networkId", networkId).add("tunnelId", tunnelId).toString();
93 }
Brian Stanke9a108972016-04-11 15:25:17 -040094
95 /**
96 * Creates a new default virtual link builder.
97 *
98 * @return default virtual link builder
99 */
100 public static Builder builder() {
101 return new Builder();
102 }
103
104 /**
105 * Builder for DefaultVirtualLink objects.
106 */
107 public static final class Builder extends DefaultLink.Builder {
108 private NetworkId networkId;
109 private ConnectPoint src;
110 private ConnectPoint dst;
111 private TunnelId tunnelId;
Brian Stanke612cebf2016-05-02 10:21:33 -0400112 private State state;
Brian Stanke9a108972016-04-11 15:25:17 -0400113
114 private Builder() {
115 // Hide constructor
116 }
117
118 /**
119 * Sets the network identifier to be used by the builder.
120 *
121 * @param networkId network identifier
122 * @return self
123 */
124 public Builder networkId(NetworkId networkId) {
125 this.networkId = networkId;
126 return this;
127 }
128
129 /**
130 * Sets the source connect point to be used by the builder.
131 *
132 * @param src source connect point
133 * @return self
134 */
135 public Builder src(ConnectPoint src) {
136 this.src = src;
137 return this;
138 }
139
140 /**
141 * Sets the destination connect point to be used by the builder.
142 *
143 * @param dst new destination connect point
144 * @return self
145 */
146 public Builder dst(ConnectPoint dst) {
147 this.dst = dst;
148 return this;
149 }
150
151 /**
152 * Sets the tunnel identifier to be used by the builder.
153 *
154 * @param tunnelId tunnel identifier
155 * @return self
156 */
157 public Builder tunnelId(TunnelId tunnelId) {
158 this.tunnelId = tunnelId;
159 return this;
160 }
161
162 /**
Brian Stanke612cebf2016-05-02 10:21:33 -0400163 * Sets the link state to be used by the builder.
164 *
165 * @param state link state
166 * @return self
167 */
168 public Builder state(State state) {
169 this.state = state;
170 return this;
171 }
172
173 /**
Brian Stanke9a108972016-04-11 15:25:17 -0400174 * Builds a default virtual link object from the accumulated parameters.
175 *
176 * @return default virtual link object
177 */
178 public DefaultVirtualLink build() {
179 checkNotNull(src, "Source connect point cannot be null");
180 checkNotNull(dst, "Destination connect point cannot be null");
181 checkNotNull(networkId, "Network Id cannot be null");
182
Brian Stanke612cebf2016-05-02 10:21:33 -0400183 return new DefaultVirtualLink(networkId, src, dst, state, tunnelId);
Brian Stanke9a108972016-04-11 15:25:17 -0400184 }
185 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500186}