e0341a94c15f45496e5479c8a671093558417df5
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / utils / NetconfTopologyUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.singleton.impl.utils;
9
10 import java.net.InetSocketAddress;
11 import org.opendaylight.netconf.api.DocumentedException;
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.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
22 import org.opendaylight.yangtools.yang.binding.Identifier;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.common.Decimal64;
26 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29
30 public final class NetconfTopologyUtils {
31     public static final long DEFAULT_REQUEST_TIMEOUT_MILLIS = 60000L;
32     public static final int DEFAULT_KEEPALIVE_DELAY = 0;
33     public static final boolean DEFAULT_RECONNECT_ON_CHANGED_SCHEMA = false;
34     public static final boolean DEFAULT_IS_TCP_ONLY = false;
35     public static final int DEFAULT_CONCURRENT_RPC_LIMIT = 0;
36     public static final int DEFAULT_MAX_CONNECTION_ATTEMPTS = 0;
37     public static final int DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS = 2000;
38     public static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 20000L;
39     public static final Decimal64 DEFAULT_SLEEP_FACTOR = Decimal64.valueOf("1.5");
40
41     private NetconfTopologyUtils() {
42
43     }
44
45     public static RemoteDeviceId createRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
46         final IpAddress ipAddress = node.getHost().getIpAddress();
47         final InetSocketAddress address = new InetSocketAddress(ipAddress.getIpv4Address() != null
48                 ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue(),
49                 node.getPort().getValue().toJava());
50         return new RemoteDeviceId(nodeId.getValue(), address);
51     }
52
53     public static String createActorPath(final String masterMember, final String name) {
54         return  masterMember + "/user/" + name;
55     }
56
57     public static String createMasterActorName(final String name, final String masterAddress) {
58         return masterAddress.replace("//", "") + "_" + name;
59     }
60
61     public static NodeId getNodeId(final InstanceIdentifier.PathArgument pathArgument) {
62         if (pathArgument instanceof InstanceIdentifier.IdentifiableItem) {
63             final Identifier<?> key = ((InstanceIdentifier.IdentifiableItem<?, ?>) pathArgument).getKey();
64             if (key instanceof NodeKey) {
65                 return ((NodeKey) key).getNodeId();
66             }
67         }
68         throw new IllegalStateException("Unable to create NodeId from: " + pathArgument);
69     }
70
71     public static KeyedInstanceIdentifier<Topology, TopologyKey> createTopologyListPath(final String topologyId) {
72         final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.create(NetworkTopology.class);
73         return networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
74     }
75
76     public static KeyedInstanceIdentifier<Node, NodeKey> createTopologyNodeListPath(final NodeKey key,
77             final String topologyId) {
78         return createTopologyListPath(topologyId)
79                 .child(Node.class, new NodeKey(new NodeId(key.getNodeId().getValue())));
80     }
81
82     public static InstanceIdentifier<Node> createTopologyNodePath(final String topologyId) {
83         return createTopologyListPath(topologyId).child(Node.class);
84     }
85
86     public static DocumentedException createMasterIsDownException(final RemoteDeviceId id, final Exception cause) {
87         return new DocumentedException(id + ":Master is down. Please try again.", cause,
88                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.WARNING);
89     }
90 }