2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
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;
26 public class ExportPolicyTest {
28 private static final Ipv4Address IPV4 = new Ipv4Address("1.2.3.4");
29 private static final ClusterIdentifier CLUSTER = new ClusterIdentifier(IPV4);
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);
37 public void testEbgpEffectiveAttributes() {
38 final ContainerNode clusterIn = createClusterInput();
39 final PeerRole peerRole = PeerRole.Ebgp;
40 assertEquals(clusterIn, REF_POLICY.effectiveAttributes(peerRole, clusterIn));
41 assertEquals(clusterIn, INT_POLICY.effectiveAttributes(peerRole, clusterIn));
43 final ContainerNode asPathIn = createPathInput(null);
44 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
48 public void testIbgpEffectiveAttributes() {
49 final ContainerNode clusterIn = createClusterInput();
50 final PeerRole peerRole = PeerRole.Ibgp;
51 assertEquals(createInputWithOriginator(), REF_POLICY.effectiveAttributes(peerRole, clusterIn));
52 assertEquals(null, INT_POLICY.effectiveAttributes(peerRole, clusterIn));
54 final ContainerNode asPathIn = createPathInput(null);
55 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
59 public void testRrClientEffectiveAttributes() {
60 final ContainerNode clusterIn = createClusterInput();
61 final PeerRole peerRole = PeerRole.RrClient;
62 assertEquals(createInputWithOriginator(), REF_POLICY.effectiveAttributes(peerRole, clusterIn));
63 assertEquals(createInputWithOriginator(), INT_POLICY.effectiveAttributes(peerRole, clusterIn));
65 final ContainerNode asPathIn = createPathInput(null);
66 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
70 public void testInternalPeerEffectiveAttributes() {
71 final ContainerNode clusterIn = createClusterInput();
72 final PeerRole peerRole = PeerRole.Internal;
73 assertEquals(createInternalOutput(), REF_POLICY.effectiveAttributes(peerRole, clusterIn));
74 assertEquals(createInternalOutput(), INT_POLICY.effectiveAttributes(peerRole, clusterIn));
76 final ContainerNode asPathIn = createPathInput(null);
77 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
81 * container cluster-id {
83 * type cluster-identifier;
88 * container originator-id {
95 private static ContainerNode createInputWithOriginator() {
96 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> b = Builders.containerBuilder();
97 b.withNodeIdentifier(new NodeIdentifier(QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "attribute-container"))));
98 b.withChild(createClusterId());
99 b.withChild(createOriginatorId());
103 private static ContainerNode createInternalOutput() {
104 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> b = Builders.containerBuilder();
105 b.withNodeIdentifier(new NodeIdentifier(QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "attribute-container"))));
106 b.withChild(createWithoutInternalClusterId());
110 private static ContainerNode createClusterInput() {
111 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> b = Builders.containerBuilder();
112 b.withNodeIdentifier(new NodeIdentifier(QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "attribute-container"))));
113 b.withChild(createClusterIdInput());
117 private static ContainerNode createWithoutInternalClusterId() {
118 final QName clusterContQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "cluster-id"));
119 final QName clusterQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "cluster"));
120 final NodeIdentifier clusterContNid = new NodeIdentifier(clusterContQName);
121 final NodeIdentifier clusterNid = new NodeIdentifier(clusterQName);
123 final ClusterIdentifier cluster1 = new ClusterIdentifier(new Ipv4Address("1.1.1.1"));
124 final ClusterIdentifier cluster2 = new ClusterIdentifier(new Ipv4Address("1.1.1.2"));
125 return Builders.containerBuilder().withNodeIdentifier(clusterContNid).addChild(Builders.orderedLeafSetBuilder().withNodeIdentifier(clusterNid)
126 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster2.getValue())).withValue(cluster2.getValue()).build())
127 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster1.getValue())).withValue(cluster1.getValue()).build())
131 private static ContainerNode createClusterId() {
132 final QName clusterContQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "cluster-id"));
133 final QName clusterQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "cluster"));
134 final NodeIdentifier clusterContNid = new NodeIdentifier(clusterContQName);
135 final NodeIdentifier clusterNid = new NodeIdentifier(clusterQName);
137 final ClusterIdentifier cluster1 = new ClusterIdentifier(new Ipv4Address("1.1.1.1"));
138 final ClusterIdentifier cluster2 = new ClusterIdentifier(new Ipv4Address("1.1.1.2"));
139 return Builders.containerBuilder().withNodeIdentifier(clusterContNid).addChild(
140 Builders.orderedLeafSetBuilder().withNodeIdentifier(clusterNid)
141 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, CLUSTER.getValue())).withValue(CLUSTER.getValue()).build())
142 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster2.getValue())).withValue(cluster2.getValue()).build())
143 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster1.getValue())).withValue(cluster1.getValue()).build())
147 private static ContainerNode createClusterIdInput() {
148 final QName clusterContQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "cluster-id"));
149 final QName clusterQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "cluster"));
150 final NodeIdentifier clusterContNid = new NodeIdentifier(clusterContQName);
151 final NodeIdentifier clusterNid = new NodeIdentifier(clusterQName);
153 final ClusterIdentifier cluster1 = new ClusterIdentifier(new Ipv4Address("1.1.1.1"));
154 final ClusterIdentifier cluster2 = new ClusterIdentifier(new Ipv4Address("1.1.1.2"));
155 return Builders.containerBuilder().withNodeIdentifier(clusterContNid).addChild(
156 Builders.orderedLeafSetBuilder().withNodeIdentifier(clusterNid)
157 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster2.getValue())).withValue(cluster2.getValue()).build())
158 .withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(clusterQName, cluster1.getValue())).withValue(cluster1.getValue()).build())
162 private static ContainerNode createOriginatorId() {
163 final QName originatorContQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "originator-id"));
164 final QName originatorQName = QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "originator"));
165 final NodeIdentifier originatorContNid = new NodeIdentifier(originatorContQName);
166 final NodeIdentifier originatorNid = new NodeIdentifier(originatorQName);
168 return Builders.containerBuilder().withNodeIdentifier(originatorContNid).withChild(
169 ImmutableNodes.leafNode(originatorNid, CLUSTER.getValue())).build();
175 private static ContainerNode createPathInputWithAs() {
176 return createPathInput(createSequenceWithLocalAs());
179 private static ContainerNode createPathInput(final UnkeyedListEntryNode child) {
180 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> segB = Builders.unkeyedListBuilder();
181 segB.withNodeIdentifier(BestPathSelectorTest.SEGMENTS_NID);
183 segB.addChild(child);
185 segB.addChild(BestPathSelectorTest.SET_SEGMENT).addChild(BestPathSelectorTest.SEQ_SEGMENT);
186 return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(BestPathSelectorTest.ATTRS_EXTENSION_Q))
187 .addChild(Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(QName.cachedReference(QName.create(BestPathSelectorTest.ATTRS_EXTENSION_Q, "as-path"))))
188 .addChild(segB.build()).build()).build();
191 private static UnkeyedListEntryNode createSequenceWithLocalAs() {
192 return Builders.unkeyedListEntryBuilder().withNodeIdentifier(BestPathSelectorTest.SEGMENTS_NID)
193 .addChild(Builders.orderedLeafSetBuilder().withNodeIdentifier(BestPathSelectorTest.SEQ_LEAFLIST_NID)
194 .addChild(Builders.leafSetEntryBuilder().withNodeIdentifier(new NodeWithValue(BestPathSelectorTest.AS_NUMBER_Q, LOCAL_AS)).withValue(LOCAL_AS).build())