Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 16 | package org.onosproject.simplefabric.impl; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.net.EncapsulationType; |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 23 | import org.onosproject.simplefabric.api.FabricSubnet; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | |
| 29 | /** |
| 30 | * Configuration details for an ip subnet entry. |
| 31 | */ |
| 32 | public final class DefaultFabricSubnet implements FabricSubnet { |
| 33 | |
| 34 | private static final String NOT_NULL_MSG = "FabricSubnet % cannot be null"; |
| 35 | |
| 36 | private final IpPrefix prefix; |
| 37 | private final IpAddress gatewayIp; |
| 38 | private final MacAddress gatewayMac; |
| 39 | private EncapsulationType encapsulation; |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 40 | private final String networkName; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Creates a new subnet entry. |
| 44 | * |
| 45 | * @param prefix an ip subnet |
| 46 | * @param gatewayIp IP of the virtual gateway |
| 47 | * @param gatewayMac MacAddress of the virtual gateway |
| 48 | * @param encapsulation encapsulation type |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 49 | * @param networkName network name |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 50 | */ |
| 51 | private DefaultFabricSubnet(IpPrefix prefix, IpAddress gatewayIp, |
| 52 | MacAddress gatewayMac, EncapsulationType encapsulation, |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 53 | String networkName) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 54 | this.prefix = prefix; |
| 55 | this.gatewayIp = gatewayIp; |
| 56 | this.gatewayMac = gatewayMac; |
| 57 | this.encapsulation = encapsulation; |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 58 | this.networkName = networkName; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public IpPrefix prefix() { |
| 63 | return prefix; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public IpAddress gatewayIp() { |
| 68 | return gatewayIp; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public MacAddress gatewayMac() { |
| 73 | return gatewayMac; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public EncapsulationType encapsulation() { |
| 78 | return encapsulation; |
| 79 | } |
| 80 | |
| 81 | @Override |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 82 | public String networkName() { |
| 83 | return networkName; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean isIp4() { |
| 88 | return prefix.isIp4(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean isIp6() { |
| 93 | return prefix.isIp6(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 98 | return Objects.hash(prefix, gatewayIp, gatewayMac, encapsulation, networkName); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(Object obj) { |
| 103 | if (obj == this) { |
| 104 | return true; |
| 105 | } |
| 106 | if (!(obj instanceof DefaultFabricSubnet)) { |
| 107 | return false; |
| 108 | } |
| 109 | DefaultFabricSubnet that = (DefaultFabricSubnet) obj; |
| 110 | return Objects.equals(this.prefix, that.prefix) |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 111 | && Objects.equals(this.gatewayIp, that.gatewayIp) |
| 112 | && Objects.equals(this.gatewayMac, that.gatewayMac) |
| 113 | && Objects.equals(this.encapsulation, that.encapsulation) |
| 114 | && Objects.equals(this.networkName, that.networkName); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | return MoreObjects.toStringHelper(getClass()) |
| 120 | .add("prefix", prefix) |
| 121 | .add("gatewayIp", gatewayIp) |
| 122 | .add("gatewayMac", gatewayMac) |
| 123 | .add("encapsulation", encapsulation) |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 124 | .add("networkName", networkName) |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 125 | .toString(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns new builder instance. |
| 130 | * |
| 131 | * @return fabric IP subnet builder |
| 132 | */ |
| 133 | public static DefaultSubnetBuilder builder() { |
| 134 | return new DefaultSubnetBuilder(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * A builder class for Ip Subnet. |
| 139 | */ |
| 140 | public static final class DefaultSubnetBuilder implements Builder { |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 141 | private IpPrefix prefix; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 142 | private IpAddress gatewayIp; |
| 143 | private MacAddress gatewayMac; |
| 144 | private EncapsulationType encapsulation; |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 145 | private String networkName; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 146 | |
| 147 | private DefaultSubnetBuilder() { |
| 148 | } |
| 149 | |
| 150 | @Override |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 151 | public Builder prefix(IpPrefix prefix) { |
| 152 | this.prefix = prefix; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 153 | return this; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Builder gatewayIp(IpAddress gatewayIp) { |
| 158 | this.gatewayIp = gatewayIp; |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Builder gatewayMac(MacAddress gatewayMac) { |
| 164 | this.gatewayMac = gatewayMac; |
| 165 | return this; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Builder encapsulation(EncapsulationType encapsulation) { |
| 170 | this.encapsulation = encapsulation; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | @Override |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 175 | public Builder networkName(String networkName) { |
| 176 | this.networkName = networkName; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public FabricSubnet build() { |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 182 | checkArgument(prefix != null, NOT_NULL_MSG, "prefix"); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 183 | checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp"); |
| 184 | checkArgument(gatewayMac != null, NOT_NULL_MSG, "gatewayMac"); |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 185 | checkArgument(networkName != null, NOT_NULL_MSG, "name"); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 186 | |
| 187 | if (this.encapsulation == null) { |
| 188 | encapsulation = EncapsulationType.NONE; |
| 189 | } |
| 190 | |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 191 | return new DefaultFabricSubnet(prefix, gatewayIp, gatewayMac, |
| 192 | encapsulation, networkName); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 193 | } |
| 194 | } |
Jian Li | da0b485 | 2018-08-29 20:40:44 +0900 | [diff] [blame] | 195 | } |