Upgrade ietf-{inet,yang}-types to 2013-07-15
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / ExportPolicyTest.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.assertNull;
12 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.AS_NUMBER_Q;
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_LEAFLIST_NID;
16 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.SEQ_SEGMENT;
17 import static org.opendaylight.protocol.bgp.mode.impl.base.BasePathSelectorTest.SET_SEGMENT;
18
19 import org.junit.Test;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
33
34 public class ExportPolicyTest {
35
36     private static final Ipv4Address IPV4 = new Ipv4Address("1.2.3.4");
37     private static final ClusterIdentifier CLUSTER = new ClusterIdentifier(IPV4);
38
39     private static final ToReflectorClientExportPolicy REF_POLICY = new ToReflectorClientExportPolicy(IPV4, CLUSTER);
40     private static final long LOCAL_AS = 8;
41     private static final ToExternalExportPolicy EXT_POLICY = new ToExternalExportPolicy(LOCAL_AS);
42     private static final ToInternalExportPolicy INT_POLICY = new ToInternalExportPolicy(IPV4, CLUSTER);
43     private static final ToInternalReflectorClientExportPolicy INTERNAL_POLICY = new ToInternalReflectorClientExportPolicy(IPV4, CLUSTER);
44
45     @Test
46     public void testEbgpEffectiveAttributes() {
47         final ContainerNode clusterIn = createClusterInput();
48         final PeerRole peerRole = PeerRole.Ebgp;
49         assertEquals(clusterIn, REF_POLICY.effectiveAttributes(peerRole, clusterIn));
50         assertNull(INTERNAL_POLICY.effectiveAttributes(peerRole, clusterIn));
51         assertEquals(clusterIn, INT_POLICY.effectiveAttributes(peerRole, clusterIn));
52
53         final ContainerNode asPathIn = createPathInput(null);
54         assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
55     }
56
57     @Test
58     public void testIbgpEffectiveAttributes() {
59         final ContainerNode clusterIn = createClusterInput();
60         final PeerRole peerRole = PeerRole.Ibgp;
61         assertEquals(createInputWithOriginator(), REF_POLICY.effectiveAttributes(peerRole, clusterIn));
62         assertNull(INTERNAL_POLICY.effectiveAttributes(peerRole, clusterIn));
63         assertEquals(null, INT_POLICY.effectiveAttributes(peerRole, clusterIn));
64
65         final ContainerNode asPathIn = createPathInput(null);
66         assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
67     }
68
69     @Test
70     public void testRrClientEffectiveAttributes() {
71         final ContainerNode clusterIn = createClusterInput();
72         final PeerRole peerRole = PeerRole.RrClient;
73         assertEquals(createInputWithOriginator(), REF_POLICY.effectiveAttributes(peerRole, clusterIn));
74         assertNull(INTERNAL_POLICY.effectiveAttributes(peerRole, clusterIn));
75         assertEquals(createInputWithOriginator(), INT_POLICY.effectiveAttributes(peerRole, clusterIn));
76
77         final ContainerNode asPathIn = createPathInput(null);
78         assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
79     }
80
81     @Test
82     public void testInternalPeerEffectiveAttributes() {
83         final ContainerNode clusterIn = createClusterInput();
84         final PeerRole peerRole = PeerRole.Internal;
85         assertEquals(createInternalOutput(), REF_POLICY.effectiveAttributes(peerRole, clusterIn));
86         assertNull(INTERNAL_POLICY.effectiveAttributes(peerRole, clusterIn));
87         assertEquals(createInternalOutput(), INT_POLICY.effectiveAttributes(peerRole, clusterIn));
88
89         final ContainerNode asPathIn = createPathInput(null);
90         assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
91     }
92
93     /**
94      * container cluster-id {
95      *     leaf-list cluster {
96      *         type cluster-identifier;
97      *     }
98      *     uses cluster-id;
99      * }
100      *
101      * container originator-id {
102      *     leaf originator {
103      *         type ipv4-address;
104      *     }
105      *     uses originator-id;
106      * }
107      */
108     private static ContainerNode createInputWithOriginator() {
109         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> b = Builders.containerBuilder();
110         b.withNodeIdentifier(new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "attribute-container").intern()));
111         b.withChild(createClusterId());
112         b.withChild(createOriginatorId());
113         return b.build();
114     }
115
116     private static ContainerNode createInternalOutput() {
117         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> b = Builders.containerBuilder();
118         b.withNodeIdentifier(new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "attribute-container").intern()));
119         b.withChild(createWithoutInternalClusterId());
120         return b.build();
121     }
122
123     private static ContainerNode createClusterInput() {
124         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> b = Builders.containerBuilder();
125         b.withNodeIdentifier(new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "attribute-container").intern()));
126         b.withChild(createClusterIdInput());
127         return b.build();
128     }
129
130     private static ContainerNode createWithoutInternalClusterId() {
131         final QName clusterContQName = QName.create(ATTRS_EXTENSION_Q, "cluster-id").intern();
132         final QName clusterQName = QName.create(ATTRS_EXTENSION_Q, "cluster").intern();
133         final NodeIdentifier clusterContNid = new NodeIdentifier(clusterContQName);
134         final NodeIdentifier clusterNid = new NodeIdentifier(clusterQName);
135
136         final ClusterIdentifier cluster1 = new ClusterIdentifier(new Ipv4Address("1.1.1.1"));
137         final ClusterIdentifier cluster2 = new ClusterIdentifier(new Ipv4Address("1.1.1.2"));
138         return Builders.containerBuilder().withNodeIdentifier(clusterContNid).addChild(Builders.orderedLeafSetBuilder().withNodeIdentifier(clusterNid)
139                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster2.getValue())).withValue(cluster2.getValue()).build())
140                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster1.getValue())).withValue(cluster1.getValue()).build())
141                 .build()).build();
142     }
143
144     private static ContainerNode createClusterId() {
145         final QName clusterContQName = QName.create(ATTRS_EXTENSION_Q, "cluster-id").intern();
146         final QName clusterQName = QName.create(ATTRS_EXTENSION_Q, "cluster").intern();
147         final NodeIdentifier clusterContNid = new NodeIdentifier(clusterContQName);
148         final NodeIdentifier clusterNid = new NodeIdentifier(clusterQName);
149
150         final ClusterIdentifier cluster1 = new ClusterIdentifier(new Ipv4Address("1.1.1.1"));
151         final ClusterIdentifier cluster2 = new ClusterIdentifier(new Ipv4Address("1.1.1.2"));
152         return Builders.containerBuilder().withNodeIdentifier(clusterContNid).addChild(
153             Builders.orderedLeafSetBuilder().withNodeIdentifier(clusterNid)
154                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, CLUSTER.getValue())).withValue(CLUSTER.getValue()).build())
155                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster2.getValue())).withValue(cluster2.getValue()).build())
156                 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster1.getValue())).withValue(cluster1.getValue()).build())
157                 .build()).build();
158     }
159
160     private static ContainerNode createClusterIdInput() {
161         final QName clusterContQName = QName.create(ATTRS_EXTENSION_Q, "cluster-id").intern();
162         final QName clusterQName = QName.create(ATTRS_EXTENSION_Q, "cluster").intern();
163         final NodeIdentifier clusterContNid = new NodeIdentifier(clusterContQName);
164         final NodeIdentifier clusterNid = new NodeIdentifier(clusterQName);
165
166         final ClusterIdentifier cluster1 = new ClusterIdentifier(new Ipv4Address("1.1.1.1"));
167         final ClusterIdentifier cluster2 = new ClusterIdentifier(new Ipv4Address("1.1.1.2"));
168         return Builders.containerBuilder().withNodeIdentifier(clusterContNid).addChild(
169             Builders.orderedLeafSetBuilder().withNodeIdentifier(clusterNid)
170             .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster2.getValue())).withValue(cluster2.getValue()).build())
171             .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster1.getValue())).withValue(cluster1.getValue()).build())
172             .build()).build();
173     }
174
175     private static ContainerNode createOriginatorId() {
176         final QName originatorContQName = QName.create(ATTRS_EXTENSION_Q, "originator-id").intern();
177         final QName originatorQName = QName.create(ATTRS_EXTENSION_Q, "originator").intern();
178         final NodeIdentifier originatorContNid = new NodeIdentifier(originatorContQName);
179         final NodeIdentifier originatorNid = new NodeIdentifier(originatorQName);
180
181         return Builders.containerBuilder().withNodeIdentifier(originatorContNid).withChild(
182             ImmutableNodes.leafNode(originatorNid, CLUSTER.getValue())).build();
183     }
184
185     /*
186      * AsPath
187      */
188     private static ContainerNode createPathInputWithAs() {
189         return createPathInput(createSequenceWithLocalAs());
190     }
191
192     private static ContainerNode createPathInput(final UnkeyedListEntryNode child) {
193         final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> segB = Builders.unkeyedListBuilder();
194         segB.withNodeIdentifier(SEGMENTS_NID);
195         if (child != null) {
196             segB.addChild(child);
197         }
198         segB.addChild(SET_SEGMENT).addChild(SEQ_SEGMENT);
199         return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(ATTRS_EXTENSION_Q))
200             .addChild(Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(QName.create(ATTRS_EXTENSION_Q, "as-path").intern()))
201                 .addChild(segB.build()).build()).build();
202     }
203
204     private static UnkeyedListEntryNode createSequenceWithLocalAs() {
205         return Builders.unkeyedListEntryBuilder().withNodeIdentifier(SEGMENTS_NID)
206             .addChild(Builders.orderedLeafSetBuilder().withNodeIdentifier(SEQ_LEAFLIST_NID)
207                 .addChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(AS_NUMBER_Q, LOCAL_AS)).withValue(LOCAL_AS).build())
208                 .build()).build();
209     }
210 }