+ private IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
+ SessionContext sessionContext = sw.getSessionContext();
+ if (!sessionContext.isValid()) {
+ LOG.warn("IP address of the node {} cannot be obtained. Session is not valid.", sw.getNodeId());
+ return null;
+ }
+ InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
+ .getRemoteAddress();
+ if (remoteAddress == null) {
+ LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
+ return null;
+ }
+ return resolveIpAddress(remoteAddress.getAddress());
+ }
+
+ private static IpAddress resolveIpAddress(InetAddress address) {
+ String hostAddress = address.getHostAddress();
+ if (address instanceof Inet4Address) {
+ return new IpAddress(new Ipv4Address(hostAddress));
+ }
+ if (address instanceof Inet6Address) {
+ return new IpAddress(new Ipv6Address(hostAddress));
+ }
+ throw new IllegalArgumentException("Unsupported IP address type!");
+ }
+