2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl.config;
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil.INSTANCE;
13 import com.google.common.collect.ImmutableList;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Optional;
21 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
25 import org.opendaylight.protocol.bgp.mode.impl.add.all.paths.AllPathSelection;
26 import org.opendaylight.protocol.bgp.mode.impl.add.n.paths.AddPathBestNPathSelection;
27 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
28 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandlingSupport;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.RevisedErrorHandlingSupportImpl;
30 import org.opendaylight.protocol.concepts.KeyMapping;
31 import org.opendaylight.protocol.util.Ipv4Util;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.BgpCommonAfiSafiList;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborAddPathsConfig;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborGroup;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborTransportConfig;
38 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.graceful.restart.GracefulRestart;
39 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.ErrorHandling;
40 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.RouteReflector;
41 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Timers;
42 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Transport;
43 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.transport.Config;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborKey;
46 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroup;
47 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
48 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
49 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
50 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.PeerType;
51 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.add.path.capability.AddressFamilies;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.add.path.capability.AddressFamiliesBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAddPathsConfig;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalConfigAugmentation;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAddPathsConfig;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborPeerGroupConfig;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTransportConfig;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupTransportConfig;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.TransportConfig;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
73 import org.opendaylight.yangtools.yang.binding.Augmentation;
74 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
75 import org.opendaylight.yangtools.yang.common.Uint16;
77 final class OpenConfigMappingUtil {
78 static final String APPLICATION_PEER_GROUP_NAME = "application-peers";
79 static final Optional<String> APPLICATION_PEER_GROUP_NAME_OPT = Optional.of(APPLICATION_PEER_GROUP_NAME);
80 static final int HOLDTIMER = 90;
81 private static final AfiSafi IPV4_AFISAFI = new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class).build();
82 private static final List<AfiSafi> DEFAULT_AFISAFI = ImmutableList.of(IPV4_AFISAFI);
83 private static final int CONNECT_RETRY = 30;
84 private static final PortNumber PORT = new PortNumber(Uint16.valueOf(179).intern());
86 private OpenConfigMappingUtil() {
90 static String getRibInstanceName(final InstanceIdentifier<?> rootIdentifier) {
91 return rootIdentifier.firstKeyOf(Protocol.class).getName();
94 static KeyMapping getNeighborKey(final Neighbor neighbor) {
95 if (neighbor.getConfig() != null) {
96 final String authPassword = neighbor.getConfig().getAuthPassword();
97 if (authPassword != null) {
98 return KeyMapping.getKeyMapping(INSTANCE.inetAddressFor(neighbor.getNeighborAddress()), authPassword);
104 static InstanceIdentifier<Neighbor> getNeighborInstanceIdentifier(
105 final InstanceIdentifier<Bgp> rootIdentifier,
106 final NeighborKey neighborKey) {
107 return rootIdentifier.child(Neighbors.class).child(Neighbor.class, neighborKey);
110 static IpAddressNoZone convertIpAddress(final IpAddress addr) {
114 final Ipv4Address ipv4 = addr.getIpv4Address();
116 return new IpAddressNoZone(INSTANCE.ipv4AddressNoZoneFor(ipv4));
118 final Ipv6Address ipv6 = addr.getIpv6Address();
119 checkState(ipv6 != null, "Unexpected address %s", addr);
120 return new IpAddressNoZone(INSTANCE.ipv6AddressNoZoneFor(ipv6));
123 static String getNeighborInstanceName(final InstanceIdentifier<?> rootIdentifier) {
124 return Ipv4Util.toStringIP(convertIpAddress(rootIdentifier.firstKeyOf(Neighbor.class).getNeighborAddress()));
127 //make sure IPv4 Unicast (RFC 4271) when required
128 static List<AfiSafi> getAfiSafiWithDefault(
129 final BgpCommonAfiSafiList afiSAfis, final boolean setDeafultIPv4) {
130 if (afiSAfis == null || afiSAfis.getAfiSafi() == null) {
131 return setDeafultIPv4 ? DEFAULT_AFISAFI : Collections.emptyList();
133 final List<AfiSafi> afiSafi = afiSAfis.getAfiSafi();
134 if (setDeafultIPv4) {
135 final boolean anyMatch = afiSafi.stream()
136 .anyMatch(input -> input.getAfiSafiName().equals(IPV4UNICAST.class));
138 final List<AfiSafi> newAfiSafi = new ArrayList<>(afiSafi.size() + 1);
139 newAfiSafi.addAll(afiSafi);
140 newAfiSafi.add(IPV4_AFISAFI);
147 static ClusterIdentifier getGlobalClusterIdentifier(final org.opendaylight.yang.gen.v1.http.openconfig.net
148 .yang.bgp.rev151009.bgp.global.base.Config globalConfig) {
149 final GlobalConfigAugmentation globalConfigAugmentation
150 = globalConfig.augmentation(GlobalConfigAugmentation.class);
151 final Ipv4Address addr;
152 if (globalConfigAugmentation != null && globalConfigAugmentation.getRouteReflectorClusterId() != null) {
153 addr = globalConfigAugmentation.getRouteReflectorClusterId().getIpv4Address();
155 addr = globalConfig.getRouterId();
157 return new ClusterIdentifier(IetfInetUtil.INSTANCE.ipv4AddressNoZoneFor(addr));
160 static @Nullable ClusterIdentifier getNeighborClusterIdentifier(
161 final @Nullable RouteReflector routeReflector, final @Nullable PeerGroup peerGroup) {
162 if (peerGroup != null) {
163 final ClusterIdentifier clusteriId = extractClusterId(peerGroup.getRouteReflector());
164 if (clusteriId != null) {
169 return extractClusterId(routeReflector);
172 private static ClusterIdentifier extractClusterId(final RouteReflector routeReflector) {
173 if (routeReflector != null) {
174 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.route
175 .reflector.Config config = routeReflector.getConfig();
176 if (config != null && config.getRouteReflectorClusterId() != null) {
177 return new ClusterIdentifier(IetfInetUtil.INSTANCE.ipv4AddressNoZoneFor(
178 config.getRouteReflectorClusterId().getIpv4Address()));
184 static Map<BgpTableType, PathSelectionMode> toPathSelectionMode(final List<AfiSafi> afiSafis,
185 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
186 final Map<BgpTableType, PathSelectionMode> pathSelectionModes = new HashMap<>();
187 for (final AfiSafi afiSafi : afiSafis) {
188 final BgpNeighborAddPathsConfig afiSafi2 = afiSafi.augmentation(GlobalAddPathsConfig.class);
189 if (afiSafi2 != null) {
190 final Optional<BgpTableType> bgpTableType = tableTypeRegistry.getTableType(afiSafi.getAfiSafiName());
191 if (bgpTableType.isPresent()) {
192 final short sendMax = afiSafi2.getSendMax().toJava();
193 final PathSelectionMode selectionMode;
195 selectionMode = new AddPathBestNPathSelection(sendMax);
197 selectionMode = new AllPathSelection();
199 pathSelectionModes.put(bgpTableType.get(), selectionMode);
203 return pathSelectionModes;
206 static boolean isApplicationPeer(final Neighbor neighbor) {
207 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
208 .Config config = neighbor.getConfig();
209 if (config != null) {
210 final NeighborPeerGroupConfig config1 = config.augmentation(NeighborPeerGroupConfig.class);
211 if (config1 != null) {
212 final String peerGroup = config1.getPeerGroup();
213 return peerGroup != null && peerGroup.equals(APPLICATION_PEER_GROUP_NAME);
219 static List<AddressFamilies> toAddPathCapability(final List<AfiSafi> afiSafis,
220 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
221 final List<AddressFamilies> addPathCapability = new ArrayList<>();
222 for (final AfiSafi afiSafi : afiSafis) {
223 final BgpNeighborAddPathsConfig afiSafi1 = afiSafi.augmentation(NeighborAddPathsConfig.class);
224 final Optional<BgpTableType> bgpTableType = tableTypeRegistry.getTableType(afiSafi.getAfiSafiName());
225 if (afiSafi1 != null && bgpTableType.isPresent()) {
226 final AddressFamiliesBuilder builder = new AddressFamiliesBuilder(bgpTableType.get());
227 builder.setSendReceive(toSendReceiveMode(afiSafi1));
228 addPathCapability.add(builder.build());
231 return addPathCapability;
234 private static SendReceive toSendReceiveMode(final BgpNeighborAddPathsConfig addPath) {
235 if (addPath.isReceive() && addPath.getSendMax() != null) {
236 return SendReceive.Both;
238 if (addPath.getSendMax() != null) {
239 return SendReceive.Send;
241 return SendReceive.Receive;
244 private static boolean isRrClient(final BgpNeighborGroup neighbor) {
245 final RouteReflector routeReflector = neighbor.getRouteReflector();
246 if (routeReflector != null && routeReflector.getConfig() != null) {
247 return routeReflector.getConfig().isRouteReflectorClient();
252 static List<BgpTableType> toTableTypes(final List<AfiSafi> afiSafis,
253 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
254 return afiSafis.stream()
255 .map(afiSafi -> tableTypeRegistry.getTableType(afiSafi.getAfiSafiName()))
256 .filter(Optional::isPresent)
258 .collect(Collectors.toList());
261 static Set<TablesKey> toTableKey(final List<AfiSafi> afiSafis, final BGPTableTypeRegistryConsumer
263 return afiSafis.stream()
264 .map(afiSafi -> tableTypeRegistry.getTableKey(afiSafi.getAfiSafiName()))
265 .filter(Optional::isPresent)
267 .collect(Collectors.toSet());
270 static boolean isActive(final Neighbor neighbor, final PeerGroup peerGroup) {
271 Optional<Boolean> activeConnection = peerGroup == null ? Optional.empty() : isActive(peerGroup.getTransport());
272 if (!activeConnection.isPresent()) {
273 activeConnection = isActive(neighbor.getTransport());
275 return activeConnection.orElse(Boolean.TRUE);
278 private static Optional<Boolean> isActive(final Transport transport) {
279 if (transport != null) {
280 final Config config = transport.getConfig();
281 if (config != null) {
282 final Boolean passive = config.isPassiveMode();
283 if (passive != null) {
284 return Optional.of(!passive);
288 return Optional.empty();
291 static PeerRole toPeerRole(final BgpNeighborGroup neighbor) {
292 if (isRrClient(neighbor)) {
293 return PeerRole.RrClient;
296 if (neighbor.getConfig() != null) {
297 final PeerType peerType = neighbor.getConfig().getPeerType();
298 if (peerType == PeerType.EXTERNAL) {
299 return PeerRole.Ebgp;
300 } else if (peerType == PeerType.INTERNAL) {
301 return PeerRole.Ibgp;
307 static @NonNull PeerRole toPeerRole(final Neighbor neighbor, final PeerGroup peerGroup) {
308 PeerRole role = null;
309 if (peerGroup != null) {
310 role = toPeerRole(peerGroup);
314 role = toPeerRole(neighbor);
318 return PeerRole.Ibgp;
323 static int getHoldTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
325 if (peerGroup != null) {
326 hold = getHoldTimer(peerGroup.getTimers());
330 hold = getHoldTimer(neighbor.getTimers());
340 private static @Nullable Integer getHoldTimer(final Timers timers) {
341 if (timers == null) {
344 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
345 .Config config = timers.getConfig();
346 if (config != null && config.getHoldTime() != null) {
347 return config.getHoldTime().intValue();
352 static int getGracefulRestartTimer(final Neighbor neighbor, final PeerGroup peerGroup, final int holdTimer) {
354 if (peerGroup != null) {
355 timer = getGracefulRestartTimer(peerGroup.getGracefulRestart());
359 timer = getGracefulRestartTimer(neighbor.getGracefulRestart());
363 * RFC4724: "A suggested default for the Restart Time is a value less than or
364 * equal to the HOLDTIME carried in the OPEN."
366 return timer == null ? holdTimer : timer.toJava();
369 private static @Nullable Uint16 getGracefulRestartTimer(final GracefulRestart gracefulRestart) {
370 if (gracefulRestart != null) {
371 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.graceful.restart.graceful
372 .restart.Config config = gracefulRestart.getConfig();
373 if (config != null) {
374 return config.getRestartTime();
380 static @NonNull AsNumber getRemotePeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009
381 .bgp.neighbor.group.Config config, final PeerGroup peerGroup, final AsNumber localAs) {
382 AsNumber neighborAs = null;
383 if (peerGroup != null) {
384 neighborAs = getRemotePeerAs(peerGroup.getConfig());
387 if (neighborAs == null) {
388 neighborAs = getRemotePeerAs(config);
391 if (neighborAs == null) {
397 private static AsNumber getRemotePeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
398 .rev151009.bgp.neighbor.group.@Nullable Config config) {
399 return config == null ? null : config.getPeerAs();
402 static @NonNull AsNumber getLocalPeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
403 .rev151009.bgp.neighbor.group.@Nullable Config config, final @NonNull AsNumber globalAs) {
404 if (config != null) {
405 final AsNumber peerAs = config.getLocalAs();
406 if (peerAs != null) {
413 static int getRetryTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
414 Integer retryTimer = null;
415 if (peerGroup != null) {
416 retryTimer = getRetryTimer(peerGroup.getTimers());
419 if (retryTimer == null) {
420 retryTimer = getRetryTimer(neighbor.getTimers());
423 if (retryTimer == null) {
424 return CONNECT_RETRY;
430 private static @Nullable Integer getRetryTimer(final Timers timers) {
431 if (timers == null) {
434 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
435 .Config config = timers.getConfig();
436 if (config != null && config.getConnectRetry() != null) {
437 return config.getConnectRetry().intValue();
442 static @NonNull PortNumber getPort(final Neighbor neighbor, final PeerGroup peerGroup) {
443 PortNumber port = null;
444 if (peerGroup != null) {
445 port = getPort(peerGroup.getTransport(), PeerGroupTransportConfig.class);
449 port = getPort(neighbor.getTransport(), NeighborTransportConfig.class);
459 private static <T extends TransportConfig & Augmentation<Config>> @Nullable PortNumber getPort(
460 final @Nullable Transport transport, final Class<T> augment) {
461 if (transport != null) {
462 final Config config = transport.getConfig();
463 if (config != null) {
464 final T peerTc = config.augmentation(augment);
465 if (peerTc != null) {
466 return peerTc.getRemotePort();
473 static @Nullable IpAddressNoZone getLocalAddress(@Nullable final Transport transport) {
474 if (transport != null && transport.getConfig() != null) {
475 final BgpNeighborTransportConfig.LocalAddress localAddress = transport.getConfig().getLocalAddress();
476 if (localAddress != null) {
477 return convertIpAddress(localAddress.getIpAddress());
483 static @Nullable RevisedErrorHandlingSupport getRevisedErrorHandling(final PeerRole role,final PeerGroup peerGroup,
484 final Neighbor neighbor) {
485 Optional<Boolean> enabled = getRevisedErrorHandling(neighbor);
486 if (!enabled.isPresent()) {
487 enabled = getRevisedErrorHandling(peerGroup);
489 if (!enabled.orElse(Boolean.FALSE)) {
494 return RevisedErrorHandlingSupportImpl.forExternalPeer();
498 return RevisedErrorHandlingSupportImpl.forInternalPeer();
500 throw new IllegalStateException("Unhandled role " + role);
504 private static Optional<Boolean> getRevisedErrorHandling(final BgpNeighborGroup group) {
506 return Optional.empty();
508 final ErrorHandling errorHandling = group.getErrorHandling();
509 if (errorHandling == null) {
510 return Optional.empty();
512 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.error.handling
513 .Config config = errorHandling.getConfig();
514 return config == null ? Optional.empty() : Optional.of(config.isTreatAsWithdraw());