Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.simplefabric; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.intf.Interface; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.Host; |
| 27 | import org.onosproject.net.HostId; |
| 28 | import org.onosproject.net.EncapsulationType; |
| 29 | |
| 30 | import java.util.Collection; |
| 31 | import java.util.Objects; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Class stores a L2Network information. |
| 37 | */ |
| 38 | public final class L2Network { |
| 39 | |
| 40 | private String name; // also for network configuration |
| 41 | private Set<String> interfaceNames; // also for network configuration |
| 42 | private EncapsulationType encapsulation; // also for network configuration |
| 43 | private boolean l2Forward; // do l2Forward (default:true) or not |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 44 | private boolean l2Broadcast; // do l2Broadcast (default:true) or not |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 45 | |
| 46 | /* status variables */ |
| 47 | private Set<Interface> interfaces; // available interfaces from interfaceNames |
| 48 | private Set<HostId> hostIds; // available hosts from interfaces |
| 49 | private boolean dirty; |
| 50 | |
| 51 | /** |
| 52 | * Constructs a L2Network data for Config value. |
| 53 | * |
| 54 | * @param name the given name |
| 55 | * @param ifaceNames the interface names |
| 56 | * @param encapsulation the encapsulation type |
| 57 | * @param l2Forward flag for l2Forward intents to be installed or not |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 58 | * @param l2Broadcast flag for l2Broadcast intents to be installed or not |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 59 | */ |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 60 | L2Network(String name, Collection<String> ifaceNames, EncapsulationType encapsulation, |
| 61 | boolean l2Forward, boolean l2Broadcast) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 62 | this.name = name; |
| 63 | this.interfaceNames = Sets.newHashSet(); |
| 64 | this.interfaceNames.addAll(ifaceNames); |
| 65 | this.encapsulation = encapsulation; |
| 66 | this.l2Forward = (SimpleFabricService.ALLOW_ETH_ADDRESS_SELECTOR) ? l2Forward : false; |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 67 | this.l2Broadcast = (SimpleFabricService.ALLOW_ETH_ADDRESS_SELECTOR) ? l2Broadcast : false; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 68 | this.interfaces = Sets.newHashSet(); |
| 69 | this.hostIds = Sets.newHashSet(); |
| 70 | this.dirty = false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Constructs a L2Network data by given name and encapsulation type. |
| 75 | * |
| 76 | * @param name the given name |
| 77 | * @param encapsulation the encapsulation type |
| 78 | */ |
| 79 | private L2Network(String name, EncapsulationType encapsulation) { |
| 80 | this.name = name; |
| 81 | this.interfaceNames = Sets.newHashSet(); |
| 82 | this.encapsulation = encapsulation; |
| 83 | this.l2Forward = (SimpleFabricService.ALLOW_ETH_ADDRESS_SELECTOR) ? true : false; |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 84 | this.l2Broadcast = (SimpleFabricService.ALLOW_ETH_ADDRESS_SELECTOR) ? true : false; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 85 | this.interfaces = Sets.newHashSet(); |
| 86 | this.hostIds = Sets.newHashSet(); |
| 87 | this.dirty = false; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a L2Network data by given name. |
| 92 | * The encapsulation type of the L2Network will be NONE. |
| 93 | * |
| 94 | * @param name the given name |
| 95 | * @return the L2Network data |
| 96 | */ |
| 97 | public static L2Network of(String name) { |
| 98 | Objects.requireNonNull(name); |
| 99 | return new L2Network(name, EncapsulationType.NONE); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Creates a copy of L2Network data. |
| 104 | * |
| 105 | * @param l2Network the L2Network data |
| 106 | * @return the copy of the L2Network data |
| 107 | */ |
| 108 | public static L2Network of(L2Network l2Network) { |
| 109 | Objects.requireNonNull(l2Network); |
| 110 | L2Network l2NetworkCopy = new L2Network(l2Network.name(), l2Network.encapsulation()); |
| 111 | l2NetworkCopy.interfaceNames.addAll(l2Network.interfaceNames()); |
| 112 | l2NetworkCopy.l2Forward = (SimpleFabricService.ALLOW_ETH_ADDRESS_SELECTOR) ? l2Network.l2Forward() : false; |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 113 | l2NetworkCopy.l2Broadcast = (SimpleFabricService.ALLOW_ETH_ADDRESS_SELECTOR) ? l2Network.l2Broadcast() : false; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 114 | l2NetworkCopy.interfaces.addAll(l2Network.interfaces()); |
| 115 | l2NetworkCopy.hostIds.addAll(l2Network.hostIds()); |
| 116 | l2NetworkCopy.setDirty(l2Network.dirty()); |
| 117 | return l2NetworkCopy; |
| 118 | } |
| 119 | |
| 120 | // field queries |
| 121 | |
| 122 | /** |
| 123 | * Gets L2Network name. |
| 124 | * |
| 125 | * @return the name of L2Network |
| 126 | */ |
| 127 | public String name() { |
| 128 | return name; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Gets L2Network interfaceNames. |
| 133 | * |
| 134 | * @return the interfaceNames of L2Network |
| 135 | */ |
| 136 | public Set<String> interfaceNames() { |
| 137 | return ImmutableSet.copyOf(interfaceNames); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Gets L2Network encapsulation type. |
| 142 | * |
| 143 | * @return the encapsulation type of L2Network |
| 144 | */ |
| 145 | public EncapsulationType encapsulation() { |
| 146 | return encapsulation; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Gets L2Network l2Forward flag. |
| 151 | * |
| 152 | * @return the l2Forward flag of L2Network |
| 153 | */ |
| 154 | public boolean l2Forward() { |
| 155 | return l2Forward; |
| 156 | } |
| 157 | |
| 158 | /** |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 159 | * Gets L2Network l2Broadcast flag. |
| 160 | * |
| 161 | * @return the l2Broadcast flag of L2Network |
| 162 | */ |
| 163 | public boolean l2Broadcast() { |
| 164 | return l2Broadcast; |
| 165 | } |
| 166 | |
| 167 | /** |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 168 | * Gets L2Network interfaces. |
| 169 | * |
| 170 | * @return the interfaces of L2Network |
| 171 | */ |
| 172 | public Set<Interface> interfaces() { |
| 173 | return ImmutableSet.copyOf(interfaces); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets L2Network hosts. |
| 178 | * |
| 179 | * @return the hosts of L2Network |
| 180 | */ |
| 181 | public Set<HostId> hostIds() { |
| 182 | return ImmutableSet.copyOf(hostIds); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Gets L2Network dirty flag. |
| 187 | * |
| 188 | * @return the dirty flag of L2Network |
| 189 | */ |
| 190 | public boolean dirty() { |
| 191 | return dirty; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Checks if the interface is of L2Network. |
| 196 | * |
| 197 | * @param iface the interface to be checked |
| 198 | * @return true if L2Network contains the interface |
| 199 | */ |
| 200 | public boolean contains(Interface iface) { |
| 201 | return interfaces.contains(iface); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Checks if the ConnectPoint and Vlan is of L2Network. |
| 206 | * |
| 207 | * @param port the ConnectPoint to be checked |
| 208 | * @param vlanId the VlanId of the ConnectPoint to be checked |
| 209 | * @return true if L2Network contains the interface of the ConnnectPoint and VlanId |
| 210 | */ |
| 211 | public boolean contains(ConnectPoint port, VlanId vlanId) { |
| 212 | for (Interface iface : interfaces) { |
| 213 | if (iface.connectPoint().equals(port) && iface.vlan().equals(vlanId)) { |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Checks if the DeviceId is of L2Network. |
| 222 | * |
| 223 | * @param deviceId the DeviceId to be checked |
| 224 | * @return true if L2Network contains any interface of the DeviceId |
| 225 | */ |
| 226 | public boolean contains(DeviceId deviceId) { |
| 227 | for (Interface iface : interfaces) { |
| 228 | if (iface.connectPoint().deviceId().equals(deviceId)) { |
| 229 | return true; |
| 230 | } |
| 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Adds interface to L2Network. |
| 237 | * |
| 238 | * @param iface the Interface to be added |
| 239 | */ |
| 240 | public void addInterface(Interface iface) { |
| 241 | Objects.requireNonNull(iface); |
| 242 | if (interfaces.add(iface)) { |
| 243 | setDirty(true); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Adds host to L2Network. |
| 249 | * |
| 250 | * @param host the Host to be added |
| 251 | */ |
| 252 | public void addHost(Host host) { |
| 253 | Objects.requireNonNull(host); |
| 254 | if (hostIds.add(host.id())) { |
| 255 | setDirty(true); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Sets L2Network dirty flag. |
| 261 | * |
| 262 | * @param newDirty the dirty flag to be set |
| 263 | */ |
| 264 | public void setDirty(boolean newDirty) { |
| 265 | dirty = newDirty; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public String toString() { |
| 270 | return MoreObjects.toStringHelper(getClass()) |
| 271 | .add("name", name) |
| 272 | .add("interfaceNames", interfaceNames) |
| 273 | .add("encapsulation", encapsulation) |
| 274 | .add("l2Forward", l2Forward) |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 275 | .add("l2Broadcast", l2Broadcast) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 276 | .add("interfaces", interfaces) |
| 277 | .add("hostIds", hostIds) |
| 278 | .add("dirty", dirty) |
| 279 | .toString(); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean equals(Object obj) { |
| 284 | if (obj == this) { |
| 285 | return true; |
| 286 | } |
| 287 | if (!(obj instanceof L2Network)) { |
| 288 | return false; |
| 289 | } |
| 290 | L2Network other = (L2Network) obj; |
| 291 | return Objects.equals(other.name, this.name) |
| 292 | && Objects.equals(other.interfaceNames, this.interfaceNames) |
| 293 | && Objects.equals(other.encapsulation, this.encapsulation) |
| 294 | && Objects.equals(other.l2Forward, this.l2Forward) |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 295 | && Objects.equals(other.l2Broadcast, this.l2Broadcast) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 296 | && Objects.equals(other.interfaces, this.interfaces) |
| 297 | && Objects.equals(other.hostIds, this.hostIds); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public int hashCode() { |
Lee Yongjae | 6dc7e4f | 2017-12-06 16:17:51 +0900 | [diff] [blame] | 302 | return Objects.hash(name, interfaces, encapsulation, l2Forward, l2Broadcast); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 303 | } |
| 304 | } |