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