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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
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 javax.annotation.Nonnull;
23 import javax.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.IpAddress;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.add.path.capability.AddressFamilies;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.add.path.capability.AddressFamiliesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAddPathsConfig;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalConfigAugmentation;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAddPathsConfig;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborPeerGroupConfig;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTransportConfig;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupTransportConfig;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.TransportConfig;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
69 import org.opendaylight.yangtools.yang.binding.Augmentation;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
72 final class OpenConfigMappingUtil {
74 static final String APPLICATION_PEER_GROUP_NAME = "application-peers";
75 static final Optional<String> APPLICATION_PEER_GROUP_NAME_OPT = Optional.of(APPLICATION_PEER_GROUP_NAME);
76 static final int HOLDTIMER = 90;
77 private static final AfiSafi IPV4_AFISAFI = new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class).build();
78 private static final List<AfiSafi> DEFAULT_AFISAFI = ImmutableList.of(IPV4_AFISAFI);
79 private static final int CONNECT_RETRY = 30;
80 private static final PortNumber PORT = new PortNumber(179);
82 private OpenConfigMappingUtil() {
83 throw new UnsupportedOperationException();
86 static String getRibInstanceName(final InstanceIdentifier<?> rootIdentifier) {
87 return rootIdentifier.firstKeyOf(Protocol.class).getName();
90 static KeyMapping getNeighborKey(final Neighbor neighbor) {
91 if (neighbor.getConfig() != null) {
92 final String authPassword = neighbor.getConfig().getAuthPassword();
93 if (authPassword != null) {
94 return KeyMapping.getKeyMapping(INSTANCE.inetAddressFor(neighbor.getNeighborAddress()), authPassword);
100 static InstanceIdentifier<Neighbor> getNeighborInstanceIdentifier(
101 final InstanceIdentifier<Bgp> rootIdentifier,
102 final NeighborKey neighborKey) {
103 return rootIdentifier.child(Neighbors.class).child(Neighbor.class, neighborKey);
106 static String getNeighborInstanceName(final InstanceIdentifier<?> rootIdentifier) {
107 return Ipv4Util.toStringIP(rootIdentifier.firstKeyOf(Neighbor.class).getNeighborAddress());
110 //make sure IPv4 Unicast (RFC 4271) when required
111 static List<AfiSafi> getAfiSafiWithDefault(
112 final BgpCommonAfiSafiList afiSAfis, final boolean setDeafultIPv4) {
113 if (afiSAfis == null || afiSAfis.getAfiSafi() == null) {
114 return setDeafultIPv4 ? DEFAULT_AFISAFI : Collections.emptyList();
116 final List<AfiSafi> afiSafi = afiSAfis.getAfiSafi();
117 if (setDeafultIPv4) {
118 final boolean anyMatch = afiSafi.stream()
119 .anyMatch(input -> input.getAfiSafiName().equals(IPV4UNICAST.class));
121 afiSafi.add(IPV4_AFISAFI);
127 static ClusterIdentifier getGlobalClusterIdentifier(final org.opendaylight.yang.gen.v1.http.openconfig.net
128 .yang.bgp.rev151009.bgp.global.base.Config globalConfig) {
129 final GlobalConfigAugmentation globalConfigAugmentation
130 = globalConfig.augmentation(GlobalConfigAugmentation.class);
131 if (globalConfigAugmentation != null && globalConfigAugmentation.getRouteReflectorClusterId() != null) {
132 return new ClusterIdentifier(globalConfigAugmentation.getRouteReflectorClusterId().getIpv4Address());
134 return new ClusterIdentifier(globalConfig.getRouterId());
138 static ClusterIdentifier getNeighborClusterIdentifier(
139 @Nullable final RouteReflector routeReflector,
140 @Nullable final PeerGroup peerGroup) {
141 if (peerGroup != null) {
142 final ClusterIdentifier clusteriId = extractClusterId(peerGroup.getRouteReflector());
143 if (clusteriId != null) {
148 return extractClusterId(routeReflector);
151 private static ClusterIdentifier extractClusterId(final RouteReflector routeReflector) {
152 if (routeReflector != null) {
153 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.route
154 .reflector.Config config = routeReflector.getConfig();
155 if (config != null && config.getRouteReflectorClusterId() != null) {
156 return new ClusterIdentifier(config.getRouteReflectorClusterId().getIpv4Address());
162 static Map<BgpTableType, PathSelectionMode> toPathSelectionMode(final List<AfiSafi> afiSafis,
163 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
164 final Map<BgpTableType, PathSelectionMode> pathSelectionModes = new HashMap<>();
165 for (final AfiSafi afiSafi : afiSafis) {
166 final BgpNeighborAddPathsConfig afiSafi2 = afiSafi.augmentation(GlobalAddPathsConfig.class);
167 if (afiSafi2 != null) {
168 final Optional<BgpTableType> bgpTableType = tableTypeRegistry.getTableType(afiSafi.getAfiSafiName());
169 if (bgpTableType.isPresent()) {
170 final int sendMax = afiSafi2.getSendMax();
171 final PathSelectionMode selectionMode;
173 selectionMode = new AddPathBestNPathSelection(sendMax);
175 selectionMode = new AllPathSelection();
177 pathSelectionModes.put(bgpTableType.get(), selectionMode);
181 return pathSelectionModes;
184 static boolean isApplicationPeer(final Neighbor neighbor) {
185 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
186 .Config config = neighbor.getConfig();
187 if (config != null) {
188 final NeighborPeerGroupConfig config1 = config.augmentation(NeighborPeerGroupConfig.class);
189 if (config1 != null) {
190 final String peerGroup = config1.getPeerGroup();
191 return peerGroup != null && peerGroup.equals(APPLICATION_PEER_GROUP_NAME);
197 static List<AddressFamilies> toAddPathCapability(final List<AfiSafi> afiSafis,
198 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
199 final List<AddressFamilies> addPathCapability = new ArrayList<>();
200 for (final AfiSafi afiSafi : afiSafis) {
201 final BgpNeighborAddPathsConfig afiSafi1 = afiSafi.augmentation(NeighborAddPathsConfig.class);
202 final Optional<BgpTableType> bgpTableType = tableTypeRegistry.getTableType(afiSafi.getAfiSafiName());
203 if (afiSafi1 != null && bgpTableType.isPresent()) {
204 final AddressFamiliesBuilder builder = new AddressFamiliesBuilder(bgpTableType.get());
205 builder.setSendReceive(toSendReceiveMode(afiSafi1));
206 addPathCapability.add(builder.build());
209 return addPathCapability;
212 private static SendReceive toSendReceiveMode(final BgpNeighborAddPathsConfig addPath) {
213 if (addPath.isReceive() && addPath.getSendMax() != null) {
214 return SendReceive.Both;
216 if (addPath.getSendMax() != null) {
217 return SendReceive.Send;
219 return SendReceive.Receive;
222 private static boolean isRrClient(final BgpNeighborGroup neighbor) {
223 final RouteReflector routeReflector = neighbor.getRouteReflector();
224 if (routeReflector != null && routeReflector.getConfig() != null) {
225 return routeReflector.getConfig().isRouteReflectorClient();
230 static List<BgpTableType> toTableTypes(final List<AfiSafi> afiSafis,
231 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
232 return afiSafis.stream()
233 .map(afiSafi -> tableTypeRegistry.getTableType(afiSafi.getAfiSafiName()))
234 .filter(Optional::isPresent)
236 .collect(Collectors.toList());
239 static Set<TablesKey> toTableKey(final List<AfiSafi> afiSafis, final BGPTableTypeRegistryConsumer
241 return afiSafis.stream()
242 .map(afiSafi -> tableTypeRegistry.getTableKey(afiSafi.getAfiSafiName()))
243 .filter(Optional::isPresent)
245 .collect(Collectors.toSet());
248 static boolean isActive(final Neighbor neighbor, final PeerGroup peerGroup) {
249 Boolean activeConnection = null;
250 if (peerGroup != null) {
251 activeConnection = isActive(peerGroup.getTransport());
254 if (activeConnection == null) {
255 activeConnection = isActive(neighbor.getTransport());
257 if (activeConnection == null) {
260 return activeConnection;
264 private static Boolean isActive(final Transport transport) {
265 if (transport != null) {
266 final Config config = transport.getConfig();
267 if (config != null && config.isPassiveMode() != null) {
268 return !config.isPassiveMode();
274 static PeerRole toPeerRole(final BgpNeighborGroup neighbor) {
275 if (isRrClient(neighbor)) {
276 return PeerRole.RrClient;
279 if (neighbor.getConfig() != null) {
280 final PeerType peerType = neighbor.getConfig().getPeerType();
281 if (peerType == PeerType.EXTERNAL) {
282 return PeerRole.Ebgp;
283 } else if (peerType == PeerType.INTERNAL) {
284 return PeerRole.Ibgp;
291 static PeerRole toPeerRole(final Neighbor neighbor, final PeerGroup peerGroup) {
292 PeerRole role = null;
293 if (peerGroup != null) {
294 role = toPeerRole(peerGroup);
298 role = toPeerRole(neighbor);
302 return PeerRole.Ibgp;
307 static int getHoldTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
309 if (peerGroup != null) {
310 hold = getHoldTimer(peerGroup.getTimers());
314 hold = getHoldTimer(neighbor.getTimers());
325 private static Integer getHoldTimer(final Timers timers) {
326 if (timers == null) {
329 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
330 .Config config = timers.getConfig();
331 if (config != null && config.getHoldTime() != null) {
332 return config.getHoldTime().intValue();
337 static int getGracefulRestartTimer(final Neighbor neighbor, final PeerGroup peerGroup, final int holdTimer) {
338 Integer timer = null;
339 if (peerGroup != null) {
340 timer = getGracefulRestartTimer(peerGroup.getGracefulRestart());
344 timer = getGracefulRestartTimer(neighbor.getGracefulRestart());
348 * RFC4724: "A suggested default for the Restart Time is a value less than or
349 * equal to the HOLDTIME carried in the OPEN."
359 private static Integer getGracefulRestartTimer(final GracefulRestart gracefulRestart) {
360 if (gracefulRestart != null) {
361 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.graceful.restart.graceful
362 .restart.Config config = gracefulRestart.getConfig();
363 if (config != null) {
364 return config.getRestartTime();
371 static AsNumber getRemotePeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009
372 .bgp.neighbor.group.Config config, final PeerGroup peerGroup, final AsNumber localAs) {
373 AsNumber neighborAs = null;
374 if (peerGroup != null) {
375 neighborAs = getRemotePeerAs(peerGroup.getConfig());
378 if (neighborAs == null) {
379 neighborAs = getRemotePeerAs(config);
382 if (neighborAs == null) {
389 private static AsNumber getRemotePeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
390 .rev151009.bgp.neighbor.group.Config config) {
391 return config == null ? null : config.getPeerAs();
395 static AsNumber getLocalPeerAs(@Nullable final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
396 .rev151009.bgp.neighbor.group.Config config, @Nonnull final AsNumber globalAs) {
397 if (config != null) {
398 final AsNumber peerAs = config.getLocalAs();
399 if (peerAs != null) {
406 static int getRetryTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
407 Integer retryTimer = null;
408 if (peerGroup != null) {
409 retryTimer = getRetryTimer(peerGroup.getTimers());
412 if (retryTimer == null) {
413 retryTimer = getRetryTimer(neighbor.getTimers());
416 if (retryTimer == null) {
417 return CONNECT_RETRY;
424 private static Integer getRetryTimer(final Timers timers) {
425 if (timers == null) {
428 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
429 .Config config = timers.getConfig();
430 if (config != null && config.getConnectRetry() != null) {
431 return config.getConnectRetry().intValue();
437 static PortNumber getPort(final Neighbor neighbor, final PeerGroup peerGroup) {
438 PortNumber port = null;
439 if (peerGroup != null) {
440 port = getPort(peerGroup.getTransport(), PeerGroupTransportConfig.class);
444 port = getPort(neighbor.getTransport(), NeighborTransportConfig.class);
455 private static <T extends TransportConfig & Augmentation<Config>> PortNumber getPort(
456 @Nullable final Transport transport, final Class<T> augment) {
457 if (transport != null) {
458 final Config config = transport.getConfig();
459 if (config != null) {
460 final T peerTc = config.augmentation(augment);
461 if (peerTc != null) {
462 return peerTc.getRemotePort();
470 static IpAddress getLocalAddress(@Nullable final Transport transport) {
471 if (transport != null && transport.getConfig() != null) {
472 final BgpNeighborTransportConfig.LocalAddress localAddress = transport.getConfig().getLocalAddress();
473 if (localAddress != null) {
474 return localAddress.getIpAddress();
481 static RevisedErrorHandlingSupport getRevisedErrorHandling(final PeerRole role,final PeerGroup peerGroup,
482 final Neighbor neighbor) {
483 Boolean enabled = getRevisedErrorHandling(neighbor);
484 if (enabled == null) {
485 enabled = getRevisedErrorHandling(peerGroup);
487 if (!Boolean.TRUE.equals(enabled)) {
492 return RevisedErrorHandlingSupportImpl.forExternalPeer();
496 return RevisedErrorHandlingSupportImpl.forInternalPeer();
498 throw new IllegalStateException("Unhandled role " + role);
503 private static @org.eclipse.jdt.annotation.Nullable Boolean getRevisedErrorHandling(final BgpNeighborGroup group) {
507 final ErrorHandling errorHandling = group.getErrorHandling();
508 if (errorHandling == null) {
511 final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.error.handling
512 .Config config = errorHandling.getConfig();
513 return config == null ? null : config.isTreatAsWithdraw();