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