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