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