blob: b7fec47cd9119165c4e4e2858ccf56dae8356ac3 [file] [log] [blame]
Jian Lie2d87512018-08-23 17:33:05 +09001/*
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 Lida0b4852018-08-29 20:40:44 +090016package org.onosproject.simplefabric.impl;
Jian Lie2d87512018-08-23 17:33:05 +090017
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22import org.onosproject.net.EncapsulationType;
Jian Lida0b4852018-08-29 20:40:44 +090023import org.onosproject.simplefabric.api.FabricSubnet;
Jian Lie2d87512018-08-23 17:33:05 +090024
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkArgument;
28
29/**
30 * Configuration details for an ip subnet entry.
31 */
32public 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 Lida0b4852018-08-29 20:40:44 +090040 private final String networkName;
Jian Lie2d87512018-08-23 17:33:05 +090041
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 Lida0b4852018-08-29 20:40:44 +090049 * @param networkName network name
Jian Lie2d87512018-08-23 17:33:05 +090050 */
51 private DefaultFabricSubnet(IpPrefix prefix, IpAddress gatewayIp,
52 MacAddress gatewayMac, EncapsulationType encapsulation,
Jian Lida0b4852018-08-29 20:40:44 +090053 String networkName) {
Jian Lie2d87512018-08-23 17:33:05 +090054 this.prefix = prefix;
55 this.gatewayIp = gatewayIp;
56 this.gatewayMac = gatewayMac;
57 this.encapsulation = encapsulation;
Jian Lida0b4852018-08-29 20:40:44 +090058 this.networkName = networkName;
Jian Lie2d87512018-08-23 17:33:05 +090059 }
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 Lida0b4852018-08-29 20:40:44 +090082 public String networkName() {
83 return networkName;
Jian Lie2d87512018-08-23 17:33:05 +090084 }
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 Lida0b4852018-08-29 20:40:44 +090098 return Objects.hash(prefix, gatewayIp, gatewayMac, encapsulation, networkName);
Jian Lie2d87512018-08-23 17:33:05 +090099 }
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 Lida0b4852018-08-29 20:40:44 +0900111 && 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 Lie2d87512018-08-23 17:33:05 +0900115 }
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 Lida0b4852018-08-29 20:40:44 +0900124 .add("networkName", networkName)
Jian Lie2d87512018-08-23 17:33:05 +0900125 .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 Lida0b4852018-08-29 20:40:44 +0900141 private IpPrefix prefix;
Jian Lie2d87512018-08-23 17:33:05 +0900142 private IpAddress gatewayIp;
143 private MacAddress gatewayMac;
144 private EncapsulationType encapsulation;
Jian Lida0b4852018-08-29 20:40:44 +0900145 private String networkName;
Jian Lie2d87512018-08-23 17:33:05 +0900146
147 private DefaultSubnetBuilder() {
148 }
149
150 @Override
Jian Lida0b4852018-08-29 20:40:44 +0900151 public Builder prefix(IpPrefix prefix) {
152 this.prefix = prefix;
Jian Lie2d87512018-08-23 17:33:05 +0900153 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 Lida0b4852018-08-29 20:40:44 +0900175 public Builder networkName(String networkName) {
176 this.networkName = networkName;
Jian Lie2d87512018-08-23 17:33:05 +0900177 return this;
178 }
179
180 @Override
181 public FabricSubnet build() {
Jian Lida0b4852018-08-29 20:40:44 +0900182 checkArgument(prefix != null, NOT_NULL_MSG, "prefix");
Jian Lie2d87512018-08-23 17:33:05 +0900183 checkArgument(gatewayIp != null, NOT_NULL_MSG, "gatewayIp");
184 checkArgument(gatewayMac != null, NOT_NULL_MSG, "gatewayMac");
Jian Lida0b4852018-08-29 20:40:44 +0900185 checkArgument(networkName != null, NOT_NULL_MSG, "name");
Jian Lie2d87512018-08-23 17:33:05 +0900186
187 if (this.encapsulation == null) {
188 encapsulation = EncapsulationType.NONE;
189 }
190
Jian Lida0b4852018-08-29 20:40:44 +0900191 return new DefaultFabricSubnet(prefix, gatewayIp, gatewayMac,
192 encapsulation, networkName);
Jian Lie2d87512018-08-23 17:33:05 +0900193 }
194 }
Jian Lida0b4852018-08-29 20:40:44 +0900195}