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 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;
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;
34 public class ExportPolicyTest {
36 private static final Ipv4Address IPV4 = new Ipv4Address("1.2.3.4");
37 private static final ClusterIdentifier CLUSTER = new ClusterIdentifier(IPV4);
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);
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));
53 final ContainerNode asPathIn = createPathInput(null);
54 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
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));
65 final ContainerNode asPathIn = createPathInput(null);
66 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
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));
77 final ContainerNode asPathIn = createPathInput(null);
78 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
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));
89 final ContainerNode asPathIn = createPathInput(null);
90 assertEquals(createPathInputWithAs(), EXT_POLICY.effectiveAttributes(peerRole, asPathIn));
94 * container cluster-id {
96 * type cluster-identifier;
101 * container originator-id {
105 * uses originator-id;
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());
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());
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());
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);
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())
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);
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())
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);
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())
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);
181 return Builders.containerBuilder().withNodeIdentifier(originatorContNid).withChild(
182 ImmutableNodes.leafNode(originatorNid, CLUSTER.getValue())).build();
188 private static ContainerNode createPathInputWithAs() {
189 return createPathInput(createSequenceWithLocalAs());
192 private static ContainerNode createPathInput(final UnkeyedListEntryNode child) {
193 final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> segB = Builders.unkeyedListBuilder();
194 segB.withNodeIdentifier(SEGMENTS_NID);
196 segB.addChild(child);
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();
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())