Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.incubator.net.virtual; |
| 18 | |
| 19 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.DefaultLink; |
| 23 | import org.onosproject.net.provider.ProviderId; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Default representation of a virtual link. |
| 32 | */ |
| 33 | public final class DefaultVirtualLink extends DefaultLink implements VirtualLink { |
| 34 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 35 | private static final String VIRTUAL = "virtualLink"; |
| 36 | public static final ProviderId PID = new ProviderId(VIRTUAL, VIRTUAL); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 37 | |
| 38 | private final NetworkId networkId; |
| 39 | private final TunnelId tunnelId; |
| 40 | |
| 41 | /** |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 42 | * Private constructor for a default virtual link. |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 43 | * |
| 44 | * @param networkId network identifier |
| 45 | * @param src source connection point |
| 46 | * @param dst destination connection point |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 47 | * @param state link state |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 48 | * @param tunnelId tunnel identifier |
| 49 | */ |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 50 | 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 53 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 94 | |
| 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 Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 112 | private State state; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 113 | |
| 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 Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 163 | * 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 174 | * 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 Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 183 | return new DefaultVirtualLink(networkId, src, dst, state, tunnelId); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 186 | } |