Centralize NetconfNode/InetSocketAddress conversion
[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 org.opendaylight.netconf.api.DocumentedException;
11 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
12 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
13 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
19 import org.opendaylight.yangtools.yang.binding.Identifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.common.Decimal64;
23 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
24 import org.opendaylight.yangtools.yang.common.ErrorTag;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
26
27 public final class NetconfTopologyUtils {
28     public static final long DEFAULT_REQUEST_TIMEOUT_MILLIS = 60000L;
29     public static final int DEFAULT_KEEPALIVE_DELAY = 0;
30     public static final boolean DEFAULT_RECONNECT_ON_CHANGED_SCHEMA = false;
31     public static final boolean DEFAULT_IS_TCP_ONLY = false;
32     public static final int DEFAULT_CONCURRENT_RPC_LIMIT = 0;
33     public static final int DEFAULT_MAX_CONNECTION_ATTEMPTS = 0;
34     public static final int DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS = 2000;
35     public static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 20000L;
36     public static final Decimal64 DEFAULT_SLEEP_FACTOR = Decimal64.valueOf("1.5");
37
38     private NetconfTopologyUtils() {
39         // Hidden on purpose
40     }
41
42     public static String createActorPath(final String masterMember, final String name) {
43         return  masterMember + "/user/" + name;
44     }
45
46     public static String createMasterActorName(final String name, final String masterAddress) {
47         return masterAddress.replace("//", "") + "_" + name;
48     }
49
50     public static NodeId getNodeId(final InstanceIdentifier.PathArgument pathArgument) {
51         if (pathArgument instanceof InstanceIdentifier.IdentifiableItem) {
52             final Identifier<?> key = ((InstanceIdentifier.IdentifiableItem<?, ?>) pathArgument).getKey();
53             if (key instanceof NodeKey) {
54                 return ((NodeKey) key).getNodeId();
55             }
56         }
57         throw new IllegalStateException("Unable to create NodeId from: " + pathArgument);
58     }
59
60     public static KeyedInstanceIdentifier<Topology, TopologyKey> createTopologyListPath(final String topologyId) {
61         final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.create(NetworkTopology.class);
62         return networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
63     }
64
65     public static KeyedInstanceIdentifier<Node, NodeKey> createTopologyNodeListPath(final NodeKey key,
66             final String topologyId) {
67         return createTopologyListPath(topologyId)
68                 .child(Node.class, new NodeKey(new NodeId(key.getNodeId().getValue())));
69     }
70
71     public static InstanceIdentifier<Node> createTopologyNodePath(final String topologyId) {
72         return createTopologyListPath(topologyId).child(Node.class);
73     }
74
75     public static DocumentedException createMasterIsDownException(final RemoteDeviceId id, final Exception cause) {
76         return new DocumentedException(id + ":Master is down. Please try again.", cause,
77                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.WARNING);
78     }
79 }