Upgrade ietf-{inet,yang}-types to 2013-07-15
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AttributeOperationsTest.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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.ATTRS_EXTENSION_Q;
14 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.SEGMENTS_NID;
15 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.SEQ_SEGMENT;
16 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.SET_SEGMENT;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20 import org.junit.Test;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPath;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AtomicAggregate;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ClusterId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Origin;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginatorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41
42 public class AttributeOperationsTest {
43
44     static final NodeIdentifier ORIGIN_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, Origin.QNAME.getLocalName()).intern());
45     static final NodeIdentifier ORIGIN_VALUE_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "value").intern());
46     static final NodeIdentifier AS_PATH_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, AsPath.QNAME.getLocalName()).intern());
47     static final NodeIdentifier ATOMIC_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, AtomicAggregate.QNAME.getLocalName()));
48     static final NodeIdentifier CLUSTER_C_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, ClusterId.QNAME.getLocalName()));
49     static final NodeIdentifier CLUSTER_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "cluster"));
50     static final NodeIdentifier ORIGINATOR_C_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, OriginatorId.QNAME.getLocalName()));
51     static final NodeIdentifier ORIGINATOR_NID = new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "originator"));
52
53     @Test
54     public void testExportedAttributesSetFirst() {
55         final Long ourAs = 72L;
56         final ContainerNode attributesSetBefore = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(ATTRS_EXTENSION_Q))
57             .addChild(Builders.containerBuilder().withNodeIdentifier(AS_PATH_NID)
58                 .addChild(Builders.unkeyedListBuilder().withNodeIdentifier(SEGMENTS_NID)
59                     .addChild(SET_SEGMENT)
60                     .addChild(SEQ_SEGMENT)
61                     .build())
62             .build())
63             .build();
64         final AttributeOperations operations  = AttributeOperations.getInstance(attributesSetBefore);
65         final ContainerNode exportedAttributes = operations.exportedAttributes(attributesSetBefore, ourAs);
66
67         // make sure our AS is prepended to the list (as the AS-PATH starts with AS-SET)
68         final LeafSetNode<?> list = checkFirstLeafList(exportedAttributes);
69         assertEquals(ourAs, list.getValue().iterator().next().getValue());
70     }
71
72     @Test
73     public void testExportedAttributesListFirst() {
74         final Long ourAs = 72L;
75         final ContainerNode attributesListBefore = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(ATTRS_EXTENSION_Q))
76             .addChild(Builders.containerBuilder().withNodeIdentifier(AS_PATH_NID)
77                 .addChild(Builders.unkeyedListBuilder().withNodeIdentifier(SEGMENTS_NID)
78                     .addChild(SEQ_SEGMENT)
79                     .addChild(SET_SEGMENT)
80                     .build())
81             .build())
82             .build();
83         final AttributeOperations operations  = AttributeOperations.getInstance(attributesListBefore);
84         final ContainerNode exportedAttributes = operations.exportedAttributes(attributesListBefore, ourAs);
85
86         // make sure our AS is appended to the a-list (as the AS-PATH starts with A-LIST)
87         final LeafSetNode<?> list = checkFirstLeafList(exportedAttributes);
88         final Iterator<?> iter = list.getValue().iterator();
89         assertEquals(ourAs, ((LeafSetEntryNode<?>)iter.next()).getValue());
90         assertEquals(1L, ((LeafSetEntryNode<?>)iter.next()).getValue());
91         assertEquals(2L, ((LeafSetEntryNode<?>)iter.next()).getValue());
92         assertEquals(3L, ((LeafSetEntryNode<?>)iter.next()).getValue());
93     }
94
95     @Test
96     public void testExportedAttributesEmptyWithTransitive() {
97         final Long ourAs = 72L;
98         final ContainerNode attributesSetBefore = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(ATTRS_EXTENSION_Q))
99             .addChild(Builders.containerBuilder().withNodeIdentifier(ORIGIN_NID)
100                 .addChild(Builders.leafBuilder().withNodeIdentifier(ORIGIN_VALUE_NID).withValue(BgpOrigin.Egp).build())
101             .build())
102             .addChild(Builders.containerBuilder().withNodeIdentifier(AS_PATH_NID)
103                 .addChild(Builders.unkeyedListBuilder().withNodeIdentifier(SEGMENTS_NID).build())
104             .build())
105             .addChild(Builders.containerBuilder().withNodeIdentifier(ATOMIC_NID).build())
106             .build();
107         final AttributeOperations operations  = AttributeOperations.getInstance(attributesSetBefore);
108         final ContainerNode exportedAttributes = operations.exportedAttributes(attributesSetBefore, ourAs);
109
110         // Origin should be within exportedAttributes as it is Transitive
111         assertTrue(exportedAttributes.getChild(ORIGIN_NID).isPresent());
112
113         // AS-PATH should also be there with our AS
114         final LeafSetNode<?> list = checkFirstLeafList(exportedAttributes);
115         assertEquals(1, list.getValue().size());
116         assertEquals(ourAs, list.getValue().iterator().next().getValue());
117
118         // Atomic Aggregate should be filtered out
119         assertFalse(exportedAttributes.getChild(ATOMIC_NID).isPresent());
120     }
121
122     private LeafSetNode<?> checkFirstLeafList(final ContainerNode exportedAttributes) {
123         assertTrue(NormalizedNodes.findNode(exportedAttributes, AS_PATH_NID, SEGMENTS_NID).isPresent());
124         final UnkeyedListNode segments = (UnkeyedListNode) NormalizedNodes.findNode(exportedAttributes, AS_PATH_NID, SEGMENTS_NID).get();
125         final UnkeyedListEntryNode seg = segments.getValue().iterator().next();
126         final DataContainerChild<? extends PathArgument, ?> firstLeafList = seg.getValue().iterator().next();
127         return (LeafSetNode<?>) firstLeafList;
128     }
129
130     @Test
131     public void testReflectedAttributesOriginatorAndClusterNotPresent() {
132         final Ipv4Address originatorId = new Ipv4Address("127.0.0.2");
133         final ClusterIdentifier clusterId = new ClusterIdentifier("10.10.10.10");
134         final ContainerNode attributes = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(ATTRS_EXTENSION_Q))
135             .addChild(Builders.containerBuilder().withNodeIdentifier(ORIGIN_NID)
136                 .addChild(Builders.leafBuilder().withNodeIdentifier(ORIGIN_VALUE_NID).withValue(BgpOrigin.Egp).build())
137             .build())
138             .build();
139         final AttributeOperations operations  = AttributeOperations.getInstance(attributes);
140         final ContainerNode reflectedAttributes = operations.reflectedAttributes(attributes, originatorId, clusterId);
141
142         // Origin should be within reflectedAttributes as part of original attributes
143         assertTrue(reflectedAttributes.getChild(ORIGIN_NID).isPresent());
144
145         // ClusterIdentifier should be prepended
146         final Collection<?> clusters = checkCluster(reflectedAttributes).getValue();
147         assertEquals(1, clusters.size());
148         assertEquals(clusterId.getValue(), ((LeafSetEntryNode<?>)clusters.iterator().next()).getValue());
149
150         // OriginatorId should be added
151         assertTrue(reflectedAttributes.getChild(ORIGINATOR_C_NID).isPresent());
152         assertEquals(originatorId.getValue(), ((ContainerNode)reflectedAttributes.getChild(ORIGINATOR_C_NID).get()).getChild(ORIGINATOR_NID).get().getValue());
153     }
154
155     @Test
156     public void testReflectedAttributesOriginatorAndClusterPresent() {
157         final Ipv4Address originatorId = new Ipv4Address("127.0.0.2");
158         final ClusterIdentifier ourClusterId = new ClusterIdentifier("1.1.1.1");
159         final ClusterIdentifier clusterId1 = new ClusterIdentifier("10.10.10.10");
160         final ClusterIdentifier clusterId2 = new ClusterIdentifier("11.11.11.11");
161         final ContainerNode attributes = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(ATTRS_EXTENSION_Q))
162             .addChild(Builders.containerBuilder().withNodeIdentifier(ORIGINATOR_C_NID)
163                 .addChild(Builders.leafBuilder().withNodeIdentifier(ORIGINATOR_NID).withValue("127.0.0.2").build())
164             .build())
165             .addChild(Builders.containerBuilder().withNodeIdentifier(CLUSTER_C_NID)
166                 .addChild(Builders.orderedLeafSetBuilder().withNodeIdentifier(CLUSTER_NID)
167                     .addChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(CLUSTER_NID.getNodeType(), clusterId1.getValue())).withValue(clusterId1.getValue()).build())
168                     .addChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(CLUSTER_NID.getNodeType(), clusterId2.getValue())).withValue(clusterId2.getValue()).build())
169                 .build())
170             .build())
171             .build();
172         final AttributeOperations operations  = AttributeOperations.getInstance(attributes);
173         final ContainerNode reflectedAttributes = operations.reflectedAttributes(attributes, originatorId, ourClusterId);
174
175         // ClusterIdentifier should be prepended and other entries should be preserved
176         final Collection<?> clusters = checkCluster(reflectedAttributes).getValue();
177         assertEquals(3, clusters.size());
178         final Iterator<?> cl = clusters.iterator();
179
180         final LeafSetEntryNode<?> c1 = (LeafSetEntryNode<?>) cl.next();
181         assertEquals(ourClusterId.getValue(), c1.getValue());
182
183         final LeafSetEntryNode<?> c2 = (LeafSetEntryNode<?>) cl.next();
184         assertEquals(clusterId1.getValue(), c2.getValue());
185
186         final LeafSetEntryNode<?> c3 = (LeafSetEntryNode<?>) cl.next();
187         assertEquals(clusterId2.getValue(), c3.getValue());
188
189         // OriginatorId should be the same
190         assertTrue(reflectedAttributes.getChild(ORIGINATOR_C_NID).isPresent());
191         assertEquals(originatorId.getValue(), ((ContainerNode)reflectedAttributes.getChild(ORIGINATOR_C_NID).get()).getChild(ORIGINATOR_NID).get().getValue());
192     }
193
194     private LeafSetNode<?> checkCluster(final ContainerNode reflectedAttributes) {
195         assertTrue(reflectedAttributes.getChild(CLUSTER_C_NID).isPresent());
196         final ContainerNode clusterContainer = (ContainerNode) reflectedAttributes.getChild(CLUSTER_C_NID).get();
197         final LeafSetNode<?> clusters = (LeafSetNode<?>) clusterContainer.getChild(CLUSTER_NID).get();
198         return clusters;
199     }
200 }