Bug 6581 - Make timeout for ask configurable
[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.io.File;
12 import java.math.BigDecimal;
13 import java.net.InetSocketAddress;
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.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
31 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
32
33 public class NetconfTopologyUtils {
34
35     private static final String DEFAULT_SCHEMA_REPOSITORY_NAME = "sal-netconf-connector";
36
37     public static final long DEFAULT_REQUEST_TIMEOUT_MILLIS = 60000L;
38     public static final int DEFAULT_KEEPALIVE_DELAY = 0;
39     public static final boolean DEFAULT_RECONNECT_ON_CHANGED_SCHEMA = false;
40     public static final int DEFAULT_CONCURRENT_RPC_LIMIT = 0;
41     public static final int DEFAULT_MAX_CONNECTION_ATTEMPTS = 0;
42     public static final int DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS = 2000;
43     public static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 20000L;
44     public static final BigDecimal DEFAULT_SLEEP_FACTOR = new BigDecimal(1.5);
45
46
47     // The default cache directory relative to <code>CACHE_DIRECTORY</code>
48
49     public static final String DEFAULT_CACHE_DIRECTORY = "schema";
50
51     // Filesystem based caches are stored relative to the cache directory.
52     public static final String CACHE_DIRECTORY = "cache";
53
54     // The qualified schema cache directory <code>cache/schema</code>
55     public static final String QUALIFIED_DEFAULT_CACHE_DIRECTORY =
56             CACHE_DIRECTORY + File.separator + DEFAULT_CACHE_DIRECTORY;
57
58     // The default schema repository in the case that one is not specified.
59     public static final SharedSchemaRepository DEFAULT_SCHEMA_REPOSITORY =
60             new SharedSchemaRepository(DEFAULT_SCHEMA_REPOSITORY_NAME);
61
62
63      // The default <code>FilesystemSchemaSourceCache</code>, which stores cached files in <code>cache/schema</code>.
64     public static final FilesystemSchemaSourceCache<YangTextSchemaSource> DEFAULT_CACHE =
65             new FilesystemSchemaSourceCache<>(DEFAULT_SCHEMA_REPOSITORY, YangTextSchemaSource.class,
66                     new File(QUALIFIED_DEFAULT_CACHE_DIRECTORY));
67
68     // The default factory for creating <code>SchemaContext</code> instances.
69     public static final SchemaContextFactory DEFAULT_SCHEMA_CONTEXT_FACTORY =
70             DEFAULT_SCHEMA_REPOSITORY.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
71
72     public static RemoteDeviceId createRemoteDeviceId(final NodeId nodeId, final NetconfNode node) {
73         final IpAddress ipAddress = node.getHost().getIpAddress();
74         final InetSocketAddress address = new InetSocketAddress(ipAddress.getIpv4Address() != null
75                 ? ipAddress.getIpv4Address().getValue() : ipAddress.getIpv6Address().getValue(),
76                 node.getPort().getValue());
77         return new RemoteDeviceId(nodeId.getValue(), address);
78     }
79
80     public static String createActorPath(final String masterMember, final String name) {
81         return  masterMember + "/user/" + name;
82     }
83
84     public static String createMasterActorName(final String name, final String masterAddress) {
85         return masterAddress.replaceAll("//", "") + "_" + name;
86     }
87
88     public static NodeId getNodeId(final InstanceIdentifier.PathArgument pathArgument) {
89         if (pathArgument instanceof InstanceIdentifier.IdentifiableItem<?, ?>) {
90
91             final Identifier key = ((InstanceIdentifier.IdentifiableItem) pathArgument).getKey();
92             if (key instanceof NodeKey) {
93                 return ((NodeKey) key).getNodeId();
94             }
95         }
96         throw new IllegalStateException("Unable to create NodeId from: " + pathArgument);
97     }
98
99     public static KeyedInstanceIdentifier<Topology, TopologyKey> createTopologyListPath(final String topologyId) {
100         final InstanceIdentifier<NetworkTopology> networkTopology = InstanceIdentifier.create(NetworkTopology.class);
101         return networkTopology.child(Topology.class, new TopologyKey(new TopologyId(topologyId)));
102     }
103
104     public static InstanceIdentifier<Node> createTopologyNodeListPath(final NodeKey key, final String topologyId) {
105         return createTopologyListPath(topologyId)
106                 .child(Node.class, new NodeKey(new NodeId(key.getNodeId().getValue())));
107     }
108
109     public static InstanceIdentifier<Node> createTopologyNodePath(final String topologyId) {
110         return createTopologyListPath(topologyId).child(Node.class);
111     }
112 }