Fix wrong aligment in Openconfig
[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(), AFISAFI_IPV4);
60     }
61
62     @Test
63     public void testToBgpTableType() {
64         final Optional<BgpTableType> bgpTableType = OpenConfigUtil.toBgpTableType(IPV4UNICAST.class);
65         assertEquals(BGP_TABLE_TYPE_IPV4, bgpTableType.get());
66     }
67
68     @Test
69     public void testToAfiSafis() {
70         final List<AfiSafi> afiSafis = OpenConfigUtil.toAfiSafis(Lists.newArrayList(BGP_TABLE_TYPE_IPV4), (afisafi, tableType) -> afisafi);
71         Assert.assertEquals(Collections.singletonList(AFISAFI_IPV4), afiSafis);
72     }
73
74     @Test
75     public void toNeigborAfiSafiMultiPathMatch() {
76         final AfiSafi afiSafi = OpenConfigUtil.toNeigborAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
77             Lists.newArrayList(new AddressFamiliesBuilder(BGP_TABLE_TYPE_IPV4).setSendReceive(SendReceive.Both).build()));
78         Assert.assertTrue(afiSafi.getAugmentation(AfiSafi2.class).getUseMultiplePaths().getConfig().isEnabled());
79     }
80
81     @Test
82     public void toNeigborAfiSafiMultiPathNoMatch() {
83         final AfiSafi afiSafi = OpenConfigUtil.toNeigborAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4, Collections.emptyList());
84         Assert.assertEquals(AFISAFI_IPV4, afiSafi);
85     }
86
87     @Test
88     public void toGlobalAfiSafiMultiPathNPaths() {
89         final AfiSafi afiSafi = OpenConfigUtil.toGlobalAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
90             Collections.singletonMap(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class), new AddPathBestNPathSelection(5L)));
91         final UseMultiplePaths multiplePaths = afiSafi.getAugmentation(AfiSafi1.class).getUseMultiplePaths();
92         Assert.assertTrue(multiplePaths.getConfig().isEnabled());
93         Assert.assertEquals(5, multiplePaths.getIbgp().getConfig().getMaximumPaths().intValue());
94         Assert.assertEquals(5, multiplePaths.getEbgp().getConfig().getMaximumPaths().intValue());
95     }
96
97     @Test
98     public void toGlobalAfiSafiMultiPathAllPaths() {
99         final AfiSafi afiSafi = OpenConfigUtil.toGlobalAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4,
100             Collections.singletonMap(new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class), new AllPathSelection()));
101         final UseMultiplePaths multiplePaths = afiSafi.getAugmentation(AfiSafi1.class).getUseMultiplePaths();
102         Assert.assertTrue(multiplePaths.getConfig().isEnabled());
103         Assert.assertNull(multiplePaths.getIbgp());
104         Assert.assertNull(multiplePaths.getEbgp());
105     }
106
107     @Test
108     public void toGlobalAfiSafiMultiPathNoMatch() {
109         final AfiSafi afiSafi = OpenConfigUtil.toGlobalAfiSafiMultiPath(AFISAFI_IPV4, BGP_TABLE_TYPE_IPV4, Collections.emptyMap());
110         Assert.assertEquals(AFISAFI_IPV4, afiSafi);
111     }
112
113     @Test(expected = UnsupportedOperationException.class)
114     public void testPrivateConstructor() throws Throwable {
115         final Constructor<OpenConfigUtil> c = OpenConfigUtil.class.getDeclaredConstructor();
116         c.setAccessible(true);
117         try {
118             c.newInstance();
119         } catch (final InvocationTargetException e) {
120             throw e.getCause();
121         }
122     }
123 }