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