blob: d8e24c44687fb6ab463e09be13c05ae74c6fe861 [file] [log] [blame]
Jian Li0dab5962016-12-15 03:44:28 +09001/*
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 */
16package org.onosproject.lisp.ctl;
17
18import io.netty.channel.Channel;
19import org.onlab.packet.IpAddress;
20import org.onosproject.lisp.msg.protocols.LispMessage;
21import org.onosproject.net.Device;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.net.InetSocketAddress;
26import java.net.SocketAddress;
27
28/**
29 * An abstract representation of a LISP router.
30 * This class can be extended by others to serve as a base for their vendor
31 * specific representation of a router.
32 */
33public abstract class AbstractLispRouter implements LispRouter {
34
35 private final Logger log = LoggerFactory.getLogger(getClass());
36
37 private static final String DROP_MESSAGE_WARN =
38 "Drop message {} destined to router {} as channel is closed.";
39
40 private Channel channel;
41 private String channelId;
42
43 private boolean connected;
44 private boolean subscribed;
45 private LispRouterId routerId;
46 private LispRouterAgent agent;
47
48 /**
49 * A default constructor.
50 *
51 * @param routerId router identifier
52 */
53 AbstractLispRouter(LispRouterId routerId) {
54 this.routerId = routerId;
55 }
56
57 @Override
58 public final String channelId() {
59 return channelId;
60 }
61
62 @Override
63 public final IpAddress routerId() {
64 return routerId.id();
65 }
66
67 @Override
68 public final String stringId() {
69 return routerId.toString();
70 }
71
72 @Override
73 public final void setChannel(Channel channel) {
74 this.channel = channel;
75 final SocketAddress address = channel.remoteAddress();
76 if (address instanceof InetSocketAddress) {
77 final InetSocketAddress inetAddress = (InetSocketAddress) address;
78 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
79 if (ipAddress.isIp4()) {
80 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
81 } else {
82 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
83 }
84 }
85 }
86
87 @Override
88 public final void setConnected(boolean connected) {
89 this.connected = connected;
90 }
91
92 @Override
93 public final boolean isConnected() {
94 return connected;
95 }
96
97
98 @Override
99 public final boolean isSubscribed() {
100 return subscribed;
101 }
102
103 @Override
104 public final void setSubscribed(boolean subscribed) {
105 this.subscribed = subscribed;
106 }
107
108 @Override
109 public final void setAgent(LispRouterAgent agent) {
110 // we never assign the agent more than one time
111 if (this.agent == null) {
112 this.agent = agent;
113 }
114 }
115
116 @Override
117 public final Device.Type deviceType() {
118 return Device.Type.ROUTER;
119 }
120
121 @Override
122 public void sendMessage(LispMessage message) {
123 if (channel.isOpen()) {
124 // TODO: need to consider to use writeAndFlush if possible
125 channel.write(message);
126 agent.processDownstreamMessage(routerId, message);
127 } else {
128 log.warn(DROP_MESSAGE_WARN, message, routerId);
129 }
130 }
131
132 @Override
133 public void handleMessage(LispMessage message) {
134 this.agent.processUpstreamMessage(routerId, message);
135 }
136
137 @Override
138 public final boolean connectRouter() {
139 return this.agent.addConnectedRouter(routerId, this);
140 }
141
142 @Override
143 public final void disconnectRouter() {
144 setConnected(false);
145 channel.close();
146 }
147
148 @Override
149 public String toString() {
150
151 StringBuilder sb = new StringBuilder();
152 sb.append(this.getClass().getName());
153
154 String address = (channel != null) ? channel.remoteAddress().toString() : "?";
155 String routerId = (stringId() != null) ? stringId() : "?";
156
157 sb.append(" [");
158 sb.append(address);
159 sb.append(" routerId[");
160 sb.append(routerId);
161 sb.append("]]");
162
163 return sb.toString();
164 }
165}