Jian Li | 0dab596 | 2016-12-15 03:44:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.lisp.ctl; |
| 17 | |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Jian Li | 0dab596 | 2016-12-15 03:44:28 +0900 | [diff] [blame] | 19 | import io.netty.channel.Channel; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.lisp.msg.protocols.LispMessage; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import java.net.InetSocketAddress; |
| 27 | import java.net.SocketAddress; |
| 28 | |
| 29 | /** |
| 30 | * An abstract representation of a LISP router. |
| 31 | * This class can be extended by others to serve as a base for their vendor |
| 32 | * specific representation of a router. |
| 33 | */ |
| 34 | public abstract class AbstractLispRouter implements LispRouter { |
| 35 | |
| 36 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 37 | |
| 38 | private static final String DROP_MESSAGE_WARN = |
| 39 | "Drop message {} destined to router {} as channel is closed."; |
| 40 | |
| 41 | private Channel channel; |
| 42 | private String channelId; |
| 43 | |
| 44 | private boolean connected; |
| 45 | private boolean subscribed; |
| 46 | private LispRouterId routerId; |
| 47 | private LispRouterAgent agent; |
| 48 | |
| 49 | /** |
| 50 | * A default constructor. |
| 51 | * |
| 52 | * @param routerId router identifier |
| 53 | */ |
| 54 | AbstractLispRouter(LispRouterId routerId) { |
| 55 | this.routerId = routerId; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public final String channelId() { |
| 60 | return channelId; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public final IpAddress routerId() { |
| 65 | return routerId.id(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public final String stringId() { |
| 70 | return routerId.toString(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public final void setChannel(Channel channel) { |
| 75 | this.channel = channel; |
| 76 | final SocketAddress address = channel.remoteAddress(); |
| 77 | if (address instanceof InetSocketAddress) { |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 78 | channelId = genChannelId((InetSocketAddress) address); |
Jian Li | 0dab596 | 2016-12-15 03:44:28 +0900 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public final void setConnected(boolean connected) { |
| 84 | this.connected = connected; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public final boolean isConnected() { |
| 89 | return connected; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | @Override |
| 94 | public final boolean isSubscribed() { |
| 95 | return subscribed; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public final void setSubscribed(boolean subscribed) { |
| 100 | this.subscribed = subscribed; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public final void setAgent(LispRouterAgent agent) { |
| 105 | // we never assign the agent more than one time |
| 106 | if (this.agent == null) { |
| 107 | this.agent = agent; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public final Device.Type deviceType() { |
| 113 | return Device.Type.ROUTER; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void sendMessage(LispMessage message) { |
| 118 | if (channel.isOpen()) { |
| 119 | // TODO: need to consider to use writeAndFlush if possible |
| 120 | channel.write(message); |
| 121 | agent.processDownstreamMessage(routerId, message); |
| 122 | } else { |
| 123 | log.warn(DROP_MESSAGE_WARN, message, routerId); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void handleMessage(LispMessage message) { |
| 129 | this.agent.processUpstreamMessage(routerId, message); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public final boolean connectRouter() { |
| 134 | return this.agent.addConnectedRouter(routerId, this); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public final void disconnectRouter() { |
| 139 | setConnected(false); |
| 140 | channel.close(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
| 145 | |
| 146 | StringBuilder sb = new StringBuilder(); |
| 147 | sb.append(this.getClass().getName()); |
| 148 | |
| 149 | String address = (channel != null) ? channel.remoteAddress().toString() : "?"; |
| 150 | String routerId = (stringId() != null) ? stringId() : "?"; |
| 151 | |
| 152 | sb.append(" ["); |
| 153 | sb.append(address); |
| 154 | sb.append(" routerId["); |
| 155 | sb.append(routerId); |
| 156 | sb.append("]]"); |
| 157 | |
| 158 | return sb.toString(); |
| 159 | } |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 160 | |
| 161 | @Override |
| 162 | public boolean equals(Object o) { |
| 163 | if (this == o) { |
| 164 | return true; |
| 165 | } |
| 166 | if (o == null || getClass() != o.getClass()) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | AbstractLispRouter that = (AbstractLispRouter) o; |
| 171 | return Objects.equal(channel, that.channel) && |
| 172 | Objects.equal(channelId, that.channelId) && |
| 173 | Objects.equal(connected, that.connected) && |
| 174 | Objects.equal(subscribed, that.subscribed) && |
| 175 | Objects.equal(routerId, that.routerId) && |
| 176 | Objects.equal(agent, that.agent); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public int hashCode() { |
| 181 | return Objects.hashCode(channel, channelId, connected, |
| 182 | subscribed, routerId, agent); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Generates a string format of channel ID from the given InetSocketAddress. |
| 187 | * |
| 188 | * @param inetAddress InetAddress object |
| 189 | * @return string format of channel ID |
| 190 | */ |
| 191 | private String genChannelId(InetSocketAddress inetAddress) { |
| 192 | StringBuilder sb = new StringBuilder(); |
| 193 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 194 | if (ipAddress.isIp4()) { |
| 195 | sb.append(ipAddress.toString()); |
| 196 | sb.append(":"); |
| 197 | sb.append(inetAddress.getPort()); |
| 198 | } else { |
| 199 | sb.append("["); |
| 200 | sb.append(ipAddress.toString()); |
| 201 | sb.append("]:"); |
| 202 | sb.append(inetAddress.getPort()); |
| 203 | } |
| 204 | return sb.toString(); |
| 205 | } |
Jian Li | 0dab596 | 2016-12-15 03:44:28 +0900 | [diff] [blame] | 206 | } |