2 * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. 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.netvirt.bgpmanager;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ThreadFactoryBuilder;
12 import io.netty.util.concurrent.GlobalEventExecutor;
13 import java.io.IOException;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.net.InetAddress;
17 import java.net.NetworkInterface;
18 import java.net.SocketException;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Timer;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.Future;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.ScheduledFuture;
37 import java.util.concurrent.TimeUnit;
38 import java.util.concurrent.TimeoutException;
39 import java.util.concurrent.atomic.AtomicInteger;
40 import java.util.concurrent.atomic.AtomicReference;
41 import javax.annotation.Nullable;
42 import javax.annotation.PreDestroy;
43 import javax.inject.Inject;
44 import javax.inject.Singleton;
45 import org.apache.thrift.TApplicationException;
46 import org.apache.thrift.TException;
47 import org.apache.thrift.transport.TTransport;
48 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
49 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
50 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
51 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
52 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
53 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
54 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
55 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
56 import org.opendaylight.genius.mdsalutil.NwConstants;
57 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
58 import org.opendaylight.infrautils.metrics.MetricProvider;
59 import org.opendaylight.mdsal.eos.binding.api.Entity;
60 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
61 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
62 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
63 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
64 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
65 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
66 import org.opendaylight.netvirt.bgpmanager.commands.ClearBgpCli;
67 import org.opendaylight.netvirt.bgpmanager.oam.BgpAlarms;
68 import org.opendaylight.netvirt.bgpmanager.oam.BgpConstants;
69 import org.opendaylight.netvirt.bgpmanager.oam.BgpCounters;
70 import org.opendaylight.netvirt.bgpmanager.thrift.client.BgpRouter;
71 import org.opendaylight.netvirt.bgpmanager.thrift.client.BgpRouterException;
72 import org.opendaylight.netvirt.bgpmanager.thrift.client.BgpSyncHandle;
73 import org.opendaylight.netvirt.bgpmanager.thrift.gen.Routes;
74 import org.opendaylight.netvirt.bgpmanager.thrift.gen.Update;
75 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_afi;
76 import org.opendaylight.netvirt.bgpmanager.thrift.gen.af_safi;
77 import org.opendaylight.netvirt.bgpmanager.thrift.gen.protocol_type;
78 import org.opendaylight.netvirt.bgpmanager.thrift.gen.qbgpConstants;
79 import org.opendaylight.netvirt.bgpmanager.thrift.server.BgpThriftService;
80 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
81 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.IVpnLinkService;
82 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.AddressFamily;
83 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
84 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.BgpControlPlaneType;
85 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.EncapType;
86 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.TcpMd5SignaturePasswordType;
87 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.AsId;
88 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.AsIdBuilder;
89 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.ConfigServer;
90 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.ConfigServerBuilder;
91 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.GracefulRestart;
92 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.GracefulRestartBuilder;
93 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Logging;
94 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.LoggingBuilder;
95 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Multipath;
96 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.MultipathBuilder;
97 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.MultipathKey;
98 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
99 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.NeighborsBuilder;
100 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.NeighborsKey;
101 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Networks;
102 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.NetworksBuilder;
103 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.NetworksKey;
104 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.VrfMaxpath;
105 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.VrfMaxpathBuilder;
106 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.VrfMaxpathKey;
107 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Vrfs;
108 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.VrfsBuilder;
109 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.VrfsKey;
110 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.AddressFamilies;
111 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.AddressFamiliesBuilder;
112 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.AddressFamiliesKey;
113 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.EbgpMultihop;
114 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.EbgpMultihopBuilder;
115 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.UpdateSource;
116 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.UpdateSourceBuilder;
117 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfs.AddressFamiliesVrf;
118 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfs.AddressFamiliesVrfBuilder;
119 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.tcp.security.option.grouping.TcpSecurityOption;
120 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.tcp.security.option.grouping.tcp.security.option.TcpMd5SignatureOption;
121 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.tcp.security.option.grouping.tcp.security.option.TcpMd5SignatureOptionBuilder;
122 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
123 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.macvrfentries.MacVrfEntry;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
129 import org.opendaylight.yangtools.yang.binding.DataObject;
130 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
131 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
132 import org.osgi.framework.BundleContext;
133 import org.slf4j.Logger;
134 import org.slf4j.LoggerFactory;
137 public class BgpConfigurationManager {
138 private static final Logger LOG = LoggerFactory.getLogger(BgpConfigurationManager.class);
140 // to have stale FIB map (RD, Prefix)
141 // number of seconds wait for route sync-up between ODL and BGP
142 private static final int BGP_RESTART_ROUTE_SYNC_SEC = 600;
143 private static final String DEF_LOGFILE = "/var/log/bgp_debug.log";
144 private static final String DEF_LOGLEVEL = "errors";
145 private static final String UPDATE_PORT = "bgp.thrift.service.port";
146 private static final String CONFIG_HOST = "vpnservice.bgpspeaker.host.name";
147 private static final String CONFIG_PORT = "vpnservice.bgpspeaker.thrift.port";
148 private static final String DEF_UPORT = "6644";
149 private static final String DEF_CHOST = "255.255.255.255"; // Invalid Host IP
150 private static final String DEF_CPORT = "0"; // Invalid Port
151 private static final String DEF_BGP_SDNC_MIP = "127.0.0.1";
152 private static final String BGP_SDNC_MIP = "vpnservice.bgp.thrift.sdnc.mip";
153 private static final int RESTART_DEFAULT_GR = 90;
154 private static final int DS_RETRY_COUNT = 100; //100 retries, each after WAIT_TIME_BETWEEN_EACH_TRY_MILLIS seconds
155 private static final long WAIT_TIME_BETWEEN_EACH_TRY_MILLIS = 1000L; //one second sleep after every retry
156 private static final String BGP_ENTITY_TYPE_FOR_OWNERSHIP = "bgp";
157 private static final String BGP_ENTITY_NAME = "bgp";
158 private static final String ADD_WARN = "Config store updated; undo with Delete if needed.";
159 private static final String DEL_WARN = "Config store updated; undo with Add if needed.";
160 private static final String UPD_WARN = "Update operation not supported; Config store updated;"
161 + " restore with another Update if needed.";
163 private static final Class<?>[] REACTORS = {
164 ConfigServerReactor.class, AsIdReactor.class,
165 GracefulRestartReactor.class, LoggingReactor.class,
166 NeighborsReactor.class, UpdateSourceReactor.class,
167 EbgpMultihopReactor.class, AddressFamiliesReactor.class,
168 NetworksReactor.class, VrfsReactor.class, BgpReactor.class,
169 MultipathReactor.class, VrfMaxpathReactor.class
172 private IBgpManager bgpManager;
173 private final DataBroker dataBroker;
174 private final FibDSWriter fibDSWriter;
175 private final IVpnLinkService vpnLinkService;
176 private final BundleContext bundleContext;
177 private final BgpUtil bgpUtil;
178 private volatile Bgp config;
179 private final BgpRouter bgpRouter;
180 private final BgpSyncHandle bgpSyncHandle = new BgpSyncHandle();
181 private BgpThriftService updateServer;
183 private final CountDownLatch initer = new CountDownLatch(1);
185 private final String hostStartup;
186 private final String portStartup;
188 private final AtomicReference<BgpCounters> bgpCountersReference = new AtomicReference<>();
189 private ScheduledFuture<?> bgpCountersTask;
191 private final AtomicReference<BgpAlarms> bgpAlarmsReference = new AtomicReference<>();
192 private ScheduledFuture<?> bgpAlarmsTask;
194 private Future<?> lastReplayJobFt;
195 private ScheduledFuture<?> routeCleanupFuture;
197 private long staleStartTime;
198 private long staleEndTime;
199 private long cfgReplayStartTime;
200 private long cfgReplayEndTime;
201 private long staleCleanupTime;
202 private int totalStaledCount;
203 private int totalCleared;
204 private int totalExternalRoutes;
205 private int totalExternalMacRoutes;
207 private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
208 new ThreadFactoryBuilder().setNameFormat("BgpConfigurationManager-%d").setDaemon(true).build());
211 * this map store the new address families to send to quagga. When it is sended you must clear it.
212 * The keys String are rd (route distinguisher).
214 private final ConcurrentHashMap<String, List<AddressFamiliesVrf>> mapNewAdFamily = new ConcurrentHashMap<>();
216 // map<rd, map<prefix/len:nexthop, label>>
217 private final Map<String, Map<String, Long>> staledFibEntriesMap = new ConcurrentHashMap<>();
219 // map<rd, map<tep-ip, map<mac, l2vni>>>
220 private final Map<String, Map<String, Map<String, Long>>> rt2TepMap = new ConcurrentHashMap<>();
222 private final List<AutoCloseable> listeners = new ArrayList<>();
224 private final EntityOwnershipUtils entityOwnershipUtils;
225 private final EntityOwnershipCandidateRegistration candidateRegistration;
226 private final EntityOwnershipListenerRegistration entityListenerRegistration;
227 private final MetricProvider metricProvider;
230 public BgpConfigurationManager(final DataBroker dataBroker,
231 final EntityOwnershipService entityOwnershipService,
232 final FibDSWriter fibDSWriter,
233 final IVpnLinkService vpnLinkSrvce,
234 final BundleContext bundleContext,
235 final BgpUtil bgpUtil,
236 final MetricProvider metricProvider) {
237 this.dataBroker = dataBroker;
238 this.fibDSWriter = fibDSWriter;
239 this.vpnLinkService = vpnLinkSrvce;
240 this.bundleContext = bundleContext;
241 this.bgpUtil = bgpUtil;
242 this.metricProvider = metricProvider;
243 String updatePort = getProperty(UPDATE_PORT, DEF_UPORT);
244 hostStartup = getProperty(CONFIG_HOST, DEF_CHOST);
245 portStartup = getProperty(CONFIG_PORT, DEF_CPORT);
246 LOG.info("UpdateServer at localhost:{}, ConfigServer at {}:{}", updatePort, hostStartup, portStartup);
247 VtyshCli.setHostAddr(hostStartup);
248 ClearBgpCli.setHostAddr(hostStartup);
249 bgpRouter = BgpRouter.newInstance(this::getConfig, this::isBGPEntityOwner);
252 entityOwnershipUtils = new EntityOwnershipUtils(entityOwnershipService);
254 candidateRegistration = registerEntityCandidate(entityOwnershipService);
255 entityListenerRegistration = registerEntityListener(entityOwnershipService);
257 LOG.info("BGP Configuration manager initialized");
260 GlobalEventExecutor.INSTANCE.execute(() -> {
261 final WaitingServiceTracker<IBgpManager> tracker = WaitingServiceTracker.create(
262 IBgpManager.class, bundleContext);
263 bgpManager = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
264 updateServer = new BgpThriftService(Integer.parseInt(updatePort), bgpManager, this);
265 updateServer.start();
266 LOG.info("BgpConfigurationManager initialized. IBgpManager={}", bgpManager);
270 public String getBgpSdncMipIp() {
271 return getProperty(BGP_SDNC_MIP, DEF_BGP_SDNC_MIP);
274 public long getStaleCleanupTime() {
275 return staleCleanupTime;
278 public void setStaleCleanupTime(long staleCleanupTime) {
279 this.staleCleanupTime = staleCleanupTime;
282 public long getCfgReplayEndTime() {
283 return cfgReplayEndTime;
286 public void setCfgReplayEndTime(long cfgReplayEndTime) {
287 this.cfgReplayEndTime = cfgReplayEndTime;
290 public long getCfgReplayStartTime() {
291 return cfgReplayStartTime;
294 public void setCfgReplayStartTime(long cfgReplayStartTime) {
295 this.cfgReplayStartTime = cfgReplayStartTime;
298 public long getStaleEndTime() {
302 public void setStaleEndTime(long staleEndTime) {
303 this.staleEndTime = staleEndTime;
306 public long getStaleStartTime() {
307 return staleStartTime;
310 public void setStaleStartTime(long staleStartTime) {
311 this.staleStartTime = staleStartTime;
314 private Object createListener(Class<?> cls) {
316 Constructor<?> ctor = cls.getConstructor(BgpConfigurationManager.class);
317 return ctor.newInstance(this);
318 } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException
320 LOG.error("Failed to create listener object", e);
325 private void registerCallbacks() {
326 for (Class<?> reactor : REACTORS) {
327 Object obj = createListener(reactor);
329 AsyncDataTreeChangeListenerBase dcl = (AsyncDataTreeChangeListenerBase) obj;
330 dcl.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
336 @SuppressWarnings("checkstyle:IllegalCatch")
338 public void close() {
341 if (updateServer != null) {
345 if (candidateRegistration != null) {
346 candidateRegistration.close();
349 entityListenerRegistration.close();
351 listeners.forEach(l -> {
354 } catch (Exception e) {
355 LOG.warn("Error closing {}", l ,e);
359 LOG.info("{} close", getClass().getSimpleName());
362 private String getProperty(String var, String def) {
363 String property = bundleContext.getProperty(var);
364 return property == null ? def : property;
367 private EntityOwnershipCandidateRegistration registerEntityCandidate(
368 final EntityOwnershipService entityOwnershipService) {
370 return entityOwnershipService.registerCandidate(
371 new Entity(BGP_ENTITY_TYPE_FOR_OWNERSHIP, BGP_ENTITY_NAME));
372 } catch (CandidateAlreadyRegisteredException e) {
373 LOG.error("failed to register bgp entity", e);
378 private EntityOwnershipListenerRegistration registerEntityListener(
379 final EntityOwnershipService entityOwnershipService) {
380 return entityOwnershipService.registerListener(BGP_ENTITY_TYPE_FOR_OWNERSHIP, ownershipChange -> {
381 LOG.trace("entity owner change event fired: {}", ownershipChange);
383 if (ownershipChange.getState() == EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED) {
384 LOG.trace("This PL is the Owner");
388 LOG.debug("Not owner: hasOwner: {}, isOwner: {}", ownershipChange.getState().hasOwner(),
389 ownershipChange.getState().isOwner());
394 public boolean isBGPEntityOwner() {
395 return entityOwnershipUtils.isEntityOwner(new Entity(BGP_ENTITY_TYPE_FOR_OWNERSHIP, BGP_ENTITY_NAME), 0, 1);
399 config = getConfig();
403 public class ConfigServerReactor
404 extends AsyncDataTreeChangeListenerBase<ConfigServer, ConfigServerReactor>
405 implements ClusteredDataTreeChangeListener<ConfigServer> {
406 private static final String YANG_OBJ = "config-server ";
408 public ConfigServerReactor() {
409 super(ConfigServer.class, ConfigServerReactor.class);
413 protected void add(InstanceIdentifier<ConfigServer> iid, ConfigServer val) {
414 LOG.trace("received bgp connect config host {}", val.getHost().getValue());
415 if (!isBGPEntityOwner()) {
421 } catch (InterruptedException e) {
424 LOG.debug("issueing bgp router connect to host {}", val.getHost().getValue());
425 bgpRouter.configServerUpdated();
426 synchronized (BgpConfigurationManager.this) {
427 boolean res = bgpRouter.connect(val.getHost().getValue(),
428 val.getPort().intValue());
430 LOG.error(YANG_OBJ + "Add failed; " + ADD_WARN);
433 VtyshCli.setHostAddr(val.getHost().getValue());
434 ClearBgpCli.setHostAddr(val.getHost().getValue());
438 protected ConfigServerReactor getDataTreeChangeListener() {
439 return ConfigServerReactor.this;
443 protected InstanceIdentifier<ConfigServer> getWildCardPath() {
444 return InstanceIdentifier.create(Bgp.class).child(ConfigServer.class);
448 protected void remove(InstanceIdentifier<ConfigServer> iid, ConfigServer val) {
449 LOG.trace("received bgp disconnect");
450 if (!isBGPEntityOwner()) {
454 bgpRouter.configServerUpdated();
456 synchronized (BgpConfigurationManager.this) {
457 bgpRouter.disconnect();
462 protected void update(InstanceIdentifier<ConfigServer> iid,
463 ConfigServer oldval, ConfigServer newval) {
464 LOG.trace("received bgp Connection update");
465 if (!isBGPEntityOwner()) {
468 LOG.error(YANG_OBJ + UPD_WARN);
472 private BgpRouter getClient(String yangObj) {
473 if (bgpRouter == null || !bgpRouter.isBgpConnected()) {
474 LOG.warn("{}: configuration received when BGP is inactive", yangObj);
480 public class AsIdReactor
481 extends AsyncDataTreeChangeListenerBase<AsId, AsIdReactor>
482 implements ClusteredDataTreeChangeListener<AsId> {
484 private static final String YANG_OBJ = "as-id ";
486 public AsIdReactor() {
487 super(AsId.class, AsIdReactor.class);
491 protected void add(InstanceIdentifier<AsId> iid, AsId val) {
492 LOG.error("received bgp add asid {}", val);
493 if (!isBGPEntityOwner()) {
496 LOG.debug("received add router config asNum {}", val.getLocalAs());
497 synchronized (BgpConfigurationManager.this) {
498 BgpRouter br = getClient(YANG_OBJ);
500 LOG.debug("{} Unable to process add for asNum {}; {} {}", YANG_OBJ, val.getLocalAs(),
501 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
506 startBgpCountersTask();
507 startBgpAlarmsTask();
512 protected AsIdReactor getDataTreeChangeListener() {
513 return AsIdReactor.this;
517 protected InstanceIdentifier<AsId> getWildCardPath() {
518 return InstanceIdentifier.create(Bgp.class).child(AsId.class);
522 protected void remove(InstanceIdentifier<AsId> iid, AsId val) {
523 LOG.error("received delete router config asNum {}", val.getLocalAs());
524 if (!isBGPEntityOwner()) {
527 synchronized (BgpConfigurationManager.this) {
528 long asNum = val.getLocalAs();
529 BgpRouter br = getClient(YANG_OBJ);
531 LOG.debug("{} Unable to process remove for asNum {}; {} {}", YANG_OBJ, asNum,
532 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
537 } catch (TException | BgpRouterException e) {
538 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
541 stopBgpCountersTask();
544 Bgp conf = getConfig();
546 LOG.error("Config Null while removing the as-id");
549 LOG.debug("Removing external routes from FIB");
550 deleteExternalFibRoutes();
551 List<Neighbors> nbrs = conf.getNeighbors();
552 if (nbrs != null && nbrs.size() > 0) {
553 LOG.error("Tring to remove the as-id when neighbor config is already present");
554 for (Neighbors nbr : nbrs) {
555 LOG.debug("Removing Neighbor {} from Data store", nbr.getAddress().getValue());
556 delNeighbor(nbr.getAddress().getValue());
563 protected void update(InstanceIdentifier<AsId> iid,
564 AsId oldval, AsId newval) {
565 if (!isBGPEntityOwner()) {
568 LOG.error(YANG_OBJ + UPD_WARN);
572 public class GracefulRestartReactor
573 extends AsyncDataTreeChangeListenerBase<GracefulRestart, GracefulRestartReactor>
574 implements ClusteredDataTreeChangeListener<GracefulRestart> {
576 private static final String YANG_OBJ = "graceful-restart ";
578 public GracefulRestartReactor() {
579 super(GracefulRestart.class, GracefulRestartReactor.class);
583 protected void add(InstanceIdentifier<GracefulRestart> iid, GracefulRestart val) {
584 if (!isBGPEntityOwner()) {
587 synchronized (BgpConfigurationManager.this) {
588 int stalePathTime = val.getStalepathTime().intValue();
589 BgpRouter br = getClient(YANG_OBJ);
591 LOG.error("{} Unable to add stale-path time {}; {} {}", YANG_OBJ, stalePathTime,
592 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
596 br.addGracefulRestart(stalePathTime);
597 } catch (TException | BgpRouterException e) {
598 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
604 protected GracefulRestartReactor getDataTreeChangeListener() {
605 return GracefulRestartReactor.this;
609 protected InstanceIdentifier<GracefulRestart> getWildCardPath() {
610 return InstanceIdentifier.create(Bgp.class).child(GracefulRestart.class);
614 protected void remove(InstanceIdentifier<GracefulRestart> iid, GracefulRestart val) {
615 if (!isBGPEntityOwner()) {
618 LOG.debug("received delete GracefulRestart config val {}", val.getStalepathTime().intValue());
619 synchronized (BgpConfigurationManager.this) {
620 BgpRouter br = getClient(YANG_OBJ);
622 LOG.error("{} Unable to delete stale-path time; {} {}", YANG_OBJ,
623 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
627 br.delGracefulRestart();
628 } catch (TException | BgpRouterException e) {
629 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
635 protected void update(InstanceIdentifier<GracefulRestart> iid,
636 GracefulRestart oldval, GracefulRestart newval) {
637 if (!isBGPEntityOwner()) {
640 LOG.debug("received update GracefulRestart config val {}", newval.getStalepathTime().intValue());
641 synchronized (BgpConfigurationManager.this) {
642 int stalePathTime = newval.getStalepathTime().intValue();
643 BgpRouter br = getClient(YANG_OBJ);
645 LOG.error("{} Unable to update stale-path time to {}; {} {}", YANG_OBJ, stalePathTime,
646 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
650 br.addGracefulRestart(stalePathTime);
651 } catch (TException | BgpRouterException e) {
652 LOG.error("{} update received exception; {}", YANG_OBJ, ADD_WARN, e);
658 public class LoggingReactor
659 extends AsyncDataTreeChangeListenerBase<Logging, LoggingReactor>
660 implements ClusteredDataTreeChangeListener<Logging> {
662 private static final String YANG_OBJ = "logging ";
664 public LoggingReactor() {
665 super(Logging.class, LoggingReactor.class);
669 protected void add(InstanceIdentifier<Logging> iid, Logging val) {
670 if (!isBGPEntityOwner()) {
673 synchronized (BgpConfigurationManager.this) {
674 BgpRouter br = getClient(YANG_OBJ);
676 LOG.error("{} Unable to add logging for qbgp; {} {}", YANG_OBJ,
677 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
681 br.setLogging(val.getFile(), val.getLevel());
682 } catch (TException | BgpRouterException e) {
683 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
689 protected LoggingReactor getDataTreeChangeListener() {
690 return LoggingReactor.this;
694 protected InstanceIdentifier<Logging> getWildCardPath() {
695 return InstanceIdentifier.create(Bgp.class).child(Logging.class);
699 protected void remove(InstanceIdentifier<Logging> iid, Logging val) {
700 if (!isBGPEntityOwner()) {
703 LOG.debug("received remove Logging config val {}", val.getLevel());
704 synchronized (BgpConfigurationManager.this) {
705 BgpRouter br = getClient(YANG_OBJ);
707 LOG.error("{} Unable to remove logging for qbgp; {} {}", YANG_OBJ,
708 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
712 br.setLogging(DEF_LOGFILE, DEF_LOGLEVEL);
713 } catch (TException | BgpRouterException e) {
714 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
720 protected void update(InstanceIdentifier<Logging> iid,
721 Logging oldval, Logging newval) {
722 if (!isBGPEntityOwner()) {
725 synchronized (BgpConfigurationManager.this) {
726 BgpRouter br = getClient(YANG_OBJ);
728 LOG.error("{} Unable to update logging for qbgp; {} {}", YANG_OBJ,
729 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
733 br.setLogging(newval.getFile(), newval.getLevel());
734 } catch (TException | BgpRouterException e) {
735 LOG.error("{} newval received exception; {}", YANG_OBJ, ADD_WARN, e);
741 public class NeighborsReactor
742 extends AsyncDataTreeChangeListenerBase<Neighbors, NeighborsReactor>
743 implements ClusteredDataTreeChangeListener<Neighbors> {
745 private static final String YANG_OBJ = "neighbors ";
747 public NeighborsReactor() {
748 super(Neighbors.class, NeighborsReactor.class);
752 protected void add(InstanceIdentifier<Neighbors> iid, Neighbors val) {
753 if (!isBGPEntityOwner()) {
756 LOG.debug("received add Neighbors config val {}", val.getAddress().getValue());
757 synchronized (BgpConfigurationManager.this) {
758 String peerIp = val.getAddress().getValue();
759 long as = val.getRemoteAs();
760 final String md5Secret = extractMd5Secret(val);
761 BgpRouter br = getClient(YANG_OBJ);
763 LOG.debug("{} Unable to process add for peer {} as {}; {} {}", YANG_OBJ, peerIp, as,
764 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
768 //itmProvider.buildTunnelsToDCGW(new IpAddress(peerIp.toCharArray()));
769 br.addNeighbor(peerIp, as, md5Secret);
771 } catch (TException | BgpRouterException e) {
772 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
778 protected NeighborsReactor getDataTreeChangeListener() {
779 return NeighborsReactor.this;
783 protected InstanceIdentifier<Neighbors> getWildCardPath() {
784 return InstanceIdentifier.create(Bgp.class).child(Neighbors.class);
788 protected void remove(InstanceIdentifier<Neighbors> iid, Neighbors val) {
789 if (!isBGPEntityOwner()) {
792 LOG.debug("received remove Neighbors config val {}", val.getAddress().getValue());
793 synchronized (BgpConfigurationManager.this) {
794 String peerIp = val.getAddress().getValue();
795 BgpRouter br = getClient(YANG_OBJ);
797 LOG.debug("{} Unable to process remove for peer {}; {} {}", YANG_OBJ, peerIp,
798 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
802 //itmProvider.deleteTunnelsToDCGW(new IpAddress(val.getAddress().getValue().toCharArray()));
803 br.delNeighbor(peerIp);
804 } catch (TException | BgpRouterException e) {
805 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
808 final BgpAlarms bgpAlarms = getBgpAlarms();
809 if (bgpAlarms != null) {
810 bgpAlarms.clearBgpNbrDownAlarm(peerIp);
816 protected void update(InstanceIdentifier<Neighbors> iid,
817 Neighbors oldval, Neighbors newval) {
818 if (!isBGPEntityOwner()) {
821 //purposefully nothing to do.
825 public class EbgpMultihopReactor
826 extends AsyncDataTreeChangeListenerBase<EbgpMultihop, EbgpMultihopReactor>
827 implements ClusteredDataTreeChangeListener<EbgpMultihop> {
829 private static final String YANG_OBJ = "ebgp-multihop ";
831 public EbgpMultihopReactor() {
832 super(EbgpMultihop.class, EbgpMultihopReactor.class);
836 protected void add(InstanceIdentifier<EbgpMultihop> iid, EbgpMultihop val) {
837 if (!isBGPEntityOwner()) {
840 LOG.debug("received add EbgpMultihop config val {}", val.getPeerIp().getValue());
841 synchronized (BgpConfigurationManager.this) {
842 String peerIp = val.getPeerIp().getValue();
843 BgpRouter br = getClient(YANG_OBJ);
845 LOG.debug("{} Unable to process add for peer {}; {} {}", YANG_OBJ, peerIp,
846 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
850 br.addEbgpMultihop(peerIp, val.getNhops().intValue());
851 } catch (TException | BgpRouterException e) {
852 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
858 protected EbgpMultihopReactor getDataTreeChangeListener() {
859 return EbgpMultihopReactor.this;
863 protected InstanceIdentifier<EbgpMultihop> getWildCardPath() {
864 return InstanceIdentifier.create(Bgp.class).child(Neighbors.class).child(EbgpMultihop.class);
868 protected void remove(InstanceIdentifier<EbgpMultihop> iid, EbgpMultihop val) {
869 if (!isBGPEntityOwner()) {
872 LOG.debug("received remove EbgpMultihop config val {}", val.getPeerIp().getValue());
873 synchronized (BgpConfigurationManager.this) {
874 String peerIp = val.getPeerIp().getValue();
875 BgpRouter br = getClient(YANG_OBJ);
877 LOG.debug("{} Unable to process remove for peer {}; {} {}", YANG_OBJ, peerIp,
878 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
882 br.delEbgpMultihop(peerIp);
883 } catch (TException | BgpRouterException e) {
884 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
890 protected void update(InstanceIdentifier<EbgpMultihop> iid,
891 EbgpMultihop oldval, EbgpMultihop newval) {
892 if (!isBGPEntityOwner()) {
895 LOG.error(YANG_OBJ + UPD_WARN);
899 public class UpdateSourceReactor
900 extends AsyncDataTreeChangeListenerBase<UpdateSource, UpdateSourceReactor>
901 implements ClusteredDataTreeChangeListener<UpdateSource> {
903 private static final String YANG_OBJ = "update-source ";
905 public UpdateSourceReactor() {
906 super(UpdateSource.class, UpdateSourceReactor.class);
910 protected void add(InstanceIdentifier<UpdateSource> iid, UpdateSource val) {
911 if (!isBGPEntityOwner()) {
914 LOG.debug("received add UpdateSource config val {}", val.getSourceIp().getValue());
915 synchronized (BgpConfigurationManager.this) {
916 String peerIp = val.getPeerIp().getValue();
917 BgpRouter br = getClient(YANG_OBJ);
919 LOG.debug("{} Unable to process add for peer {}; {} {}", YANG_OBJ, peerIp,
920 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
924 br.addUpdateSource(peerIp, val.getSourceIp().getValue());
925 } catch (TException | BgpRouterException e) {
926 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
932 protected UpdateSourceReactor getDataTreeChangeListener() {
933 return UpdateSourceReactor.this;
937 protected InstanceIdentifier<UpdateSource> getWildCardPath() {
938 return InstanceIdentifier.create(Bgp.class).child(Neighbors.class).child(UpdateSource.class);
942 protected void remove(InstanceIdentifier<UpdateSource> iid, UpdateSource val) {
943 if (!isBGPEntityOwner()) {
946 LOG.debug("received remove UpdateSource config val {}", val.getSourceIp().getValue());
947 synchronized (BgpConfigurationManager.this) {
948 String peerIp = val.getPeerIp().getValue();
949 BgpRouter br = getClient(YANG_OBJ);
951 LOG.debug("{} Unable to process remove for peer {}; {} {}", YANG_OBJ, peerIp,
952 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
956 br.delUpdateSource(peerIp);
957 } catch (TException | BgpRouterException e) {
958 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
964 protected void update(InstanceIdentifier<UpdateSource> iid,
965 UpdateSource oldval, UpdateSource newval) {
966 if (!isBGPEntityOwner()) {
969 LOG.error(YANG_OBJ + UPD_WARN);
973 public class AddressFamiliesReactor
974 extends AsyncDataTreeChangeListenerBase<AddressFamilies, AddressFamiliesReactor>
975 implements ClusteredDataTreeChangeListener<AddressFamilies> {
977 private static final String YANG_OBJ = "address-families ";
979 public AddressFamiliesReactor() {
980 super(AddressFamilies.class, AddressFamiliesReactor.class);
984 protected void add(InstanceIdentifier<AddressFamilies> iid, AddressFamilies val) {
985 if (!isBGPEntityOwner()) {
988 LOG.debug("received add AddressFamilies config val {}", val.getPeerIp().getValue());
989 synchronized (BgpConfigurationManager.this) {
990 String peerIp = val.getPeerIp().getValue();
991 BgpRouter br = getClient(YANG_OBJ);
993 LOG.debug("{} Unable to process add for peer {}; {} {}", YANG_OBJ, peerIp,
994 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
997 af_afi afi = af_afi.findByValue(val.getAfi().intValue());
998 af_safi safi = af_safi.findByValue(val.getSafi().intValue());
1000 br.addAddressFamily(peerIp, afi, safi);
1001 } catch (TException | BgpRouterException e) {
1002 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
1008 protected AddressFamiliesReactor getDataTreeChangeListener() {
1009 return AddressFamiliesReactor.this;
1013 protected InstanceIdentifier<AddressFamilies> getWildCardPath() {
1014 return InstanceIdentifier.create(Bgp.class).child(Neighbors.class).child(AddressFamilies.class);
1018 protected void remove(InstanceIdentifier<AddressFamilies> iid, AddressFamilies val) {
1019 if (!isBGPEntityOwner()) {
1022 LOG.debug("received remove AddressFamilies config val {}", val.getPeerIp().getValue());
1023 synchronized (BgpConfigurationManager.this) {
1024 String peerIp = val.getPeerIp().getValue();
1025 BgpRouter br = getClient(YANG_OBJ);
1027 LOG.debug("{} Unable to process remove for peer {}; {} {}", YANG_OBJ, peerIp,
1028 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
1031 af_afi afi = af_afi.findByValue(val.getAfi().intValue());
1032 af_safi safi = af_safi.findByValue(val.getSafi().intValue());
1034 br.delAddressFamily(peerIp, afi, safi);
1035 } catch (TException | BgpRouterException e) {
1036 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
1042 protected void update(InstanceIdentifier<AddressFamilies> iid,
1043 AddressFamilies oldval, AddressFamilies newval) {
1044 if (!isBGPEntityOwner()) {
1047 LOG.error(YANG_OBJ + UPD_WARN);
1051 public class NetworksReactor
1052 extends AsyncDataTreeChangeListenerBase<Networks, NetworksReactor>
1053 implements ClusteredDataTreeChangeListener<Networks> {
1055 private static final String YANG_OBJ = "networks ";
1057 public NetworksReactor() {
1058 super(Networks.class, NetworksReactor.class);
1062 public NetworksReactor getDataTreeChangeListener() {
1063 return NetworksReactor.this;
1067 protected void add(InstanceIdentifier<Networks> iid, Networks val) {
1068 if (!isBGPEntityOwner()) {
1071 LOG.debug("received add Networks config val {}", val.getPrefixLen());
1072 synchronized (BgpConfigurationManager.this) {
1073 String rd = val.getRd();
1074 String pfxlen = val.getPrefixLen();
1075 String nh = val.getNexthop().getValue();
1076 BgpRouter br = getClient(YANG_OBJ);
1078 LOG.debug("{} Unable to process add for rd {} prefix {} nexthop {}; {} {}", YANG_OBJ, rd, pfxlen,
1079 nh, BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
1082 Long label = val.getLabel();
1083 int lbl = label == null ? qbgpConstants.LBL_NO_LABEL
1085 int l3vni = val.getL3vni() == null ? qbgpConstants.LBL_NO_LABEL
1086 : val.getL3vni().intValue();
1087 int l2vni = val.getL2vni() == null ? qbgpConstants.LBL_NO_LABEL
1088 : val.getL2vni().intValue();
1090 BgpControlPlaneType protocolType = val.getBgpControlPlaneType();
1091 int ethernetTag = val.getEthtag().intValue();
1092 String esi = val.getEsi();
1093 String macaddress = val.getMacaddress();
1094 EncapType encapType = val.getEncapType();
1095 String routerMac = val.getRoutermac();
1098 br.addPrefix(rd, pfxlen, nh, lbl, l3vni, l2vni, BgpUtil.convertToThriftProtocolType(protocolType),
1099 ethernetTag, esi, macaddress, BgpUtil.convertToThriftEncapType(encapType), routerMac);
1100 } catch (TException | BgpRouterException e) {
1101 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
1107 protected InstanceIdentifier<Networks> getWildCardPath() {
1108 return InstanceIdentifier.create(Bgp.class).child(Networks.class);
1112 protected void remove(InstanceIdentifier<Networks> iid, Networks val) {
1113 if (!isBGPEntityOwner()) {
1116 LOG.debug("received remove Networks config val {}", val.getPrefixLen());
1117 synchronized (BgpConfigurationManager.this) {
1118 String rd = val.getRd();
1119 String pfxlen = val.getPrefixLen();
1120 BgpRouter br = getClient(YANG_OBJ);
1122 LOG.debug("{} Unable to process remove for rd {} prefix {}; {} {}", YANG_OBJ, rd, pfxlen,
1123 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
1126 Long label = val.getLabel();
1127 int lbl = label == null ? 0 : label.intValue();
1128 if (rd == null && lbl > 0) {
1129 //LU prefix is being deleted.
1130 rd = Integer.toString(lbl);
1133 br.delPrefix(rd, pfxlen);
1134 } catch (TException | BgpRouterException e) {
1135 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
1140 /**get the value AFI from a prefix as "x.x.x.x/x".
1142 * @param pfxlen the prefix to get an afi
1143 * @return the afi value as you are need
1145 public int testValueAFI(String pfxlen) {
1146 int afiNew = af_afi.AFI_IP.getValue();
1148 String ipOnly = pfxlen.substring(0, pfxlen.lastIndexOf("/"));
1149 java.net.Inet6Address.getByName(ipOnly);
1150 afiNew = af_afi.AFI_IPV6.getValue();
1151 } catch (java.net.UnknownHostException e) {
1152 //ce n'est pas de l'ipv6
1159 protected void update(final InstanceIdentifier<Networks> iid,
1160 final Networks oldval, final Networks newval) {
1161 if (!isBGPEntityOwner()) {
1164 if (oldval.equals(newval)) {
1165 //Update: OLD and New values are same, no need to trigger remove/add.
1166 LOG.debug("received Updated for the same OLD and New values. RD: {}, Prefix: {}, Label: {}, NH: {}",
1167 oldval.getRd(), oldval.getPrefixLen(), oldval.getLabel(), oldval.getNexthop());
1170 LOG.debug("update networks old val RD: {}, Prefix: {}, Label: {}, NH: {} "
1171 + "new val RD: {}, Prefix: {}, Label: {}, NH: {}",
1172 oldval.getRd(), oldval.getPrefixLen(), oldval.getLabel(), oldval.getNexthop(),
1173 newval.getRd(), newval.getPrefixLen(), newval.getLabel(), newval.getNexthop());
1174 remove(iid, oldval);
1179 static Timer timer = new Timer();
1181 public class VrfsReactor
1182 extends AsyncDataTreeChangeListenerBase<Vrfs, VrfsReactor>
1183 implements ClusteredDataTreeChangeListener<Vrfs> {
1185 private static final String YANG_OBJ = "vrfs ";
1187 public VrfsReactor() {
1188 super(Vrfs.class, VrfsReactor.class);
1192 protected void add(InstanceIdentifier<Vrfs> iid, Vrfs vrfs) {
1193 if (!isBGPEntityOwner()) {
1196 LOG.debug("received add Vrfs config value {}", vrfs.getRd());
1197 synchronized (BgpConfigurationManager.this) {
1198 String rd = vrfs.getRd();
1199 BgpRouter br = getClient(YANG_OBJ);
1201 LOG.debug("{} Unable to process add for rd {}; {} {}", YANG_OBJ, rd,
1202 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
1206 List<AddressFamiliesVrf> vrfAddrFamilyList = vrfs.getAddressFamiliesVrf();
1207 for (AddressFamiliesVrf vrfAddrFamily : vrfAddrFamilyList) {
1208 /*add to br the new vrfs arguments*/
1209 br.addVrf(BgpUtil.getLayerType(vrfAddrFamily), rd, vrfs.getImportRts(), vrfs.getExportRts()
1212 /*add to br the vrfs contained in mapNewAdFamily*/
1213 List<AddressFamiliesVrf> vrfAddrFamilyListFromMap = mapNewAdFamily.get(rd);
1214 if (vrfAddrFamilyListFromMap == null) {
1218 for (AddressFamiliesVrf adf : vrfAddrFamilyListFromMap) {
1219 if (vrfAddrFamilyList.contains(adf)) {
1220 mapNewAdFamily.remove(rd);
1221 } else if (adf != null) {
1223 br.addVrf(BgpUtil.getLayerType(adf), rd, vrfs.getImportRts(), vrfs.getExportRts()
1225 // remove AddressFamiliesVrf which was already added to BGP
1226 vrfAddrFamilyListFromMap.remove(adf);
1227 if (vrfAddrFamilyListFromMap.isEmpty()) {
1228 // remove Vrf entry from temp mapNewAdFamily if all its AddressFamiliesVrf was
1230 mapNewAdFamily.remove(rd);
1234 } catch (TException | BgpRouterException e) {
1235 LOG.error("{} get {}, Add received exception", YANG_OBJ, ADD_WARN, e);
1241 protected VrfsReactor getDataTreeChangeListener() {
1242 return VrfsReactor.this;
1246 protected InstanceIdentifier<Vrfs> getWildCardPath() {
1247 return InstanceIdentifier.create(Bgp.class).child(Vrfs.class);
1251 protected void remove(InstanceIdentifier<Vrfs> iid, Vrfs val) {
1252 if (!isBGPEntityOwner()) {
1255 LOG.debug("received remove Vrfs config val {}", val.getRd());
1256 synchronized (BgpConfigurationManager.this) {
1257 String rd = val.getRd();
1258 BgpRouter br = getClient(YANG_OBJ);
1260 LOG.debug("{} Unable to process remove for rd {}; {} {}", YANG_OBJ, rd,
1261 BgpRouterException.BGP_ERR_NOT_INITED, DEL_WARN);
1265 List<AddressFamiliesVrf> adf = mapNewAdFamily.get(rd);
1266 adf = adf != null ? adf : new ArrayList<>();
1267 for (AddressFamiliesVrf s : val.getAddressFamiliesVrf()) {
1268 br.delVrf(rd, s.getAfi(), s.getSafi());
1269 adf.remove(s);// remove in the map the vrf in waiting for advertise quagga
1271 if (adf.isEmpty()) {
1272 mapNewAdFamily.remove(rd);
1274 } catch (TException | BgpRouterException e) {
1275 LOG.error("{} Delete received exception; {}", YANG_OBJ, DEL_WARN, e);
1281 protected void update(InstanceIdentifier<Vrfs> iid,
1282 Vrfs oldval, Vrfs newval) {
1283 if (oldval != null && newval != null) {
1284 LOG.debug("received update Vrfs config val {}, VRFS: Update getting triggered for VRFS rd {}",
1285 newval.getRd(), oldval.getRd());
1287 LOG.debug("received update Vrfs config val {}, from old vrf {}",
1290 if (!isBGPEntityOwner()) {
1294 List<AddressFamiliesVrf> adFamilyVrfToDel = new ArrayList<>();
1295 List<AddressFamiliesVrf> adFamilyVrfToAdd = new ArrayList<>();
1296 List<AddressFamiliesVrf> oldlistAdFamilies = new ArrayList<>();
1297 List<AddressFamiliesVrf> newlistAdFamilies = new ArrayList<>();
1298 if (oldval != null) {
1299 oldlistAdFamilies = oldval.getAddressFamiliesVrf() == null
1300 ? new ArrayList<>() : oldval.getAddressFamiliesVrf();
1302 if (newval != null) {
1303 newlistAdFamilies = newval.getAddressFamiliesVrf() == null
1304 ? new ArrayList<>() : newval.getAddressFamiliesVrf();
1306 /*find old AddressFamily to remove from new configuration*/
1307 for (AddressFamiliesVrf adVrf : oldlistAdFamilies) {
1308 if (!newlistAdFamilies.contains(adVrf)) {
1309 adFamilyVrfToDel.add(adVrf);
1312 /*find new AddressFamily to add to unexisting configuration*/
1313 for (AddressFamiliesVrf adVrf : newlistAdFamilies) {
1314 if (!oldlistAdFamilies.contains(adVrf)) {
1315 adFamilyVrfToAdd.add(adVrf);
1318 String rd = newval != null ? newval.getRd() : null;
1320 BgpRouter br = getClient(YANG_OBJ);
1322 LOG.debug("{} Unable to process add for rd {}; {} {}", YANG_OBJ, rd,
1323 BgpRouterException.BGP_ERR_NOT_INITED, ADD_WARN);
1327 for (AddressFamiliesVrf adfvrf : adFamilyVrfToAdd) {
1329 LOG.debug("call addVRf rd {} afi {} safi {}", rd, adfvrf.getAfi(), adfvrf.getSafi());
1330 br.addVrf(BgpUtil.getLayerType(adfvrf), rd, newval.getImportRts(),
1331 newval.getExportRts());
1332 } catch (TException | BgpRouterException e) {
1333 LOG.error("{} Add received exception; {}", YANG_OBJ, ADD_WARN, e);
1337 for (AddressFamiliesVrf adfToDel : adFamilyVrfToDel) {
1339 LOG.debug("call delVRf rd {} afi {} safi {}", rd, adfToDel.getAfi(), adfToDel.getSafi());
1340 br.delVrf(rd, adfToDel.getAfi(), adfToDel.getSafi());
1341 } catch (TException | BgpRouterException e) {
1342 LOG.error("{} delVrf received exception; {}", YANG_OBJ, ADD_WARN, e);
1349 protected void activateMIP() {
1351 LOG.trace("BgpReactor: Executing MIP Activate command");
1352 Runtime.getRuntime().exec("cluster ip -a sdnc_bgp_mip");
1353 Runtime.getRuntime().exec("cluster ip -a sdnc_os_mip");
1354 LOG.trace("bgpMIP Activated");
1356 } catch (IOException io) {
1357 LOG.error("IO Exception got while activating mip: {}", io.getMessage());
1361 public class BgpReactor
1362 extends AsyncDataTreeChangeListenerBase<Bgp, BgpReactor>
1363 implements ClusteredDataTreeChangeListener<Bgp> {
1365 private static final String YANG_OBJ = "Bgp ";
1367 public BgpReactor() {
1368 super(Bgp.class, BgpReactor.class);
1373 protected void add(InstanceIdentifier<Bgp> iid, Bgp val) {
1374 LOG.debug("received add Bgp config");
1378 } catch (InterruptedException e) {
1381 synchronized (BgpConfigurationManager.this) {
1383 if (!isBGPEntityOwner()) {
1391 protected BgpReactor getDataTreeChangeListener() {
1392 return BgpReactor.this;
1396 protected InstanceIdentifier<Bgp> getWildCardPath() {
1397 return InstanceIdentifier.create(Bgp.class);
1401 protected void remove(InstanceIdentifier<Bgp> iid, Bgp val) {
1402 if (!isBGPEntityOwner()) {
1405 LOG.debug("received remove Bgp config");
1411 protected void update(InstanceIdentifier<Bgp> iid,
1412 Bgp oldval, Bgp newval) {
1413 if (!isBGPEntityOwner()) {
1421 @SuppressWarnings("deprecation")
1422 public class MultipathReactor
1423 extends AsyncDataTreeChangeListenerBase<Multipath, MultipathReactor>
1424 implements ClusteredDataTreeChangeListener<Multipath> {
1426 private static final String YANG_OBJ = "multipath ";
1428 public MultipathReactor() {
1429 super(Multipath.class, MultipathReactor.class);
1434 protected MultipathReactor getDataTreeChangeListener() {
1435 return MultipathReactor.this;
1439 protected InstanceIdentifier<Multipath> getWildCardPath() {
1440 return InstanceIdentifier.create(Bgp.class).child(Multipath.class);
1444 protected void remove(InstanceIdentifier<Multipath> iid, Multipath val) {
1445 executor.execute(new MultipathStatusChange(val));
1449 protected void update(InstanceIdentifier<Multipath> iid, Multipath oldval, Multipath newval) {
1450 executor.execute(new MultipathStatusChange(newval));
1454 protected void add(InstanceIdentifier<Multipath> key, Multipath dataObjectModification) {
1455 executor.execute(new MultipathStatusChange(dataObjectModification));
1458 class MultipathStatusChange implements Runnable {
1460 Multipath multipath;
1462 MultipathStatusChange(Multipath multipath) {
1463 this.multipath = multipath;
1468 if (isBGPEntityOwner()) {
1469 synchronized (BgpConfigurationManager.this) {
1471 BgpRouter br = getClient(YANG_OBJ);
1474 af_afi afi = af_afi.findByValue(multipath.getAfi().intValue());
1475 af_safi safi = af_safi.findByValue(multipath.getSafi().intValue());
1478 if (multipath.isMultipathEnabled()) {
1479 br.enableMultipath(afi, safi);
1481 br.disableMultipath(afi, safi);
1483 } catch (TException | BgpRouterException e) {
1484 LOG.error("{} received exception", YANG_OBJ, e);
1494 public void close() {
1499 @SuppressWarnings("deprecation")
1500 public class VrfMaxpathReactor
1501 extends AsyncDataTreeChangeListenerBase<VrfMaxpath, VrfMaxpathReactor>
1502 implements ClusteredDataTreeChangeListener<VrfMaxpath> {
1504 private static final String YANG_OBJ = "vrfMaxpath ";
1506 public VrfMaxpathReactor() {
1507 super(VrfMaxpath.class, VrfMaxpathReactor.class);
1512 protected VrfMaxpathReactor getDataTreeChangeListener() {
1513 return VrfMaxpathReactor.this;
1517 protected InstanceIdentifier<VrfMaxpath> getWildCardPath() {
1518 return InstanceIdentifier.create(Bgp.class).child(VrfMaxpath.class);
1521 class VrfMaxPathConfigurator implements Runnable {
1523 VrfMaxpath vrfMaxpathVal;
1525 VrfMaxPathConfigurator(VrfMaxpath vrfMaxPathVal) {
1526 this.vrfMaxpathVal = vrfMaxPathVal;
1531 if (isBGPEntityOwner()) {
1532 synchronized (BgpConfigurationManager.this) {
1533 BgpRouter br = getClient(YANG_OBJ);
1536 br.multipaths(vrfMaxpathVal.getRd(), vrfMaxpathVal.getMaxpaths());
1537 LOG.debug("Maxpath for vrf {} is {}", vrfMaxpathVal.getRd(),
1538 vrfMaxpathVal.getMaxpaths());
1539 } catch (TException | BgpRouterException e) {
1540 LOG.error("{} received exception", YANG_OBJ, e);
1549 protected void remove(InstanceIdentifier<VrfMaxpath> iid, VrfMaxpath vrfMaxPathVal) {
1550 executor.execute(new VrfMaxPathConfigurator(vrfMaxPathVal));
1554 protected void update(InstanceIdentifier<VrfMaxpath> iid,
1555 VrfMaxpath oldval, VrfMaxpath newval) {
1556 if (!Objects.equals(oldval.getMaxpaths(), newval.getMaxpaths())) {
1557 executor.execute(new VrfMaxPathConfigurator(newval));
1562 protected void add(InstanceIdentifier<VrfMaxpath> instanceIdentifier, VrfMaxpath vrfMaxpathVal) {
1563 executor.execute(new VrfMaxPathConfigurator(vrfMaxpathVal));
1567 public void close() {
1572 public boolean isIpAvailable(String odlip) {
1575 if (odlip != null) {
1576 if ("127.0.0.1".equals(odlip)) {
1579 Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
1580 while (networkInterfaceEnumeration.hasMoreElements()) {
1581 NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
1582 Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
1583 while (inetAddressEnumeration.hasMoreElements()) {
1584 InetAddress inetAddress = inetAddressEnumeration.nextElement();
1585 if (odlip.equals(inetAddress.getHostAddress())) {
1591 } catch (SocketException e) {
1597 public long getStalePathtime(int defValue, AsId asId) {
1600 spt = getConfig().getGracefulRestart().getStalepathTime();
1601 } catch (NullPointerException e) {
1603 spt = asId.getStalepathTime();
1604 LOG.trace("BGP config/Stale-path time is not set using graceful");
1605 } catch (NullPointerException ignore) {
1606 LOG.trace("BGP AS id is not set using graceful");
1611 LOG.trace("BGP config/Stale-path time is not set using graceful/start-bgp");
1617 public static boolean isValidConfigBgpHostPort(String bgpHost, int bgpPort) {
1618 if (!bgpHost.equals(DEF_CHOST) && bgpPort != Integer.parseInt(DEF_CPORT)) {
1625 public synchronized void bgpRestarted() {
1627 * If there a thread which in the process of stale cleanup, cancel it
1628 * and start a new thread (to avoid processing same again).
1630 if (previousReplayJobInProgress()) {
1631 cancelPreviousReplayJob();
1633 Runnable task = () -> {
1635 LOG.info("running bgp replay task ");
1636 if (get() == null) {
1637 String host = getConfigHost();
1638 int port = getConfigPort();
1639 LOG.info("connecting to bgp host {} ", host);
1640 bgpRouter.connect(host, port);
1641 LOG.info("no config to push in bgp replay task ");
1644 setStaleStartTime(System.currentTimeMillis());
1645 LOG.info("started creating stale fibDSWriter map ");
1646 createStaleFibMap();
1647 setStaleEndTime(System.currentTimeMillis());
1648 LOG.info("took {} msecs for stale fibDSWriter map creation ", getStaleEndTime() - getStaleStartTime());
1649 LOG.info("started bgp config replay ");
1650 setCfgReplayStartTime(System.currentTimeMillis());
1651 boolean replaySucceded = replay();
1652 setCfgReplayEndTime(System.currentTimeMillis());
1653 LOG.info("took {} msecs for bgp replay ", getCfgReplayEndTime() - getCfgReplayStartTime());
1654 if (replaySucceded) {
1655 LOG.info("starting the stale cleanup timer");
1656 long routeSyncTime = getStalePathtime(BGP_RESTART_ROUTE_SYNC_SEC, config.getAsId());
1657 setStaleCleanupTime(routeSyncTime);
1658 routeCleanupFuture = executor.schedule(new RouteCleanup(), routeSyncTime, TimeUnit.SECONDS);
1660 staledFibEntriesMap.clear();
1662 } catch (InterruptedException | TimeoutException | ExecutionException eCancel) {
1663 LOG.error("Stale Cleanup Task Cancelled", eCancel);
1666 lastReplayJobFt = executor.submit(task);
1669 private boolean previousReplayJobInProgress() {
1670 return lastReplayJobFt != null && !lastReplayJobFt.isDone();
1673 private void cancelPreviousReplayJob() {
1675 LOG.error("cancelling already running bgp replay task");
1676 if (lastReplayJobFt != null) {
1677 lastReplayJobFt.cancel(true);
1678 lastReplayJobFt = null;
1679 staledFibEntriesMap.clear();
1681 if (routeCleanupFuture != null) {
1682 routeCleanupFuture.cancel(true);
1683 routeCleanupFuture = null;
1684 staledFibEntriesMap.clear();
1687 } catch (InterruptedException e) {
1688 LOG.error("Failed to cancel previous replay job ", e);
1692 private void doRouteSync() {
1693 LOG.error("Starting BGP route sync");
1695 bgpRouter.initRibSync(bgpSyncHandle);
1696 } catch (BgpRouterException e) {
1697 LOG.error("Route sync aborted, exception when initializing", e);
1700 while (bgpSyncHandle.getState() != BgpSyncHandle.DONE) {
1701 for (af_afi afi : af_afi.values()) {
1702 Routes routes = null;
1704 routes = bgpRouter.doRibSync(bgpSyncHandle, afi);
1705 } catch (TException | BgpRouterException e) {
1706 LOG.error("Route sync aborted, exception when syncing", e);
1709 Iterator<Update> updates = routes.getUpdatesIterator();
1710 while (updates.hasNext()) {
1711 Update update = updates.next();
1712 String rd = update.getRd();
1713 String nexthop = update.getNexthop();
1715 // TODO: decide correct label here
1716 int label = update.getL3label();
1717 int l2label = update.getL2label();
1719 String prefix = update.getPrefix();
1720 int plen = update.getPrefixlen();
1723 // TODO: protocol type will not be available in "update"
1724 // use "rd" to query vrf table and obtain the protocol_type.
1725 // Currently using PROTOCOL_EVPN as default.
1727 protocol_type.PROTOCOL_EVPN,
1732 update.getMacaddress(),
1735 update.getRoutermac(),
1741 LOG.error("Ending BGP route-sync");
1742 bgpRouter.endRibSync(bgpSyncHandle);
1743 } catch (BgpRouterException e) {
1748 public void addTepToElanDS(String rd, String tepIp, String mac, Long l2vni) {
1749 boolean needUpdate = addToRt2TepMap(rd, tepIp, mac, l2vni);
1751 LOG.info("Adding tepIp {} with RD {} to ELan DS", tepIp, rd);
1752 bgpUtil.addTepToElanInstance(rd, tepIp);
1754 LOG.debug("Skipping the Elan update for RT2 from tep {} rd {}", tepIp, rd);
1758 public void deleteTepfromElanDS(String rd, String tepIp, String mac) {
1759 boolean needUpdate = deleteFromRt2TepMap(rd, tepIp, mac);
1761 LOG.info("Deleting tepIp {} with RD {} to ELan DS", tepIp, rd);
1762 bgpUtil.deleteTepFromElanInstance(rd, tepIp);
1764 LOG.debug("Skipping the Elan update for RT2 withdraw from tep {} rd {}", tepIp, rd);
1768 /* onUpdatePushRoute
1769 * Get Stale fibDSWriter map, and compare current route/fibDSWriter entry.
1770 * - Entry compare shall include NextHop, Label.
1771 * - If entry matches: delete from STALE Map. NO Change to FIB Config DS.
1772 * - If entry not found, add to FIB Config DS.
1773 * - If entry found, but either Label/NextHop doesn't match.
1774 * - Update FIB Config DS with modified values.
1775 * - delete from Stale Map.
1778 public void onUpdatePushRoute(protocol_type protocolType, String rd, String prefix, int plen, String nextHop,
1779 String macaddress, int label, int l2label, String routermac, af_afi afi) {
1780 boolean addroute = false;
1781 boolean macupdate = false;
1783 VrfEntry.EncapType encapType = VrfEntry.EncapType.Mplsgre;
1784 if (protocolType.equals(protocol_type.PROTOCOL_EVPN)) {
1785 encapType = VrfEntry.EncapType.Vxlan;
1786 VpnInstanceOpDataEntry vpnInstanceOpDataEntry = bgpUtil.getVpnInstanceOpData(rd);
1787 if (vpnInstanceOpDataEntry != null) {
1788 if (vpnInstanceOpDataEntry.getType() == VpnInstanceOpDataEntry.Type.L2) {
1789 LOG.info("Got RT2 route for RD {} l3label {} l2label {} from tep {} with mac {} remote RD {}",
1790 vpnInstanceOpDataEntry.getVpnInstanceName(), label, l2label, nextHop, macaddress, rd);
1791 addTepToElanDS(rd, nextHop, macaddress, (long)l2label);
1794 l3vni = vpnInstanceOpDataEntry.getL3vni();
1797 LOG.error("No corresponding vpn instance found for rd {}. Aborting.", rd);
1802 if (!staledFibEntriesMap.isEmpty()) {
1803 // restart Scenario, as MAP is not empty.
1804 Map<String, Long> map = staledFibEntriesMap.get(rd);
1806 String prefixNextHop = appendNextHopToPrefix(prefix + "/" + plen, nextHop);
1807 Long labelInStaleMap = map.get(prefixNextHop);
1808 if (null == labelInStaleMap) {
1809 // New Entry, which happened to be added during restart.
1812 map.remove(prefixNextHop);
1813 if (isRouteModified(label, labelInStaleMap)) {
1814 LOG.debug("Route add ** {} ** {}/{} ** {} ** {} ", rd, prefix, plen, nextHop, label);
1815 // Existing entry, where in Label got modified during restart
1820 LOG.debug("rd {} map is null while processing prefix {} ", rd, prefix);
1824 LOG.debug("Route add ** {} ** {}/{} ** {} ** {} ", rd, prefix, plen, nextHop, label);
1828 LOG.info("ADD: Adding Mac Fib entry rd {} mac{} nexthop {} l2vni {}", rd, macaddress, nextHop, l2label);
1829 fibDSWriter.addMacEntryToDS(rd, macaddress, prefix, Collections.singletonList(nextHop),
1830 encapType, l2label, routermac, RouteOrigin.BGP);
1831 LOG.info("ADD: Added Mac Fib entry rd {} prefix {} nexthop {} label {}", rd, macaddress, nextHop, l2label);
1832 } else if (addroute) {
1833 LOG.info("ADD: Adding Fib entry rd {} prefix {} nexthop {} label {} afi {}",
1834 rd, prefix, nextHop, label, afi);
1835 // TODO: modify addFibEntryToDS signature
1836 List<String> nextHopList = Collections.singletonList(nextHop);
1837 fibDSWriter.addFibEntryToDS(rd, prefix + "/" + plen, nextHopList, encapType, label, l3vni,
1838 routermac, RouteOrigin.BGP);
1839 LOG.info("ADD: Added Fib entry rd {} prefix {} nexthop {} label {}", rd, prefix, nextHop, label);
1840 String vpnName = bgpUtil.getVpnNameFromRd(rd);
1841 if (vpnName != null) {
1842 vpnLinkService.leakRouteIfNeeded(vpnName, prefix, nextHopList, label, RouteOrigin.BGP,
1843 NwConstants.ADD_FLOW);
1848 public void onUpdateWithdrawRoute(protocol_type protocolType, String rd, String prefix, int plen, String nextHop,
1849 String macaddress) {
1851 boolean macupdate = false;
1852 if (protocolType.equals(protocol_type.PROTOCOL_EVPN)) {
1853 VpnInstanceOpDataEntry vpnInstanceOpDataEntry = bgpUtil.getVpnInstanceOpData(rd);
1854 if (vpnInstanceOpDataEntry != null) {
1855 vni = vpnInstanceOpDataEntry.getL3vni();
1856 if (vpnInstanceOpDataEntry.getType() == VpnInstanceOpDataEntry.Type.L2) {
1857 LOG.debug("Got RT2 withdraw for RD {} {} from tep {} with mac {} remote RD {}",
1858 vpnInstanceOpDataEntry.getVpnInstanceName(), vni, nextHop, macaddress, rd);
1859 deleteTepfromElanDS(rd, nextHop, macaddress);
1860 LOG.debug("For rd {}. skipping fib update", rd);
1864 LOG.error("No corresponding vpn instance found for rd {}. Aborting.", rd);
1869 LOG.info("Removing Mac Fib entry rd {} mac{} nexthop {} ", rd, macaddress, nextHop);
1870 fibDSWriter.removeMacEntryFromDS(rd, macaddress);
1871 LOG.info("Removed Mac Fib entry rd {} prefix {} nexthop {} ", rd, macaddress, nextHop);
1873 LOG.info("REMOVE: Removing Fib entry rd {} prefix {}", rd, prefix);
1874 fibDSWriter.removeOrUpdateFibEntryFromDS(rd, prefix + "/" + plen, nextHop);
1875 LOG.info("REMOVE: Removed Fib entry rd {} prefix {}", rd, prefix);
1879 //TODO: below function is for testing purpose with cli
1880 public void onUpdateWithdrawRoute(String rd, String prefix, int plen, String nexthop) {
1881 LOG.debug("Route del ** {} ** {}/{} ", rd, prefix, plen);
1882 fibDSWriter.removeOrUpdateFibEntryFromDS(rd, prefix + "/" + plen, nexthop);
1883 String vpnName = bgpUtil.getVpnNameFromRd(rd);
1884 if (vpnName != null) {
1885 vpnLinkService.leakRouteIfNeeded(vpnName, prefix, null /*nextHopList*/, 0 /*INVALID_LABEL*/,
1886 RouteOrigin.BGP, NwConstants.DEL_FLOW);
1890 private static boolean isRouteModified(int label, Long labelInStaleMap) {
1891 return labelInStaleMap != null && !labelInStaleMap.equals(Long.valueOf(label));
1894 static class ReplayNbr {
1896 boolean shouldRetry = false;
1898 public Neighbors getNbr() {
1902 public boolean isShouldRetry() {
1906 public void setShouldRetry(boolean retryNbr) {
1907 this.shouldRetry = retryNbr;
1910 ReplayNbr(Neighbors nbr, boolean shouldRetry) {
1912 this.shouldRetry = shouldRetry;
1916 private static boolean replayNbrConfig(List<Neighbors> neighbors, BgpRouter br) {
1917 if (neighbors == null || neighbors.isEmpty()) {
1918 LOG.error("Replaying nbr configuration, received NULL list ");
1922 List<ReplayNbr> replayNbrList = new ArrayList<>();
1923 for (Neighbors nbr : neighbors) {
1925 replayNbrList.add(new ReplayNbr(nbr, true));
1928 final int numberOfNbrRetries = 3;
1929 RetryOnException nbrRetry = new RetryOnException(numberOfNbrRetries);
1931 for (ReplayNbr replayNbr : replayNbrList) {
1932 if (!replayNbr.isShouldRetry()) {
1935 boolean replayDone = false;
1936 LOG.debug("Replaying addNbr {}", replayNbr.getNbr().getAddress().getValue());
1939 final String md5password = extractMd5Secret(replayNbr.getNbr());
1940 br.addNeighbor(replayNbr.getNbr().getAddress().getValue(),
1941 replayNbr.getNbr().getRemoteAs().longValue(), md5password);
1942 UpdateSource us = replayNbr.getNbr().getUpdateSource();
1944 LOG.debug("Replaying updatesource along with nbr: {} US-ip: {} to peer {}",
1945 replayNbr.getNbr().getAddress().getValue(),
1946 us.getSourceIp().getValue(),
1947 us.getPeerIp().getValue());
1948 br.addUpdateSource(us.getPeerIp().getValue(),
1949 us.getSourceIp().getValue());
1952 } catch (TException | BgpRouterException eNbr) {
1953 LOG.debug("Replaying addNbr {}, exception: ", replayNbr.getNbr().getAddress().getValue(), eNbr);
1955 boolean replaySuccess = true;
1956 replaySuccess = replaySuccess && replayDone;
1957 LOG.debug("Replay addNbr {} successful", replayNbr.getNbr().getAddress().getValue());
1959 //Update Source handling
1960 UpdateSource us = replayNbr.getNbr().getUpdateSource();
1961 if (replayDone == false && us != null) {
1962 LOG.debug("Replaying updatesource {} to peer {}", us.getSourceIp().getValue(),
1963 us.getPeerIp().getValue());
1966 br.addUpdateSource(us.getPeerIp().getValue(),
1967 us.getSourceIp().getValue());
1969 } catch (TException | BgpRouterException eUs) {
1970 LOG.debug("Replaying UpdateSource for Nbr {}, exception:",
1971 replayNbr.getNbr().getAddress().getValue(), eUs);
1973 LOG.debug("Replay updatesource {} successful", us.getSourceIp().getValue());
1974 replaySuccess = replaySuccess && replayDone;
1977 EbgpMultihop en = replayNbr.getNbr().getEbgpMultihop();
1981 br.addEbgpMultihop(en.getPeerIp().getValue(),
1982 en.getNhops().intValue());
1984 } catch (TException | BgpRouterException eEbgpMhop) {
1985 LOG.debug("Replaying EbgpMultihop for Nbr {}, exception: ",
1986 replayNbr.getNbr().getAddress().getValue(), eEbgpMhop);
1988 replaySuccess = replaySuccess && replayDone;
1992 List<AddressFamilies> afs = replayNbr.getNbr().getAddressFamilies();
1994 for (AddressFamilies af : afs) {
1995 af_afi afi = af_afi.findByValue(af.getAfi().intValue());
1996 af_safi safi = af_safi.findByValue(af.getSafi().intValue());
1999 br.addAddressFamily(af.getPeerIp().getValue(), afi, safi);
2001 } catch (TException | BgpRouterException eAFs) {
2002 LOG.debug("Replaying AddressFamily for Nbr {}, exception:",
2003 replayNbr.getNbr().getAddress().getValue(), eAFs);
2005 replaySuccess = replaySuccess && replayDone;
2008 //replay is success --> no need to replay this nbr in next iteration.
2009 replayNbr.setShouldRetry(replaySuccess ? false : true);
2011 } while (nbrRetry.decrementAndRetry());
2012 boolean replaySuccess = true;
2013 for (ReplayNbr replayNbr : replayNbrList) {
2014 replaySuccess = replaySuccess && !replayNbr.isShouldRetry();
2016 return replaySuccess;
2019 public String getConfigHost() {
2020 if (config == null) {
2023 ConfigServer ts = config.getConfigServer();
2024 return ts == null ? hostStartup : ts.getHost().getValue();
2027 public int getConfigPort() {
2028 if (config == null) {
2029 return Integer.parseInt(portStartup);
2031 ConfigServer ts = config.getConfigServer();
2032 return ts == null ? Integer.parseInt(portStartup) :
2033 ts.getPort().intValue();
2036 public Bgp getConfig() {
2037 AtomicInteger bgpDSretryCount = new AtomicInteger(DS_RETRY_COUNT);
2038 while (0 != bgpDSretryCount.decrementAndGet()) {
2040 return SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION,
2041 InstanceIdentifier.create(Bgp.class)).orNull();
2042 } catch (ReadFailedException e) {
2043 //Config DS may not be up, so sleep for 1 second and retry
2044 LOG.debug("failed to get bgp config, may be DS is yet in consistent state(?)", e);
2046 Thread.sleep(WAIT_TIME_BETWEEN_EACH_TRY_MILLIS);
2047 } catch (InterruptedException timerEx) {
2048 LOG.debug("WAIT_TIME_BETWEEN_EACH_TRY_MILLIS, Timer got interrupted while waiting for"
2049 + "config DS availability", timerEx);
2053 LOG.error("failed to get bgp config");
2057 @SuppressWarnings("checkstyle:IllegalCatch")
2058 public synchronized boolean replay() throws InterruptedException, TimeoutException, ExecutionException {
2059 boolean replaySucceded = true;
2060 String host = getConfigHost();
2061 int port = getConfigPort();
2062 LOG.error("connecting to bgp host {} ", host);
2063 boolean res = bgpRouter.connect(host, port);
2065 LOG.error("Cannot connect to BGP config server at {}:{}{}", host, port,
2066 config != null ? "; Configuration Replay aborted" : "");
2067 return replaySucceded;
2069 config = getConfig();
2070 if (config == null) {
2071 LOG.error("bgp config is empty nothing to push to bgp");
2072 return replaySucceded;
2074 BgpRouter br = bgpRouter;
2075 AsId asId = config.getAsId();
2077 LOG.error("bgp as-id is null");
2078 return replaySucceded;
2080 long asNum = asId.getLocalAs();
2081 IpAddress routerId = asId.getRouterId();
2082 String rid = routerId == null ? "" : new String(routerId.getValue());
2083 int stalepathTime = (int) getStalePathtime(RESTART_DEFAULT_GR, config.getAsId());
2084 boolean announceFbit = true;
2085 boolean replayDone = false;
2086 final int numberOfStartBgpRetries = 3;
2087 RetryOnException startBgpRetry = new RetryOnException(numberOfStartBgpRetries);
2090 LOG.debug("Replaying BGPConfig ");
2091 br.startBgp(asNum, rid, stalepathTime, announceFbit);
2092 LOG.debug("Replay BGPConfig successful");
2095 } catch (BgpRouterException bre) {
2096 if (bre.getErrorCode() == BgpRouterException.BGP_ERR_ACTIVE) {
2097 LOG.debug("Starting the routesync for exception", bre);
2098 startBgpRetry.errorOccured();
2099 if (!startBgpRetry.shouldRetry()) {
2100 LOG.debug("starting route sync for BgpRouter exception");
2104 LOG.error("Replay: startBgp() received exception error {} : ",
2105 bre.getErrorCode(), bre);
2106 startBgpRetry.errorOccured();
2108 } catch (TApplicationException tae) {
2109 if (tae.getType() == BgpRouterException.BGP_ERR_ACTIVE) {
2110 LOG.debug("Starting the routesync for exception", tae);
2111 startBgpRetry.errorOccured();
2112 if (!startBgpRetry.shouldRetry()) {
2113 LOG.debug("starting route sync for Thrift BGP_ERR_ACTIVE exception");
2116 } else if (tae.getType() == BgpRouterException.BGP_ERR_COMMON_FAILURE) {
2117 LOG.debug("Starting the routesync for AS-ID started exception", tae);
2118 startBgpRetry.errorOccured();
2119 if (!startBgpRetry.shouldRetry()) {
2120 LOG.debug("starting route sync for Thrift BGP_ERR_COMMON_FAILURE exception");
2124 LOG.error("Replay: startBgp() received exception type {}: ",
2125 tae.getType(), tae);
2126 startBgpRetry.errorOccured();
2128 } catch (Exception e) {
2129 //not unusual. We may have restarted & BGP is already on
2130 LOG.error("Replay:startBgp() received exception: ", e);
2131 startBgpRetry.errorOccured();
2133 } while (startBgpRetry.shouldRetry());
2135 replaySucceded = replayDone;
2137 startBgpCountersTask();
2138 startBgpAlarmsTask();
2141 * commenting this due to a bug with QBGP. Will uncomment once QBGP fix is done.
2142 * This wont have any functional impacts
2145 // br.delayEOR(delayEorSeconds);
2146 //} catch (TException | BgpRouterException e) {
2147 // LOG.error("Replay: delayEOR() number of seconds to wait for EOR from ODL:", e);
2150 List<Neighbors> neighbors = config.getNeighbors();
2151 if (neighbors != null) {
2152 LOG.error("configuring existing Neighbors present for replay total neighbors {}", neighbors.size());
2153 boolean neighborConfigReplayResult = replayNbrConfig(neighbors, br);
2154 if (neighborConfigReplayResult == false) {
2155 replaySucceded = false;
2158 LOG.error("no Neighbors present for replay config ");
2161 Logging logging = config.getLogging();
2162 if (logging != null) {
2164 br.setLogging(logging.getFile(), logging.getLevel());
2165 } catch (TException | BgpRouterException e) {
2166 LOG.error("Replay:setLogging() received exception", e);
2170 GracefulRestart gracefulRestart = config.getGracefulRestart();
2171 if (gracefulRestart != null) {
2173 br.addGracefulRestart(gracefulRestart.getStalepathTime().intValue());
2174 } catch (TException | BgpRouterException e) {
2175 LOG.error("Replay:addGr() received exception", e);
2179 List<Vrfs> vrfs = config.getVrfs();
2181 vrfs = new ArrayList<>();
2183 for (Vrfs vrf : vrfs) {
2184 for (AddressFamiliesVrf adf : vrf.getAddressFamiliesVrf()) {
2186 br.addVrf(BgpUtil.getLayerType(adf), vrf.getRd(), vrf.getImportRts(),
2187 vrf.getExportRts());
2188 } catch (TException | BgpRouterException e) {
2189 LOG.error("Replay:addVrf() received exception", e);
2194 List<Networks> ln = config.getNetworks();
2196 for (Networks net : ln) {
2197 String rd = net.getRd();
2198 String pfxlen = net.getPrefixLen();
2199 String nh = net.getNexthop().getValue();
2200 Long label = net.getLabel();
2201 int lbl = label == null ? 0 : label.intValue();
2202 int l3vni = net.getL3vni() == null ? 0 : net.getL3vni().intValue();
2203 int l2vni = net.getL2vni() == null ? 0 : net.getL2vni().intValue();
2204 if (rd == null && lbl > 0) {
2205 //LU prefix is being deleted.
2206 rd = Integer.toString(lbl);
2209 BgpControlPlaneType protocolType = net.getBgpControlPlaneType();
2210 int ethernetTag = net.getEthtag().intValue();
2211 String esi = net.getEsi();
2212 String macaddress = net.getMacaddress();
2213 EncapType encapType = net.getEncapType();
2214 String routerMac = net.getRoutermac();
2217 br.addPrefix(rd, pfxlen, nh, lbl, l3vni, l2vni,
2218 BgpUtil.convertToThriftProtocolType(protocolType),
2219 ethernetTag, esi, macaddress, BgpUtil.convertToThriftEncapType(encapType), routerMac);
2220 } catch (TException | BgpRouterException e) {
2221 LOG.error("Replay:addPfx() received exception", e);
2227 List<Multipath> multipaths = config.getMultipath();
2229 if (multipaths != null) {
2230 for (Multipath multipath : multipaths) {
2231 if (multipath != null) {
2232 af_afi afi = af_afi.findByValue(multipath.getAfi().intValue());
2233 af_safi safi = af_safi.findByValue(multipath.getSafi().intValue());
2236 if (multipath.isMultipathEnabled()) {
2237 br.enableMultipath(afi, safi);
2239 br.disableMultipath(afi, safi);
2241 } catch (TException | BgpRouterException e) {
2242 LOG.info("Replay:multipaths() received exception", e);
2247 List<VrfMaxpath> vrfMaxpaths = config.getVrfMaxpath();
2248 if (vrfMaxpaths != null) {
2249 for (VrfMaxpath vrfMaxpath : vrfMaxpaths) {
2251 br.multipaths(vrfMaxpath.getRd(), vrfMaxpath.getMaxpaths());
2252 } catch (TException | BgpRouterException e) {
2253 LOG.info("Replay:vrfMaxPath() received exception", e);
2258 //send End of Rib Marker to Qthriftd.
2259 final int numberOfEORRetries = 3;
2261 RetryOnException eorRetry = new RetryOnException(numberOfEORRetries);
2265 LOG.debug("Replay sendEOR {} successful");
2268 } catch (Exception e) {
2269 eorRetry.errorOccured();
2270 LOG.error("Replay:sedEOR() received exception:", e);
2272 } while (eorRetry.shouldRetry());
2274 return replaySucceded && replayDone;
2277 private <T extends DataObject> void update(InstanceIdentifier<T> iid, T dto) {
2278 bgpUtil.update(iid, dto);
2281 private <T extends DataObject> void delete(InstanceIdentifier<T> iid) {
2282 bgpUtil.delete(iid);
2285 public void startConfig(String bgpHost, int thriftPort) {
2286 InstanceIdentifier.InstanceIdentifierBuilder<ConfigServer> iib =
2287 InstanceIdentifier.builder(Bgp.class).child(ConfigServer.class);
2288 InstanceIdentifier<ConfigServer> iid = iib.build();
2289 Ipv4Address ipAddr = new Ipv4Address(bgpHost);
2290 ConfigServer dto = new ConfigServerBuilder().setHost(ipAddr)
2291 .setPort((long) thriftPort).build();
2295 public void startBgp(long as, String routerId, int spt, boolean fbit) {
2296 IpAddress rid = routerId == null ? null : new IpAddress(routerId.toCharArray());
2297 Long staleTime = (long) spt;
2298 InstanceIdentifier.InstanceIdentifierBuilder<AsId> iib =
2299 InstanceIdentifier.builder(Bgp.class).child(AsId.class);
2300 InstanceIdentifier<AsId> iid = iib.build();
2301 AsId dto = new AsIdBuilder().setLocalAs(as)
2303 .setStalepathTime(staleTime)
2304 .setAnnounceFbit(fbit).build();
2308 public void addLogging(String fileName, String logLevel) {
2309 InstanceIdentifier.InstanceIdentifierBuilder<Logging> iib =
2310 InstanceIdentifier.builder(Bgp.class).child(Logging.class);
2311 InstanceIdentifier<Logging> iid = iib.build();
2312 Logging dto = new LoggingBuilder().setFile(fileName)
2313 .setLevel(logLevel).build();
2317 public void addGracefulRestart(int staleTime) {
2318 InstanceIdentifier.InstanceIdentifierBuilder<GracefulRestart> iib =
2319 InstanceIdentifier.builder(Bgp.class).child(GracefulRestart.class);
2320 InstanceIdentifier<GracefulRestart> iid = iib.build();
2321 GracefulRestart dto = new GracefulRestartBuilder()
2322 .setStalepathTime((long) staleTime).build();
2326 public void addNeighbor(
2327 String nbrIp, long remoteAs, @Nullable final TcpMd5SignaturePasswordType md5Secret) {
2328 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2329 InstanceIdentifier.InstanceIdentifierBuilder<Neighbors> iib =
2330 InstanceIdentifier.builder(Bgp.class)
2331 .child(Neighbors.class, new NeighborsKey(nbrAddr));
2332 InstanceIdentifier<Neighbors> iid = iib.build();
2333 TcpSecurityOption tcpSecOption = null;
2334 if (md5Secret != null) {
2335 tcpSecOption = new TcpMd5SignatureOptionBuilder().setTcpMd5SignaturePassword(md5Secret).build();
2336 } // else let tcpSecOption be null
2337 Neighbors dto = new NeighborsBuilder().setAddress(nbrAddr)
2338 .setRemoteAs(remoteAs).setTcpSecurityOption(tcpSecOption).build();
2340 } // public addNeighbor(nbrIp, remoteAs, md5Secret)
2342 public void addUpdateSource(String nbrIp, String srcIp) {
2343 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2344 Ipv4Address srcAddr = new Ipv4Address(srcIp);
2345 InstanceIdentifier.InstanceIdentifierBuilder<UpdateSource> iib =
2346 InstanceIdentifier.builder(Bgp.class)
2347 .child(Neighbors.class, new NeighborsKey(nbrAddr))
2348 .child(UpdateSource.class);
2349 InstanceIdentifier<UpdateSource> iid = iib.build();
2350 UpdateSource dto = new UpdateSourceBuilder().setPeerIp(nbrAddr)
2351 .setSourceIp(srcAddr).build();
2355 public void addEbgpMultihop(String nbrIp, int hops) {
2356 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2357 InstanceIdentifier.InstanceIdentifierBuilder<EbgpMultihop> iib =
2358 InstanceIdentifier.builder(Bgp.class)
2359 .child(Neighbors.class, new NeighborsKey(nbrAddr))
2360 .child(EbgpMultihop.class);
2361 InstanceIdentifier<EbgpMultihop> iid = iib.build();
2362 EbgpMultihop dto = new EbgpMultihopBuilder().setPeerIp(nbrAddr)
2363 .setNhops((long) hops).build();
2367 public void addAddressFamily(String nbrIp, int afi, int safi) {
2368 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2369 InstanceIdentifier.InstanceIdentifierBuilder<AddressFamilies> iib =
2370 InstanceIdentifier.builder(Bgp.class)
2371 .child(Neighbors.class, new NeighborsKey(nbrAddr))
2372 .child(AddressFamilies.class, new AddressFamiliesKey((long) afi, (long) safi));
2373 InstanceIdentifier<AddressFamilies> iid = iib.build();
2374 AddressFamilies dto = new AddressFamiliesBuilder().setPeerIp(nbrAddr)
2375 .setAfi((long) afi).setSafi((long) safi).build();
2379 public void addPrefix(String rd, String macAddress, String pfx, List<String> nhList,
2380 VrfEntry.EncapType encapType, long lbl, long l3vni, long l2vni, String gatewayMac) {
2381 for (String nh : nhList) {
2382 Ipv4Address nexthop = nh != null ? new Ipv4Address(nh) : null;
2384 InstanceIdentifier<Networks> iid = InstanceIdentifier.builder(Bgp.class)
2385 .child(Networks.class, new NetworksKey(pfx, rd)).build();
2386 NetworksBuilder networksBuilder = new NetworksBuilder().setRd(rd).setPrefixLen(pfx).setNexthop(nexthop)
2387 .setLabel(label).setEthtag(BgpConstants.DEFAULT_ETH_TAG);
2388 buildVpnEncapSpecificInfo(networksBuilder, encapType, label, l3vni, l2vni, macAddress, gatewayMac);
2389 update(iid, networksBuilder.build());
2393 private static void buildVpnEncapSpecificInfo(NetworksBuilder builder, VrfEntry.EncapType encapType, long label,
2394 long l3vni, long l2vni, String macAddress, String gatewayMac) {
2395 if (encapType.equals(VrfEntry.EncapType.Mplsgre)) {
2396 builder.setLabel(label).setBgpControlPlaneType(BgpControlPlaneType.PROTOCOLL3VPN)
2397 .setEncapType(EncapType.GRE);
2399 builder.setL3vni(l3vni).setL2vni(l2vni).setMacaddress(macAddress).setRoutermac(gatewayMac)
2400 .setBgpControlPlaneType(BgpControlPlaneType.PROTOCOLEVPN).setEncapType(EncapType.VXLAN);
2404 // TODO: add LayerType as arg - supports command
2405 public void addVrf(String rd, List<String> irts, List<String> erts, AddressFamily addressFamily) {
2406 Vrfs vrf = bgpUtil.getVrfFromRd(rd);
2407 List<AddressFamiliesVrf> adfList = new ArrayList<>(1);
2409 adfList = vrf.getAddressFamiliesVrf();
2411 AddressFamiliesVrfBuilder adfBuilder = new AddressFamiliesVrfBuilder();
2412 if (addressFamily.equals(AddressFamily.IPV4)) {
2413 adfBuilder.setAfi((long) af_afi.AFI_IP.getValue());
2414 adfBuilder.setSafi((long) af_safi.SAFI_MPLS_VPN.getValue());
2415 } else if (addressFamily.equals(AddressFamily.IPV6)) {
2416 adfBuilder.setAfi((long) af_afi.AFI_IPV6.getValue());
2417 adfBuilder.setSafi((long) af_safi.SAFI_MPLS_VPN.getValue());
2418 } else if (addressFamily.equals(AddressFamily.L2VPN)) {
2419 adfBuilder.setAfi((long) af_afi.AFI_IP.getValue());
2420 adfBuilder.setSafi((long) af_safi.SAFI_EVPN.getValue());
2422 AddressFamiliesVrf adf = adfBuilder.build();
2424 InstanceIdentifier.InstanceIdentifierBuilder<Vrfs> iib = InstanceIdentifier.builder(Bgp.class)
2425 .child(Vrfs.class, new VrfsKey(rd));
2426 InstanceIdentifier<Vrfs> iid = iib.build();
2427 Vrfs dto = new VrfsBuilder().setRd(rd).setImportRts(irts)
2428 .setExportRts(erts).setAddressFamiliesVrf(adfList).build();
2430 List<AddressFamiliesVrf> listAdFamilies = mapNewAdFamily.get(rd);
2431 if (listAdFamilies != null) {
2432 listAdFamilies.add(adf);
2434 mapNewAdFamily.put(rd, adfList);
2438 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, iid, dto);
2439 } catch (TransactionCommitFailedException e) {
2440 LOG.error("Error adding VRF to datastore", e);
2441 throw new RuntimeException(e);
2445 public void stopConfig() {
2446 InstanceIdentifier.InstanceIdentifierBuilder<ConfigServer> iib =
2447 InstanceIdentifier.builder(Bgp.class).child(ConfigServer.class);
2448 InstanceIdentifier<ConfigServer> iid = iib.build();
2452 public void stopBgp() {
2453 InstanceIdentifier.InstanceIdentifierBuilder<AsId> iib =
2454 InstanceIdentifier.builder(Bgp.class).child(AsId.class);
2455 InstanceIdentifier<AsId> iid = iib.build();
2459 public void delLogging() {
2460 InstanceIdentifier.InstanceIdentifierBuilder<Logging> iib =
2461 InstanceIdentifier.builder(Bgp.class).child(Logging.class);
2462 InstanceIdentifier<Logging> iid = iib.build();
2466 public void delGracefulRestart() {
2467 InstanceIdentifier.InstanceIdentifierBuilder<GracefulRestart> iib =
2468 InstanceIdentifier.builder(Bgp.class)
2469 .child(GracefulRestart.class);
2470 InstanceIdentifier<GracefulRestart> iid = iib.build();
2474 public void delNeighbor(String nbrIp) {
2475 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2476 InstanceIdentifier.InstanceIdentifierBuilder<Neighbors> iib =
2477 InstanceIdentifier.builder(Bgp.class)
2478 .child(Neighbors.class, new NeighborsKey(nbrAddr));
2479 InstanceIdentifier<Neighbors> iid = iib.build();
2483 public void delUpdateSource(String nbrIp) {
2484 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2485 InstanceIdentifier.InstanceIdentifierBuilder<UpdateSource> iib =
2486 InstanceIdentifier.builder(Bgp.class)
2487 .child(Neighbors.class, new NeighborsKey(nbrAddr))
2488 .child(UpdateSource.class);
2489 InstanceIdentifier<UpdateSource> iid = iib.build();
2493 public void delEbgpMultihop(String nbrIp) {
2494 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2495 InstanceIdentifier.InstanceIdentifierBuilder<EbgpMultihop> iib =
2496 InstanceIdentifier.builder(Bgp.class)
2497 .child(Neighbors.class, new NeighborsKey(nbrAddr))
2498 .child(EbgpMultihop.class);
2499 InstanceIdentifier<EbgpMultihop> iid = iib.build();
2503 public void delAddressFamily(String nbrIp, int afi, int safi) {
2504 Ipv4Address nbrAddr = new Ipv4Address(nbrIp);
2505 InstanceIdentifier.InstanceIdentifierBuilder<AddressFamilies> iib =
2506 InstanceIdentifier.builder(Bgp.class)
2507 .child(Neighbors.class, new NeighborsKey(nbrAddr))
2508 .child(AddressFamilies.class, new AddressFamiliesKey((long) afi, (long) safi));
2509 InstanceIdentifier<AddressFamilies> iid = iib.build();
2513 public void delPrefix(String rd, String pfx) {
2514 InstanceIdentifier.InstanceIdentifierBuilder<Networks> iib =
2515 InstanceIdentifier.builder(Bgp.class)
2516 .child(Networks.class, new NetworksKey(pfx, rd));
2517 InstanceIdentifier<Networks> iid = iib.build();
2521 public boolean delVrf(String rd, AddressFamily addressFamily) {
2522 if (addressFamily == null) {
2523 LOG.error("delVrf: vrf {}, addressFamily invalid", rd);
2526 AddressFamiliesVrfBuilder adfBuilder = new AddressFamiliesVrfBuilder();
2527 if (addressFamily.equals(AddressFamily.IPV4)) {
2528 adfBuilder.setAfi((long) af_afi.AFI_IP.getValue());
2529 adfBuilder.setSafi((long) af_safi.SAFI_MPLS_VPN.getValue());
2530 } else if (addressFamily.equals(AddressFamily.IPV6)) {
2531 adfBuilder.setAfi((long) af_afi.AFI_IPV6.getValue());
2532 adfBuilder.setSafi((long) af_safi.SAFI_MPLS_VPN.getValue());
2533 } else if (addressFamily.equals(AddressFamily.L2VPN)) {
2534 adfBuilder.setAfi((long) af_afi.AFI_IP.getValue());
2535 adfBuilder.setSafi((long) af_safi.SAFI_EVPN.getValue());
2537 Vrfs vrfOriginal = bgpUtil.getVrfFromRd(rd);
2538 if (vrfOriginal == null) {
2539 LOG.error("delVrf: no vrf with existing rd {}. step aborted", rd);
2543 InstanceIdentifier.InstanceIdentifierBuilder<Vrfs> iib =
2544 InstanceIdentifier.builder(Bgp.class)
2545 .child(Vrfs.class, new VrfsKey(rd));
2547 InstanceIdentifier<Vrfs> iid = iib.build();
2549 @SuppressWarnings("static-access")
2550 InstanceIdentifier<Bgp> iid6 = iid.builder(Bgp.class).build()
2551 .child(Multipath.class, new MultipathKey(adfBuilder.getAfi(), adfBuilder.getSafi())).create(Bgp.class);
2552 InstanceIdentifierBuilder<Vrfs> iib3 = iid6.child(Vrfs.class, new VrfsKey(rd)).builder();
2553 InstanceIdentifier<Vrfs> iidFinal = iib3.build();
2555 //** update or delete the vrfs with the rest of AddressFamilies already present in the last list
2556 AddressFamiliesVrf adfToDel = adfBuilder.build();
2557 List<AddressFamiliesVrf> adfListOriginal = vrfOriginal.getAddressFamiliesVrf() == null
2558 ? new ArrayList<>() : vrfOriginal.getAddressFamiliesVrf();
2559 List<AddressFamiliesVrf> adfListToRemoveFromOriginal = new ArrayList<>();
2560 adfListOriginal.forEach(adf -> {
2561 if (adf.equals(adfToDel)) {
2562 adfListToRemoveFromOriginal.add(adfToDel);
2566 for (AddressFamiliesVrf adfToRemove : adfListToRemoveFromOriginal) {
2567 adfListOriginal.remove(adfToRemove);
2569 SingleTransactionDataBroker.syncWrite(dataBroker,
2570 LogicalDatastoreType.CONFIGURATION, iid, vrfOriginal);
2571 } catch (TransactionCommitFailedException e) {
2572 LOG.error("delVrf: Error updating VRF to datastore", e);
2573 throw new RuntimeException(e);
2576 if (adfListOriginal.isEmpty()) {
2580 // not all is removed
2584 public void setMultipathStatus(af_afi afi, af_safi safi, boolean enable) {
2585 long lafi = afi.getValue();
2586 long lsafi = safi.getValue();
2588 InstanceIdentifier.InstanceIdentifierBuilder<Multipath> iib =
2591 .child(Multipath.class,
2592 new MultipathKey(Long.valueOf(afi.getValue()), Long.valueOf(safi.getValue())));
2594 Multipath dto = new MultipathBuilder().setAfi(lafi).setSafi(lsafi).setMultipathEnabled(enable).build();
2595 update(iib.build(), dto);
2598 public void multipaths(String rd, int maxpath) {
2599 InstanceIdentifier.InstanceIdentifierBuilder<VrfMaxpath> iib =
2602 .child(VrfMaxpath.class, new VrfMaxpathKey(rd));
2604 VrfMaxpath dto = new VrfMaxpathBuilder().setRd(rd).setMaxpaths(maxpath).build();
2605 update(iib.build(), dto);
2609 * Remove Stale Marked Routes after timer expiry.
2611 private class RouteCleanup implements Callable<Integer> {
2614 public Integer call() {
2617 if (staledFibEntriesMap.isEmpty()) {
2618 LOG.info("BGP: RouteCleanup timertask tirggered but STALED FIB MAP is EMPTY");
2620 for (String rd : staledFibEntriesMap.keySet()) {
2621 if (Thread.interrupted()) {
2624 Map<String, Long> map = staledFibEntriesMap.get(rd);
2626 for (String key : map.keySet()) {
2627 if (Thread.interrupted()) {
2630 String prefix = extractPrefix(key);
2631 String nextHop = extractNextHop(key);
2633 LOG.debug("BGP: RouteCleanup deletePrefix called for : rd:{}, prefix{}, nextHop:{}",
2634 rd, prefix, nextHop);
2635 fibDSWriter.removeOrUpdateFibEntryFromDS(rd, prefix, nextHop);
2641 staledFibEntriesMap.clear();
2643 LOG.error("cleared {} stale routes after bgp restart", totalCleared);
2649 * BGP restart scenario, ODL-BGP manager was/is running.
2650 * On re-sync notification, Get a copy of FIB database.
2652 public void createStaleFibMap() {
2653 totalStaledCount = 0;
2655 staledFibEntriesMap.clear();
2656 InstanceIdentifier<FibEntries> id = InstanceIdentifier.create(FibEntries.class);
2658 Optional<FibEntries> fibEntries = SingleTransactionDataBroker.syncReadOptional(dataBroker,
2659 LogicalDatastoreType.CONFIGURATION, id);
2660 if (fibEntries.isPresent()) {
2661 List<VrfTables> staleVrfTables = fibEntries.get().getVrfTables();
2662 for (VrfTables vrfTable : staleVrfTables) {
2663 Map<String, Long> staleFibEntMap = new HashMap<>();
2664 for (VrfEntry vrfEntry : vrfTable.getVrfEntry()) {
2665 if (RouteOrigin.value(vrfEntry.getOrigin()) != RouteOrigin.BGP) {
2666 //Stale marking and cleanup is only meant for the routes learned through BGP.
2669 if (Thread.interrupted()) {
2673 //Create MAP from staleVrfTables.
2674 vrfEntry.getRoutePaths()
2676 routePath -> staleFibEntMap.put(
2677 appendNextHopToPrefix(vrfEntry.getDestPrefix(),
2678 routePath.getNexthopAddress()), routePath.getLabel()));
2680 staledFibEntriesMap.put(vrfTable.getRouteDistinguisher(), staleFibEntMap);
2683 LOG.error("createStaleFibMap:: FIBentries.class is not present");
2685 } catch (ReadFailedException e) {
2686 LOG.error("createStaleFibMap:: error ", e);
2688 LOG.error("created {} staled entries ", totalStaledCount);
2692 * BGP config remove scenario, Need to remove all the
2693 * external routes from FIB.
2695 public void deleteExternalFibRoutes() {
2696 totalExternalRoutes = 0;
2697 totalExternalMacRoutes = 0;
2699 InstanceIdentifier<FibEntries> id = InstanceIdentifier.create(FibEntries.class);
2701 Optional<FibEntries> fibEntries = SingleTransactionDataBroker.syncReadOptional(dataBroker,
2702 LogicalDatastoreType.CONFIGURATION, id);
2703 if (fibEntries.isPresent()) {
2704 if (fibEntries.get().getVrfTables() == null) {
2705 LOG.error("deleteExternalFibRoutes::getVrfTables is null");
2708 List<VrfTables> staleVrfTables = fibEntries.get().getVrfTables();
2709 for (VrfTables vrfTable : staleVrfTables) {
2710 String rd = vrfTable.getRouteDistinguisher();
2711 if (vrfTable.getVrfEntry() != null) {
2712 for (VrfEntry vrfEntry : vrfTable.getVrfEntry()) {
2713 if (RouteOrigin.value(vrfEntry.getOrigin()) != RouteOrigin.BGP) {
2714 //route cleanup is only meant for the routes learned through BGP.
2717 totalExternalRoutes++;
2718 fibDSWriter.removeFibEntryFromDS(rd, vrfEntry.getDestPrefix());
2720 } else if (vrfTable.getMacVrfEntry() != null) {
2721 for (MacVrfEntry macEntry : vrfTable.getMacVrfEntry()) {
2722 if (RouteOrigin.value(macEntry.getOrigin()) != RouteOrigin.BGP) {
2723 //route cleanup is only meant for the routes learned through BGP.
2726 totalExternalMacRoutes++;
2727 fibDSWriter.removeMacEntryFromDS(rd, macEntry.getMac());
2732 LOG.error("deleteExternalFibRoutes:: FIBentries.class is not present");
2734 } catch (ReadFailedException e) {
2735 LOG.error("deleteExternalFibRoutes:: error ", e);
2737 LOG.debug("deleted {} fib entries {} mac entries", totalExternalRoutes, totalExternalMacRoutes);
2740 public boolean addToRt2TepMap(String rd, String tepIp, String mac, Long l2vni) {
2741 boolean isFirstMacUpdateFromTep = false;
2742 if (rt2TepMap.containsKey(rd)) {
2743 if (rt2TepMap.get(rd).containsKey(tepIp)) {
2744 LOG.debug("RT2 with mac {} l2vni {} from existing rd {} and tep-ip {}. No Elan DS write required",
2745 mac, l2vni, rd, tepIp);
2746 rt2TepMap.get(rd).get(tepIp).put(mac, l2vni);
2748 LOG.debug("RT2 with mac {} l2vni {} from existing rd {} and new tep-ip {}",
2749 mac, l2vni, rd, tepIp);
2750 isFirstMacUpdateFromTep = true;
2751 Map<String, Long> macList = new HashMap<>();
2752 macList.put(mac, l2vni);
2753 rt2TepMap.get(rd).put(tepIp, macList);
2756 LOG.debug("RT2 with mac {} l2vni {} from new rd {} and tep ip {}",
2757 mac, l2vni, rd, tepIp);
2758 isFirstMacUpdateFromTep = true;
2759 Map<String, Long> macList = new HashMap<>();
2760 macList.put(mac, l2vni);
2761 Map<String, Map<String, Long>> tepIpMacMap = new HashMap<>();
2762 tepIpMacMap.put(tepIp, macList);
2763 rt2TepMap.put(rd, tepIpMacMap);
2765 return isFirstMacUpdateFromTep;
2768 public boolean deleteFromRt2TepMap(String rd, String tepIp, String mac) {
2769 boolean isLastMacUpdateFromTep = false;
2770 LOG.debug("RT2 withdraw with rd {} mac {} tep-ip {} ", rd, mac, tepIp);
2771 if (rt2TepMap.containsKey(rd)) {
2772 if (rt2TepMap.get(rd).containsKey(tepIp)) {
2773 if (rt2TepMap.get(rd).get(tepIp).containsKey(mac)) {
2774 LOG.debug("RT2 Withdraw : Removing the mac {} from Map", mac);
2775 rt2TepMap.get(rd).get(tepIp).remove(mac);
2776 if (rt2TepMap.get(rd).get(tepIp).isEmpty()) {
2777 isLastMacUpdateFromTep = true;
2778 LOG.debug("RT2 Withdraw : Removing the tep-ip {} from Map", tepIp);
2779 rt2TepMap.get(rd).remove(tepIp);
2780 if (rt2TepMap.get(rd).isEmpty()) {
2781 LOG.debug("RT2 Withdraw : Removing the rd {} from Map", rd);
2782 rt2TepMap.remove(rd);
2788 return isLastMacUpdateFromTep;
2791 public Collection<String> getTepIPs(String rd) {
2792 final Map<String, Map<String, Long>> tepIpMap = rt2TepMap.get(rd);
2793 return tepIpMap != null ? tepIpMap.keySet() : Collections.emptyList();
2796 public boolean isBgpConnected() {
2797 return bgpRouter.isBgpConnected();
2800 public long getLastConnectedTS() {
2801 return bgpRouter.getLastConnectedTS();
2804 public long getConnectTS() {
2805 return bgpRouter.getConnectTS();
2808 public long getStartTS() {
2809 return bgpRouter.getStartTS();
2812 public TTransport getTransport() {
2813 return bgpRouter.getTransport();
2816 public int getTotalStaledCount() {
2817 return totalStaledCount;
2820 public int getTotalCleared() {
2821 return totalCleared;
2824 public BgpCounters getBgpCounters() {
2825 return bgpCountersReference.get();
2828 private void startBgpCountersTask() {
2829 if (getBgpCounters() == null && bgpCountersReference.compareAndSet(null,
2830 new BgpCounters(getBgpSdncMipIp(), metricProvider))) {
2831 bgpCountersTask = executor.scheduleAtFixedRate(bgpCountersReference.get(), 0, 120 * 1000,
2832 TimeUnit.MILLISECONDS);
2833 LOG.info("Bgp Counters task scheduled for every two minutes.");
2835 bgpManager.setQbgpLog(BgpConstants.BGP_DEF_LOG_FILE, BgpConstants.BGP_DEF_LOG_LEVEL);
2839 private void stopBgpCountersTask() {
2840 final BgpCounters bgpCounters = bgpCountersReference.getAndSet(null);
2841 if (bgpCounters != null) {
2842 bgpCountersTask.cancel(true);
2843 bgpCounters.close();
2847 private void startBgpAlarmsTask() {
2848 if (getBgpAlarms() == null && bgpAlarmsReference.compareAndSet(null, new BgpAlarms(this))) {
2849 bgpAlarmsReference.get().init();
2850 bgpAlarmsTask = executor.scheduleAtFixedRate(bgpAlarmsReference.get(), 0, 60 * 1000, TimeUnit.MILLISECONDS);
2851 LOG.info("Bgp Alarms task scheduled for every minute.");
2855 private void stopBgpAlarmsTask() {
2856 final BgpAlarms bgpAlarms = bgpAlarmsReference.getAndSet(null);
2857 if (bgpAlarms != null) {
2858 bgpAlarmsTask.cancel(true);
2863 public BgpAlarms getBgpAlarms() {
2864 return bgpAlarmsReference.get();
2867 private static String appendNextHopToPrefix(String prefix, String nextHop) {
2868 return prefix + ":" + nextHop;
2871 private static String extractPrefix(String prefixNextHop) {
2872 return prefixNextHop.split(":")[0];
2875 private static String extractNextHop(String prefixNextHop) {
2876 return prefixNextHop.split(":")[1];
2879 private static String extractMd5Secret(final Neighbors val) {
2880 String md5Secret = null;
2881 TcpSecurityOption tcpSecOpt = val.getTcpSecurityOption();
2882 if (tcpSecOpt != null) {
2883 if (tcpSecOpt instanceof TcpMd5SignatureOption) {
2884 md5Secret = ((TcpMd5SignatureOption) tcpSecOpt).getTcpMd5SignaturePassword().getValue();
2885 } else { // unknown TcpSecurityOption
2886 LOG.debug("neighbors Ignored unknown tcp-security-option of peer {}", val.getAddress().getValue());
2890 } // private method extractMd5Secret