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