3359cbbbf0f2a0ccb6f4643060375a38e586e6b4
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AbstractAddPathTest.java
1 /*
2  * Copyright (c) 2016 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.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
13 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Lists;
17 import io.netty.channel.epoll.Epoll;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.util.concurrent.Future;
20 import java.net.InetSocketAddress;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
34 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
35 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
36 import org.opendaylight.protocol.bgp.inet.RIBActivator;
37 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
38 import org.opendaylight.protocol.bgp.parser.BGPError;
39 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
40 import org.opendaylight.protocol.bgp.parser.impl.BGPActivator;
41 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
42 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
43 import org.opendaylight.protocol.bgp.rib.impl.config.BgpPeer;
44 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
45 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
46 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
47 import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
48 import org.opendaylight.protocol.util.InetSocketAddressUtil;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.OpenBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AsPathBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ClusterIdBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.LocalPrefBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.MultiExitDiscBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginatorIdBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.SegmentsBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutesBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpId;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpOrigin;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.ClusterIdentifier;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
92 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
93 import org.opendaylight.yangtools.yang.binding.Notification;
94 import org.opendaylight.yangtools.yang.common.Uint16;
95 import org.opendaylight.yangtools.yang.common.Uint32;
96
97 public abstract class AbstractAddPathTest extends DefaultRibPoliciesMockTest {
98     private static final int RETRY_TIMER = 10;
99     static final String RIB_ID = "127.0.0.1";
100     static final BgpId BGP_ID = new BgpId(RIB_ID);
101     static final Ipv4AddressNoZone PEER1 = new Ipv4AddressNoZone("127.0.0.2");
102     static final Ipv4AddressNoZone PEER2 = new Ipv4AddressNoZone("127.0.0.3");
103     static final Ipv4AddressNoZone PEER3 = new Ipv4AddressNoZone("127.0.0.4");
104     static final Ipv4AddressNoZone PEER4 = new Ipv4AddressNoZone("127.0.0.5");
105     static final Ipv4AddressNoZone PEER5 = new Ipv4AddressNoZone("127.0.0.6");
106     static final Ipv4AddressNoZone PEER6 = new Ipv4AddressNoZone("127.0.0.7");
107     static final AsNumber AS_NUMBER = new AsNumber(Uint32.valueOf(AS));
108     static final Uint16 PORT = Uint16.valueOf(InetSocketAddressUtil.getRandomPort());
109     static final Ipv4Prefix PREFIX1 = new Ipv4Prefix("1.1.1.1/32");
110     private static final ClusterIdentifier CLUSTER_ID = new ClusterIdentifier(RIB_ID);
111     static final int HOLDTIMER = 2180;
112     private static final Ipv4AddressNoZone NH1 = new Ipv4AddressNoZone("2.2.2.2");
113     static final Update UPD_100 = createSimpleUpdate(PREFIX1, new PathId(Uint32.ONE), CLUSTER_ID, 100);
114     static final Update UPD_50 = createSimpleUpdate(PREFIX1, new PathId(Uint32.TWO), CLUSTER_ID, 50);
115     static final Update UPD_200 = createSimpleUpdate(PREFIX1, new PathId(Uint32.valueOf(3)), CLUSTER_ID, 200);
116     static final Update UPD_20 = createSimpleUpdate(PREFIX1, new PathId(Uint32.ONE), CLUSTER_ID, 20);
117     static final Update UPD_NA_100 = createSimpleUpdate(PREFIX1, null, CLUSTER_ID, 100);
118     static final Update UPD_NA_100_EBGP = createSimpleUpdateEbgp(PREFIX1);
119     static final Update UPD_NA_200 = createSimpleUpdate(PREFIX1, null, CLUSTER_ID, 200);
120     static final Update UPD_NA_200_EBGP = createSimpleUpdateEbgp(PREFIX1);
121     static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
122     static final List<BgpTableType> TABLES_TYPE = ImmutableList.of(new BgpTableTypeImpl(TABLES_KEY.getAfi(),
123         TABLES_KEY.getSafi()));
124     static final Set<TablesKey> AFI_SAFIS_ADVERTIZED = Collections.singleton(TABLES_KEY);
125     private BGPExtensionProviderContext context;
126     static final InstanceIdentifier<BgpRib> BGP_IID = InstanceIdentifier.create(BgpRib.class);
127     static final int GRACEFUL_RESTART_TIME = 5;
128     @Mock
129     protected ClusterSingletonServiceProvider clusterSingletonServiceProvider;
130     BGPDispatcherImpl serverDispatcher;
131     RIBExtensionProviderContext ribExtension;
132     private RIBActivator ribActivator;
133     private BGPActivator bgpActivator;
134     private NioEventLoopGroup worker;
135     private NioEventLoopGroup boss;
136     private org.opendaylight.protocol.bgp.inet.BGPActivator inetActivator;
137     protected StrictBGPPeerRegistry serverRegistry;
138     protected CodecsRegistryImpl codecsRegistry;
139
140     private List<BGPDispatcherImpl> clientDispatchers;
141
142     @Override
143     @Before
144     public void setUp() throws Exception {
145         super.setUp();
146         this.ribActivator = new RIBActivator();
147         this.ribExtension = new SimpleRIBExtensionProviderContext();
148
149         this.ribActivator.startRIBExtensionProvider(this.ribExtension, this.mappingService);
150
151         this.bgpActivator = new BGPActivator();
152         this.inetActivator = new org.opendaylight.protocol.bgp.inet.BGPActivator();
153         this.context = new SimpleBGPExtensionProviderContext();
154         this.bgpActivator.start(this.context);
155         this.inetActivator.start(this.context);
156         if (!Epoll.isAvailable()) {
157             this.worker = new NioEventLoopGroup();
158             this.boss = new NioEventLoopGroup();
159         }
160         this.serverRegistry = new StrictBGPPeerRegistry();
161         this.serverDispatcher = new BGPDispatcherImpl(this.context.getMessageRegistry(), this.boss, this.worker,
162             this.serverRegistry);
163         doReturn(Mockito.mock(ClusterSingletonServiceRegistration.class)).when(this.clusterSingletonServiceProvider)
164             .registerClusterSingletonService(any(ClusterSingletonService.class));
165
166         this.codecsRegistry = CodecsRegistryImpl.create(this.mappingService.getCodecFactory(),
167                 this.ribExtension.getClassLoadingStrategy());
168         this.clientDispatchers = new ArrayList<>();
169     }
170
171     @Override
172     @After
173     public void tearDown() throws Exception {
174         this.serverDispatcher.close();
175         if (!Epoll.isAvailable()) {
176             this.worker.shutdownGracefully(0, 0, TimeUnit.SECONDS);
177             this.boss.shutdownGracefully(0, 0, TimeUnit.SECONDS);
178         }
179         this.mappingService.close();
180         this.ribActivator.close();
181         this.inetActivator.close();
182         this.bgpActivator.close();
183         this.clientDispatchers.forEach(BGPDispatcherImpl::close);
184         this.clientDispatchers = null;
185         super.tearDown();
186     }
187
188     void sendRouteAndCheckIsOnLocRib(final BGPSessionImpl session, final Ipv4Prefix prefix, final long localPreference,
189         final int expectedRoutesOnDS) throws Exception {
190         waitFutureSuccess(session.writeAndFlush(createSimpleUpdate(prefix, null, null, localPreference)));
191         checkLocRib(expectedRoutesOnDS);
192     }
193
194     void sendWithdrawalRouteAndCheckIsOnLocRib(final BGPSessionImpl session, final Ipv4Prefix prefix,
195         final long localPreference, final int expectedRoutesOnDS) throws Exception {
196         waitFutureSuccess(session.writeAndFlush(createSimpleWithdrawalUpdate(prefix, localPreference)));
197         checkLocRib(expectedRoutesOnDS);
198     }
199
200     void sendNotification(final BGPSessionImpl session) {
201         final Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode())
202             .setErrorSubcode(BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }).build();
203         waitFutureSuccess(session.writeAndFlush(notMsg));
204     }
205
206     void causeBGPError(final BGPSessionImpl session) {
207         final Open openObj = new OpenBuilder().setBgpIdentifier(new Ipv4AddressNoZone("1.1.1.1"))
208             .setHoldTimer(Uint16.valueOf(50)).setMyAsNumber(Uint16.valueOf(72)).build();
209         waitFutureSuccess(session.writeAndFlush(openObj));
210     }
211
212     private void checkLocRib(final int expectedRoutesOnDS) throws Exception {
213         // FIXME: remove this sleep
214         Thread.sleep(100);
215         readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
216             final Ipv4RoutesCase routes = (Ipv4RoutesCase) bgpRib.getRib().get(0).getLocRib().getTables().get(0)
217                 .getRoutes();
218             final int size;
219             if (routes != null) {
220                 final Ipv4Routes routesCase = routes.getIpv4Routes();
221                 if (routesCase != null) {
222                     final List<Ipv4Route> routeList = routesCase.getIpv4Route();
223                     size = routeList == null ? 0 : routeList.size();
224                 } else {
225                     size = 0;
226                 }
227             } else {
228                 size = 0;
229             }
230
231             Assert.assertEquals(expectedRoutesOnDS, size);
232             return bgpRib;
233         });
234     }
235
236     void checkPeersPresentOnDataStore(final int numberOfPeers) throws Exception {
237         readDataOperational(getDataBroker(), BGP_IID, bgpRib -> {
238             Assert.assertEquals(numberOfPeers, bgpRib.getRib().get(0).getPeer().size());
239             return bgpRib;
240         });
241     }
242
243     BGPSessionImpl createPeerSession(final Ipv4AddressNoZone peer, final BgpParameters bgpParameters,
244         final SimpleSessionListener sessionListener) throws InterruptedException {
245         return createPeerSession(peer, bgpParameters, sessionListener, AS_NUMBER);
246     }
247
248     BGPSessionImpl createPeerSession(final Ipv4AddressNoZone peer, final BgpParameters bgpParameters,
249                                      final SimpleSessionListener sessionListener,
250                                      final AsNumber remoteAsNumber) throws InterruptedException {
251         final StrictBGPPeerRegistry clientRegistry = new StrictBGPPeerRegistry();
252         final BGPDispatcherImpl clientDispatcher = new BGPDispatcherImpl(this.context.getMessageRegistry(), this.boss,
253                 this.worker, clientRegistry);
254
255         clientDispatchers.add(clientDispatcher);
256         clientRegistry.addPeer(new IpAddressNoZone(new Ipv4AddressNoZone(RIB_ID)), sessionListener,
257                 new BGPSessionPreferences(remoteAsNumber, HOLDTIMER, new BgpId(peer),
258                         AS_NUMBER, Lists.newArrayList(bgpParameters)));
259
260         return connectPeer(peer, clientDispatcher);
261     }
262
263     static BGPPeer configurePeer(final BGPTableTypeRegistryConsumer tableRegistry, final Ipv4AddressNoZone peerAddress,
264             final RIBImpl ribImpl, final BgpParameters bgpParameters, final PeerRole peerRole,
265             final BGPPeerRegistry bgpPeerRegistry) {
266         return configurePeer(tableRegistry, peerAddress, ribImpl, bgpParameters, peerRole, bgpPeerRegistry,
267                 AFI_SAFIS_ADVERTIZED, Collections.emptySet());
268     }
269
270     static BGPPeer configurePeer(final BGPTableTypeRegistryConsumer tableRegistry,
271             final Ipv4AddressNoZone peerAddress, final RIBImpl ribImpl, final BgpParameters bgpParameters,
272             final PeerRole peerRole, final BGPPeerRegistry bgpPeerRegistry, final Set<TablesKey> afiSafiAdvertised,
273             final Set<TablesKey> gracefulAfiSafiAdvertised) {
274         final BgpPeer bgpPeer = Mockito.mock(BgpPeer.class);
275         doReturn(Optional.empty()).when(bgpPeer).getErrorHandling();
276         return configurePeer(tableRegistry, peerAddress, ribImpl, bgpParameters, peerRole, bgpPeerRegistry,
277                 afiSafiAdvertised, gracefulAfiSafiAdvertised, Collections.emptyMap(), bgpPeer);
278     }
279
280     static BGPPeer configurePeer(final BGPTableTypeRegistryConsumer tableRegistry, final Ipv4AddressNoZone peerAddress,
281             final RIBImpl ribImpl, final BgpParameters bgpParameters, final PeerRole peerRole,
282             final BGPPeerRegistry bgpPeerRegistry, final Set<TablesKey> afiSafiAdvertised,
283             final Set<TablesKey> gracefulAfiSafiAdvertised, final Map<TablesKey, Integer> llGracefulTimersAdvertised,
284             final BgpPeer peer) {
285         final IpAddressNoZone ipAddress = new IpAddressNoZone(peerAddress);
286
287         final BGPPeer bgpPeer = new BGPPeer(tableRegistry, new IpAddressNoZone(peerAddress), null, ribImpl, peerRole,
288                 null, null, null, afiSafiAdvertised, gracefulAfiSafiAdvertised, llGracefulTimersAdvertised, peer);
289         final List<BgpParameters> tlvs = Lists.newArrayList(bgpParameters);
290         bgpPeerRegistry.addPeer(ipAddress, bgpPeer,
291                 new BGPSessionPreferences(AS_NUMBER, HOLDTIMER, new BgpId(RIB_ID), AS_NUMBER, tlvs));
292         bgpPeer.instantiateServiceInstance();
293         return bgpPeer;
294     }
295
296     private static BGPSessionImpl connectPeer(final Ipv4Address localAddress, final BGPDispatcherImpl dispatcherImpl)
297             throws InterruptedException {
298         final Future<BGPSessionImpl> future = dispatcherImpl
299                 .createClient(new InetSocketAddress(localAddress.getValue(), PORT.toJava()),
300                         new InetSocketAddress(RIB_ID, PORT.toJava()), RETRY_TIMER, true);
301         Thread.sleep(200);
302         waitFutureSuccess(future);
303         Thread.sleep(100);
304         return future.getNow();
305     }
306
307     static BgpParameters createParameter(final boolean addPath) {
308         return createParameter(addPath, false, null);
309     }
310
311     static BgpParameters createParameter(final boolean addPath,
312                                          final boolean addIpv6,
313                                          final Map<TablesKey, Boolean> gracefulTables) {
314         final TablesKey ipv4Key = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
315         final List<TablesKey> advertisedTables = Lists.newArrayList(ipv4Key);
316         if (addIpv6) {
317             final TablesKey ipv6Key = new TablesKey(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class);
318             advertisedTables.add(ipv6Key);
319         }
320         final List<TablesKey> addPathTables = new ArrayList<>();
321         if (addPath) {
322             addPathTables.add(ipv4Key);
323         }
324         return PeerUtil.createBgpParameters(advertisedTables, addPathTables, gracefulTables, GRACEFUL_RESTART_TIME);
325     }
326
327     private static Update createSimpleUpdate(final Ipv4Prefix prefix, final PathId pathId,
328             final ClusterIdentifier clusterId, final long localPreference) {
329         final AttributesBuilder attBuilder = new AttributesBuilder();
330         attBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(localPreference)).build());
331         attBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
332         attBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
333         attBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
334         if (clusterId != null) {
335             attBuilder.setClusterId(new ClusterIdBuilder().setCluster(Collections.singletonList(clusterId)).build());
336             attBuilder.setOriginatorId(new OriginatorIdBuilder()
337                 .setOriginator(new Ipv4AddressNoZone(clusterId))
338                 .build());
339         }
340         addAttributeAugmentation(attBuilder, prefix, pathId);
341         return new UpdateBuilder().setAttributes(attBuilder.build()).build();
342     }
343
344     private static Update createSimpleUpdateEbgp(final Ipv4Prefix prefix) {
345         final AttributesBuilder attBuilder = new AttributesBuilder();
346         attBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
347         attBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.singletonList(
348             new SegmentsBuilder().setAsSequence(Collections.singletonList(AS_NUMBER)).build())).build());
349         addAttributeAugmentation(attBuilder, prefix, null);
350
351         return new UpdateBuilder().setAttributes(attBuilder.build()).build();
352     }
353
354     private static void addAttributeAugmentation(final AttributesBuilder attBuilder, final Ipv4Prefix prefix,
355         final PathId pathId) {
356         attBuilder.setUnrecognizedAttributes(Collections.emptyList());
357         attBuilder.addAugmentation(Attributes1.class,
358             new Attributes1Builder().setMpReachNlri(
359                 new MpReachNlriBuilder()
360                     .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(NH1)
361                         .build()).build())
362                     .setAfi(Ipv4AddressFamily.class)
363                     .setSafi(UnicastSubsequentAddressFamily.class)
364                     .setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
365                         new DestinationIpv4CaseBuilder().setDestinationIpv4(
366                             new DestinationIpv4Builder().setIpv4Prefixes(Collections.singletonList(
367                                 new Ipv4PrefixesBuilder().setPathId(pathId).setPrefix(new Ipv4Prefix(prefix)).build()))
368                                 .build())
369                             .build()).build())
370                     .build()).build());
371     }
372
373     private static Update createSimpleWithdrawalUpdate(final Ipv4Prefix prefix, final long localPreference) {
374         final AttributesBuilder attBuilder = new AttributesBuilder();
375         attBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(localPreference)).build());
376         attBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
377         attBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
378         attBuilder.setUnrecognizedAttributes(Collections.emptyList());
379         return new UpdateBuilder()
380                 .setWithdrawnRoutes(Collections.singletonList(new WithdrawnRoutesBuilder().setPrefix(prefix).build()))
381                 .build();
382     }
383 }