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