Bug-4827: BGP Add-Path OpenConfig Support
[bgpcep.git] / bgp / openconfig-impl / src / test / java / org / opendaylight / protocol / bgp / openconfig / impl / util / OpenConfigUtilTest.java
1 /*
2  * Copyright (c) 2015 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.openconfig.impl.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.openconfig.impl.util.OpenConfigUtil.getModuleName;
13 import static org.opendaylight.protocol.bgp.openconfig.impl.util.OpenConfigUtil.toAfiSafi;
14
15 import com.google.common.collect.Lists;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.InvocationTargetException;
18 import java.util.Collections;
19 import java.util.List;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.mode.impl.add.all.paths.AllPathSelection;
23 import org.opendaylight.protocol.bgp.mode.impl.add.n.paths.AddPathBestNPathSelection;
24 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.use.multiple.paths.UseMultiplePaths;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.AfiSafi1;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.AfiSafi2;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamiliesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
37
38 public class OpenConfigUtilTest {
39
40     private static final BgpTableType BGP_TABLE_TYPE_IPV4 = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
41     private static final AfiSafi AFISAFI_IPV4 = new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class).build();
42     private static final String TEST = "/modules/module[type='dom-concurrent-data-broker'][name='concurrent-data-broker']";
43
44     @Test
45     public void test() {
46         final String moduleName = getModuleName(TEST);
47         assertEquals("concurrent-data-broker", moduleName);
48     }
49
50     @Test
51     public void testToAfiSafi() {
52         assertEquals(toAfiSafi(BGP_TABLE_TYPE_IPV4).get(),
53                 AFISAFI_IPV4);
54     }
55
56     @Test
57     public void testToAfiSafis() {
58         final List<AfiSafi> afiSafis = OpenConfigUtil.toAfiSafis(Lists.<BgpTableType>newArrayList(BGP_TABLE_TYPE_IPV4),
59                 (afisafi, tableType) -> afisafi);
60         Assert.assertEquals(Collections.singletonList(AFISAFI_IPV4), afiSafis);
61     }
62
63     @Test
64     public void toNeigborAfiSafiMultiPathMatch() {
65         final AfiSafi afiSafi = OpenConfigUtil.toNeigborAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
66                 Lists.newArrayList(new AddressFamiliesBuilder(BGP_TABLE_TYPE_IPV4).setSendReceive(SendReceive.Both).build()));
67         Assert.assertTrue(afiSafi.getAugmentation(AfiSafi2.class).getUseMultiplePaths().getConfig().isEnabled());
68     }
69
70     @Test
71     public void toNeigborAfiSafiMultiPathNoMatch() {
72         final AfiSafi afiSafi = OpenConfigUtil.toNeigborAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4, Collections.emptyList());
73         Assert.assertEquals(AFISAFI_IPV4, afiSafi);
74     }
75
76     @Test
77     public void toGlobalAfiSafiMultiPathNPaths() {
78         final AfiSafi afiSafi = OpenConfigUtil.toGlobalAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
79                 Collections.singletonMap(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class), new AddPathBestNPathSelection(5L)));
80         final UseMultiplePaths multiplePaths = afiSafi.getAugmentation(AfiSafi1.class).getUseMultiplePaths();
81         Assert.assertTrue(multiplePaths.getConfig().isEnabled());
82         Assert.assertEquals(5, multiplePaths.getIbgp().getConfig().getMaximumPaths().intValue());
83         Assert.assertEquals(5, multiplePaths.getEbgp().getConfig().getMaximumPaths().intValue());
84     }
85
86     @Test
87     public void toGlobalAfiSafiMultiPathAllPaths() {
88         final AfiSafi afiSafi = OpenConfigUtil.toGlobalAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
89                 Collections.singletonMap(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class), new AllPathSelection()));
90         final UseMultiplePaths multiplePaths = afiSafi.getAugmentation(AfiSafi1.class).getUseMultiplePaths();
91         Assert.assertTrue(multiplePaths.getConfig().isEnabled());
92         Assert.assertNull(multiplePaths.getIbgp());
93         Assert.assertNull(multiplePaths.getEbgp());
94     }
95
96     @Test
97     public void toGlobalAfiSafiMultiPathNoMatch() {
98         final AfiSafi afiSafi = OpenConfigUtil.toGlobalAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
99                 Collections.emptyMap());
100         Assert.assertEquals(AFISAFI_IPV4, afiSafi);
101     }
102
103     @Test(expected=UnsupportedOperationException.class)
104     public void testPrivateConstructor() throws Throwable {
105         final Constructor<OpenConfigUtil> c = OpenConfigUtil.class.getDeclaredConstructor();
106         c.setAccessible(true);
107         try {
108             c.newInstance();
109         } catch (final InvocationTargetException e) {
110             throw e.getCause();
111         }
112     }
113
114 }