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