Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.routing.config; |
| 17 | |
| 18 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpPrefix; |
| 25 | |
| 26 | /** |
| 27 | * Configuration details for an IP prefix entry. |
| 28 | */ |
| 29 | public class LocalIpPrefixEntry { |
| 30 | private final IpPrefix ipPrefix; |
| 31 | private final IpPrefixType type; |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 32 | private final IpAddress gatewayIpAddress; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Specifies the type of local IP prefix. |
| 36 | */ |
| 37 | public enum IpPrefixType { |
| 38 | /** |
| 39 | * Public IP prefixes should be exchanged by eBGP. |
| 40 | */ |
| 41 | PUBLIC, |
| 42 | /** |
| 43 | * Private IP prefixes should be used only locally and not exchanged |
| 44 | * by eBGP. |
| 45 | */ |
| 46 | PRIVATE, |
| 47 | /** |
| 48 | * For IP prefixes in blacklist. |
| 49 | */ |
| 50 | BLACK_LIST |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a new IP prefix entry. |
| 55 | * |
Thomas Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 56 | * @param ipPrefix an IP prefix as a String |
| 57 | * @param type an IP prefix type as an IpPrefixType |
| 58 | * @param gatewayIpAddress IP of the gateway |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 59 | */ |
| 60 | public LocalIpPrefixEntry(@JsonProperty("ipPrefix") String ipPrefix, |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 61 | @JsonProperty("type") IpPrefixType type, |
| 62 | @JsonProperty("gatewayIp") IpAddress |
Thomas Vachuska | e10f56b | 2015-04-15 18:20:08 -0700 | [diff] [blame] | 63 | gatewayIpAddress) { |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 64 | this.ipPrefix = IpPrefix.valueOf(ipPrefix); |
| 65 | this.type = type; |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 66 | this.gatewayIpAddress = gatewayIpAddress; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the IP prefix of the IP prefix entry. |
| 71 | * |
| 72 | * @return the IP prefix |
| 73 | */ |
| 74 | public IpPrefix ipPrefix() { |
| 75 | return ipPrefix; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the IP prefix type of the IP prefix entry. |
| 80 | * |
| 81 | * @return the IP prefix type |
| 82 | */ |
| 83 | public IpPrefixType ipPrefixType() { |
| 84 | return type; |
| 85 | } |
| 86 | |
| 87 | /** |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 88 | * Gets the gateway IP address of the IP prefix entry. |
| 89 | * |
| 90 | * @return the gateway IP address |
| 91 | */ |
| 92 | public IpAddress getGatewayIpAddress() { |
| 93 | return gatewayIpAddress; |
| 94 | } |
| 95 | |
| 96 | /** |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 97 | * Tests whether the IP version of this entry is IPv4. |
| 98 | * |
| 99 | * @return true if the IP version of this entry is IPv4, otherwise false. |
| 100 | */ |
| 101 | public boolean isIp4() { |
| 102 | return ipPrefix.isIp4(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Tests whether the IP version of this entry is IPv6. |
| 107 | * |
| 108 | * @return true if the IP version of this entry is IPv6, otherwise false. |
| 109 | */ |
| 110 | public boolean isIp6() { |
| 111 | return ipPrefix.isIp6(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
| 116 | return Objects.hash(ipPrefix, type); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean equals(Object obj) { |
| 121 | if (obj == this) { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | if (!(obj instanceof LocalIpPrefixEntry)) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | LocalIpPrefixEntry that = (LocalIpPrefixEntry) obj; |
| 130 | return Objects.equals(this.ipPrefix, that.ipPrefix) |
| 131 | && Objects.equals(this.type, that.type); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String toString() { |
| 136 | return MoreObjects.toStringHelper(getClass()) |
| 137 | .add("ipPrefix", ipPrefix) |
| 138 | .add("ipPrefixType", type) |
| 139 | .toString(); |
| 140 | } |
| 141 | } |