938493cca1032ab0e1c23c0cd5e0732bb9de4c73
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / OpenConfigMappingUtil.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.protocol.bgp.rib.impl.config;
10
11 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil.INSTANCE;
12
13 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
14 import org.opendaylight.protocol.concepts.KeyMapping;
15 import org.opendaylight.protocol.util.Ipv4Util;
16 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
17 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborKey;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
19 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
20 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.Config2;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 final class OpenConfigMappingUtil {
27
28     private OpenConfigMappingUtil() {
29         throw new UnsupportedOperationException();
30     }
31
32     public static String getRibInstanceName(final InstanceIdentifier<?> rootIdentifier) {
33         return rootIdentifier.firstKeyOf(Protocol.class).getName();
34     }
35
36     public static int getHoldTimer(final Neighbor neighbor) {
37         return neighbor.getTimers().getConfig().getHoldTime().intValue();
38     }
39
40     public static AsNumber getPeerAs(final Neighbor neighbor, final RIB rib) {
41         final AsNumber peerAs = neighbor.getConfig().getPeerAs();
42         if (peerAs != null) {
43             return peerAs;
44         }
45         return rib.getLocalAs();
46     }
47
48     public static boolean isActive(final Neighbor neighbor) {
49         return !neighbor.getTransport().getConfig().isPassiveMode();
50     }
51
52     public static int getRetryTimer(final Neighbor neighbor) {
53         return neighbor.getTimers().getConfig().getConnectRetry().intValue();
54     }
55
56     public static KeyMapping getNeighborKey(final Neighbor neighbor) {
57         final String authPassword = neighbor.getConfig().getAuthPassword();
58         if (authPassword != null) {
59             KeyMapping.getKeyMapping(INSTANCE.inetAddressFor(neighbor.getNeighborAddress()), authPassword);
60         }
61         return null;
62     }
63
64     public static InstanceIdentifier<Neighbor> getNeighborInstanceIdentifier(final InstanceIdentifier<Bgp> rootIdentifier,
65             final NeighborKey neighborKey) {
66         return rootIdentifier.child(Neighbors.class).child(Neighbor.class, neighborKey);
67     }
68
69     public static String getNeighborInstanceName(final InstanceIdentifier<?> rootIdentifier) {
70         return Ipv4Util.toStringIP(rootIdentifier.firstKeyOf(Neighbor.class).getNeighborAddress());
71     }
72
73     public static PortNumber getPort(final Neighbor neighbor) {
74         return neighbor.getTransport().getConfig().getAugmentation(Config2.class).getRemotePort();
75     }
76
77 }