Centralize NetconfNode/InetSocketAddress conversion
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / spi / NetconfNodeUtils.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.topology.spi;
9
10 import java.net.InetSocketAddress;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
18
19 /**
20  * Utility methods to work with {@link NetconfNode} information.
21  */
22 public final class NetconfNodeUtils {
23     private NetconfNodeUtils() {
24         // Hidden on purpose
25     }
26
27     public static @NonNull InetSocketAddress toInetSocketAddress(final NetconfNode node) {
28         final Host host = node.requireHost();
29         final int port = node.requirePort().getValue().toJava();
30         final IpAddress ipAddress = host.getIpAddress();
31         return ipAddress != null ? new InetSocketAddress(IetfInetUtil.INSTANCE.inetAddressFor(ipAddress), port)
32             : new InetSocketAddress(host.getDomainName().getValue(), port);
33     }
34
35     public static @NonNull RemoteDeviceId toRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
36         return new RemoteDeviceId(nodeId.getValue(), toInetSocketAddress(node));
37     }
38 }