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