Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Daniel Park | a7d6e9f | 2016-01-18 17:54:14 +0900 | [diff] [blame] | 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.openstacknode; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | |
| 24 | import java.util.Comparator; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Representation of a compute/gateway node for OpenstackSwitching/Routing service. |
| 31 | */ |
| 32 | public final class OpenstackNode { |
| 33 | |
| 34 | private final String hostName; |
| 35 | private final IpAddress ovsdbIp; |
| 36 | private final TpPort ovsdbPort; |
| 37 | private final DeviceId bridgeId; |
| 38 | private final OpenstackNodeService.OpenstackNodeType openstackNodeType; |
| 39 | private final String gatewayExternalInterfaceName; |
| 40 | private final MacAddress gatewayExternalInterfaceMac; |
| 41 | private static final String OVSDB = "ovsdb:"; |
| 42 | |
| 43 | |
| 44 | public static final Comparator<OpenstackNode> OPENSTACK_NODE_COMPARATOR = |
| 45 | (node1, node2) -> node1.hostName().compareTo(node2.hostName()); |
| 46 | |
| 47 | /** |
| 48 | * Creates a new node. |
| 49 | * |
| 50 | * @param hostName hostName |
| 51 | * @param ovsdbIp OVSDB server IP address |
| 52 | * @param ovsdbPort OVSDB server port number |
| 53 | * @param bridgeId integration bridge identifier |
| 54 | * @param openstackNodeType openstack node type |
| 55 | * @param gatewayExternalInterfaceName gatewayExternalInterfaceName |
| 56 | * @param gatewayExternalInterfaceMac gatewayExternalInterfaceMac |
| 57 | */ |
| 58 | public OpenstackNode(String hostName, IpAddress ovsdbIp, TpPort ovsdbPort, DeviceId bridgeId, |
| 59 | OpenstackNodeService.OpenstackNodeType openstackNodeType, |
| 60 | String gatewayExternalInterfaceName, |
| 61 | MacAddress gatewayExternalInterfaceMac) { |
| 62 | this.hostName = checkNotNull(hostName, "hostName cannot be null"); |
| 63 | this.ovsdbIp = checkNotNull(ovsdbIp, "ovsdbIp cannot be null"); |
| 64 | this.ovsdbPort = checkNotNull(ovsdbPort, "ovsdbPort cannot be null"); |
| 65 | this.bridgeId = checkNotNull(bridgeId, "bridgeId cannot be null"); |
| 66 | this.openstackNodeType = checkNotNull(openstackNodeType, "openstackNodeType cannot be null"); |
| 67 | this.gatewayExternalInterfaceName = gatewayExternalInterfaceName; |
| 68 | this.gatewayExternalInterfaceMac = gatewayExternalInterfaceMac; |
| 69 | |
| 70 | if (openstackNodeType == OpenstackNodeService.OpenstackNodeType.GATEWAYNODE) { |
| 71 | checkNotNull(gatewayExternalInterfaceName, "gatewayExternalInterfaceName cannot be null"); |
| 72 | checkNotNull(gatewayExternalInterfaceMac, "gatewayExternalInterfaceMac cannot be null"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the OVSDB server IP address. |
| 78 | * |
| 79 | * @return ip address |
| 80 | */ |
| 81 | public IpAddress ovsdbIp() { |
| 82 | return this.ovsdbIp; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the OVSDB server port number. |
| 87 | * |
| 88 | * @return port number |
| 89 | */ |
| 90 | public TpPort ovsdbPort() { |
| 91 | return this.ovsdbPort; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the hostName. |
| 96 | * |
| 97 | * @return hostName |
| 98 | */ |
| 99 | public String hostName() { |
| 100 | return this.hostName; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the identifier of the integration bridge. |
| 105 | * |
| 106 | * @return device id |
| 107 | */ |
| 108 | public DeviceId intBrId() { |
| 109 | return this.bridgeId; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the identifier of the OVSDB device. |
| 114 | * |
| 115 | * @return device id |
| 116 | */ |
| 117 | public DeviceId ovsdbId() { |
| 118 | return DeviceId.deviceId(OVSDB.concat(this.ovsdbIp.toString())); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the openstack node type. |
| 123 | * |
| 124 | * @return openstack node type |
| 125 | */ |
| 126 | public OpenstackNodeService.OpenstackNodeType openstackNodeType() { |
| 127 | return this.openstackNodeType; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the gatewayExternalInterfaceName. |
| 132 | * |
| 133 | * @return gatewayExternalInterfaceName |
| 134 | */ |
| 135 | public String gatewayExternalInterfaceName() { |
| 136 | return this.gatewayExternalInterfaceName; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns the gatewayExternalInterfaceMac. |
| 141 | * |
| 142 | * @return gatewayExternalInterfaceMac |
| 143 | */ |
| 144 | public MacAddress gatewayExternalInterfaceMac() { |
| 145 | return this.gatewayExternalInterfaceMac; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean equals(Object obj) { |
| 150 | if (this == obj) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | if (obj instanceof OpenstackNode) { |
| 155 | OpenstackNode that = (OpenstackNode) obj; |
| 156 | |
| 157 | if (Objects.equals(hostName, that.hostName) && |
| 158 | Objects.equals(ovsdbIp, that.ovsdbIp) && |
| 159 | Objects.equals(ovsdbPort, that.ovsdbPort) && |
| 160 | Objects.equals(bridgeId, that.bridgeId) && |
| 161 | Objects.equals(openstackNodeType, that.openstackNodeType)) { |
| 162 | return true; |
| 163 | } |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public int hashCode() { |
| 170 | return Objects.hash(hostName, ovsdbIp, ovsdbPort, bridgeId, openstackNodeType); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public String toString() { |
| 175 | if (openstackNodeType == OpenstackNodeService.OpenstackNodeType.COMPUTENODE) { |
| 176 | return MoreObjects.toStringHelper(getClass()) |
| 177 | .add("host", hostName) |
| 178 | .add("ip", ovsdbIp) |
| 179 | .add("port", ovsdbPort) |
| 180 | .add("bridgeId", bridgeId) |
| 181 | .add("openstacknodetype", openstackNodeType) |
| 182 | .toString(); |
| 183 | } else { |
| 184 | return MoreObjects.toStringHelper(getClass()) |
| 185 | .add("host", hostName) |
| 186 | .add("ip", ovsdbIp) |
| 187 | .add("port", ovsdbPort) |
| 188 | .add("bridgeId", bridgeId) |
| 189 | .add("openstacknodetype", openstackNodeType) |
| 190 | .add("gatewayExternalInterfaceName", gatewayExternalInterfaceName) |
| 191 | .add("gatewayExternalInterfaceMac", gatewayExternalInterfaceMac) |
| 192 | .toString(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |