MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / OpenConfigMappingUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl.config;
10
11 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil.INSTANCE;
12
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;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.Set;
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.rib.spi.BGPPeerTracker;
29 import org.opendaylight.protocol.concepts.KeyMapping;
30 import org.opendaylight.protocol.util.Ipv4Util;
31 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.BgpCommonAfiSafiList;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborAddPathsConfig;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborGroup;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborTransportConfig;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.RouteReflector;
38 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Timers;
39 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Transport;
40 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.transport.Config;
41 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
42 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborKey;
43 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.peer.group.PeerGroup;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors;
46 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
47 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.PeerType;
48 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.add.path.capability.AddressFamilies;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.add.path.capability.AddressFamiliesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalAddPathsConfig;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.GlobalConfigAugmentation;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborAddPathsConfig;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborPeerGroupConfig;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.NeighborTransportConfig;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev180329.PeerGroupTransportConfig;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
65 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
66
67 public final class OpenConfigMappingUtil {
68
69     static final String APPLICATION_PEER_GROUP_NAME = "application-peers";
70     static final int HOLDTIMER = 90;
71     private static final AfiSafi IPV4_AFISAFI = new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class).build();
72     private static final List<AfiSafi> DEFAULT_AFISAFI = ImmutableList.of(IPV4_AFISAFI);
73     private static final int CONNECT_RETRY = 30;
74     private static final PortNumber PORT = new PortNumber(179);
75
76     private OpenConfigMappingUtil() {
77         throw new UnsupportedOperationException();
78     }
79
80     public static String getRibInstanceName(final InstanceIdentifier<?> rootIdentifier) {
81         return rootIdentifier.firstKeyOf(Protocol.class).getName();
82     }
83
84     @Nullable
85     private static Integer getHoldTimer(final Timers timers) {
86         if (timers == null) {
87             return null;
88         }
89         final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
90                 .Config config = timers.getConfig();
91         if (config != null && config.getHoldTime() != null) {
92             return config.getHoldTime().intValue();
93         }
94         return null;
95     }
96
97     @Nullable
98     private static AsNumber getRemotePeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
99             .rev151009.bgp.neighbor.group.Config config) {
100         if (config != null) {
101             final AsNumber peerAs = config.getPeerAs();
102             if (peerAs != null) {
103                 return peerAs;
104             }
105         }
106         return null;
107     }
108
109     @Nullable
110     private static Boolean isActive(final Transport transport) {
111         if (transport != null) {
112             final Config config = transport.getConfig();
113             if (config != null && config.isPassiveMode() != null) {
114                 return !config.isPassiveMode();
115             }
116         }
117         return null;
118     }
119
120     @Nullable
121     private static Integer getRetryTimer(final Timers timers) {
122         if (timers == null) {
123             return null;
124         }
125         final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers
126                 .Config config = timers.getConfig();
127         if (config != null && config.getConnectRetry() != null) {
128             return config.getConnectRetry().intValue();
129         }
130         return null;
131     }
132
133     public static KeyMapping getNeighborKey(final Neighbor neighbor) {
134         if (neighbor.getConfig() != null) {
135             final String authPassword = neighbor.getConfig().getAuthPassword();
136             if (authPassword != null) {
137                 return KeyMapping.getKeyMapping(INSTANCE.inetAddressFor(neighbor.getNeighborAddress()), authPassword);
138             }
139         }
140         return null;
141     }
142
143     public static InstanceIdentifier<Neighbor> getNeighborInstanceIdentifier(
144             final InstanceIdentifier<Bgp> rootIdentifier,
145             final NeighborKey neighborKey) {
146         return rootIdentifier.child(Neighbors.class).child(Neighbor.class, neighborKey);
147     }
148
149     public static String getNeighborInstanceName(final InstanceIdentifier<?> rootIdentifier) {
150         return Ipv4Util.toStringIP(rootIdentifier.firstKeyOf(Neighbor.class).getNeighborAddress());
151     }
152
153     @Nullable
154     private static PortNumber getPort(@Nullable final Transport transport) {
155         if (transport != null) {
156             final Config config = transport.getConfig();
157             if (config != null) {
158                 final NeighborTransportConfig peerTc = config.getAugmentation(NeighborTransportConfig.class);
159                 if (peerTc != null) {
160                     return peerTc.getRemotePort();
161                 }
162                 final PeerGroupTransportConfig peerGroupTc = config.getAugmentation(PeerGroupTransportConfig.class);
163                 if (peerGroupTc != null) {
164                     return peerGroupTc.getRemotePort();
165                 }
166             }
167         }
168         return null;
169     }
170
171     //make sure IPv4 Unicast (RFC 4271) when required
172     public static List<AfiSafi> getAfiSafiWithDefault(
173             final BgpCommonAfiSafiList afiSAfis, final boolean setDeafultIPv4) {
174         if (afiSAfis == null || afiSAfis.getAfiSafi() == null) {
175             return setDeafultIPv4 ? DEFAULT_AFISAFI : Collections.emptyList();
176         }
177         final List<AfiSafi> afiSafi = afiSAfis.getAfiSafi();
178         if (setDeafultIPv4) {
179             final boolean anyMatch = afiSafi.stream()
180                     .anyMatch(input -> input.getAfiSafiName().equals(IPV4UNICAST.class));
181             if (!anyMatch) {
182                 afiSafi.add(IPV4_AFISAFI);
183             }
184         }
185         return afiSafi;
186     }
187
188     public static ClusterIdentifier getGlobalClusterIdentifier(final org.opendaylight.yang.gen.v1.http.openconfig.net
189             .yang.bgp.rev151009.bgp.global.base.Config globalConfig) {
190         final GlobalConfigAugmentation globalConfigAugmentation
191                 = globalConfig.getAugmentation(GlobalConfigAugmentation.class);
192         if (globalConfigAugmentation != null && globalConfigAugmentation.getRouteReflectorClusterId() != null) {
193             return new ClusterIdentifier(globalConfigAugmentation.getRouteReflectorClusterId().getIpv4Address());
194         }
195         return new ClusterIdentifier(globalConfig.getRouterId());
196     }
197
198     @Nullable
199     public static ClusterIdentifier getNeighborClusterIdentifier(
200             @Nullable final RouteReflector routeReflector,
201             @Nullable final PeerGroup peerGroup) {
202         if (peerGroup != null) {
203             final ClusterIdentifier clusteriId = extractClusterId(peerGroup.getRouteReflector());
204             if (clusteriId != null) {
205                 return clusteriId;
206             }
207         }
208
209         return extractClusterId(routeReflector);
210     }
211
212     private static ClusterIdentifier extractClusterId(final RouteReflector routeReflector) {
213         if (routeReflector != null) {
214             final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.route
215                     .reflector.Config config = routeReflector.getConfig();
216             if (config != null && config.getRouteReflectorClusterId() != null) {
217                 return new ClusterIdentifier(config.getRouteReflectorClusterId().getIpv4Address());
218             }
219         }
220         return null;
221     }
222
223     public static Map<BgpTableType, PathSelectionMode> toPathSelectionMode(final List<AfiSafi> afiSafis,
224             final BGPTableTypeRegistryConsumer tableTypeRegistry, final BGPPeerTracker peerTracker) {
225         final Map<BgpTableType, PathSelectionMode> pathSelectionModes = new HashMap<>();
226         for (final AfiSafi afiSafi : afiSafis) {
227             final BgpNeighborAddPathsConfig afiSafi2 = afiSafi.getAugmentation(GlobalAddPathsConfig.class);
228             if (afiSafi2 != null) {
229                 final Optional<BgpTableType> bgpTableType = tableTypeRegistry.getTableType(afiSafi.getAfiSafiName());
230                 if (bgpTableType.isPresent()) {
231                     final Short sendMax = afiSafi2.getSendMax();
232                     final PathSelectionMode selectionMode;
233                     if (sendMax > 1) {
234                         selectionMode = new AddPathBestNPathSelection(sendMax.longValue(), peerTracker);
235                     } else {
236                         selectionMode = new AllPathSelection(peerTracker);
237                     }
238                     pathSelectionModes.put(bgpTableType.get(), selectionMode);
239                 }
240             }
241         }
242         return pathSelectionModes;
243     }
244
245     public static boolean isApplicationPeer(final Neighbor neighbor) {
246         final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group
247                 .Config config = neighbor.getConfig();
248         if (config != null) {
249             final NeighborPeerGroupConfig config1 = config.getAugmentation(NeighborPeerGroupConfig.class);
250             if (config1 != null) {
251                 final String peerGroup = config1.getPeerGroup();
252                 return peerGroup != null && peerGroup.equals(APPLICATION_PEER_GROUP_NAME);
253             }
254         }
255         return false;
256     }
257
258     public static List<AddressFamilies> toAddPathCapability(final List<AfiSafi> afiSafis,
259             final BGPTableTypeRegistryConsumer tableTypeRegistry) {
260         final List<AddressFamilies> addPathCapability = new ArrayList<>();
261         for (final AfiSafi afiSafi : afiSafis) {
262             final BgpNeighborAddPathsConfig afiSafi1 = afiSafi.getAugmentation(NeighborAddPathsConfig.class);
263             final Optional<BgpTableType> bgpTableType = tableTypeRegistry.getTableType(afiSafi.getAfiSafiName());
264             if (afiSafi1 != null && bgpTableType.isPresent()) {
265                 final AddressFamiliesBuilder builder = new AddressFamiliesBuilder(bgpTableType.get());
266                 builder.setSendReceive(toSendReceiveMode(afiSafi1));
267                 addPathCapability.add(builder.build());
268             }
269         }
270         return addPathCapability;
271     }
272
273     private static SendReceive toSendReceiveMode(final BgpNeighborAddPathsConfig addPath) {
274         if (addPath.isReceive() && addPath.getSendMax() != null) {
275             return SendReceive.Both;
276         }
277         if (addPath.getSendMax() != null) {
278             return SendReceive.Send;
279         }
280         return SendReceive.Receive;
281     }
282
283     public static PeerRole toPeerRole(final BgpNeighborGroup neighbor) {
284         if (isRrClient(neighbor)) {
285             return PeerRole.RrClient;
286         }
287
288         if (neighbor.getConfig() != null) {
289             final PeerType peerType = neighbor.getConfig().getPeerType();
290             if (peerType == PeerType.EXTERNAL) {
291                 return PeerRole.Ebgp;
292             } else if (peerType == PeerType.INTERNAL) {
293                 return PeerRole.Ibgp;
294             }
295         }
296         return null;
297     }
298
299     private static boolean isRrClient(final BgpNeighborGroup neighbor) {
300         final RouteReflector routeReflector = neighbor.getRouteReflector();
301         if (routeReflector != null && routeReflector.getConfig() != null) {
302             return routeReflector.getConfig().isRouteReflectorClient();
303         }
304         return false;
305     }
306
307     public static List<BgpTableType> toTableTypes(final List<AfiSafi> afiSafis,
308             final BGPTableTypeRegistryConsumer tableTypeRegistry) {
309         return afiSafis.stream()
310                 .map(afiSafi -> tableTypeRegistry.getTableType(afiSafi.getAfiSafiName()))
311                 .filter(Optional::isPresent)
312                 .map(Optional::get)
313                 .collect(Collectors.toList());
314     }
315
316     public static Set<TablesKey> toTableKey(final List<AfiSafi> afiSafis, final BGPTableTypeRegistryConsumer
317             tableTypeRegistry) {
318         return afiSafis.stream()
319                 .map(afiSafi -> tableTypeRegistry.getTableKey(afiSafi.getAfiSafiName()))
320                 .filter(Optional::isPresent)
321                 .map(Optional::get)
322                 .collect(Collectors.toSet());
323     }
324
325     @Nonnull
326     public static boolean isActive(final Neighbor neighbor, final PeerGroup peerGroup) {
327         Boolean activeConnection = null;
328         if (peerGroup != null) {
329             activeConnection = isActive(peerGroup.getTransport());
330         }
331
332         if (activeConnection == null) {
333             activeConnection = isActive(neighbor.getTransport());
334         }
335         if (activeConnection == null) {
336             return true;
337         }
338         return activeConnection;
339     }
340
341     @Nonnull
342     public static PeerRole toPeerRole(final Neighbor neighbor, final PeerGroup peerGroup) {
343         PeerRole role = null;
344         if (peerGroup != null) {
345             role = toPeerRole(peerGroup);
346         }
347
348         if (role == null) {
349             role = toPeerRole(neighbor);
350         }
351
352         if (role == null) {
353             return PeerRole.Ibgp;
354         }
355         return role;
356     }
357
358     public static int getHoldTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
359         Integer hold = null;
360         if (peerGroup != null) {
361             hold = getHoldTimer(peerGroup.getTimers());
362         }
363
364         if (hold == null) {
365             hold = getHoldTimer(neighbor.getTimers());
366         }
367
368         if (hold == null) {
369             return HOLDTIMER;
370         }
371
372         return hold;
373     }
374
375     @Nonnull
376     public static AsNumber getRemotePeerAs(final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009
377             .bgp.neighbor.group.Config config, final PeerGroup peerGroup, final AsNumber localAs) {
378         AsNumber neighborAs = null;
379         if (peerGroup != null) {
380             neighborAs = getRemotePeerAs(peerGroup.getConfig());
381         }
382
383         if (neighborAs == null) {
384             neighborAs = getRemotePeerAs(config);
385         }
386
387         if (neighborAs == null) {
388             return localAs;
389         }
390         return neighborAs;
391     }
392
393     @Nonnull
394     public static AsNumber getLocalPeerAs(@Nullable final org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp
395             .rev151009.bgp.neighbor.group.Config config, @Nonnull final AsNumber globalAs) {
396         if (config != null) {
397             final AsNumber peerAs = config.getLocalAs();
398             if (peerAs != null) {
399                 return peerAs;
400             }
401         }
402         return globalAs;
403     }
404
405     public static int getRetryTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
406         Integer retryTimer = null;
407         if (peerGroup != null) {
408             retryTimer = getRetryTimer(peerGroup.getTimers());
409         }
410
411         if (retryTimer == null) {
412             retryTimer = getRetryTimer(neighbor.getTimers());
413         }
414
415         if (retryTimer == null) {
416             return CONNECT_RETRY;
417         }
418
419         return retryTimer;
420     }
421
422     @Nonnull
423     public static PortNumber getPort(final Neighbor neighbor, final PeerGroup peerGroup) {
424         PortNumber port = null;
425         if (peerGroup != null) {
426             port = getPort(peerGroup.getTransport());
427         }
428
429         if (port == null) {
430             port = getPort(neighbor.getTransport());
431         }
432
433         if (port == null) {
434             return PORT;
435         }
436
437         return port;
438     }
439
440     @Nullable
441     public static IpAddress getLocalAddress(@Nullable final Transport transport) {
442         if (transport != null && transport.getConfig() != null) {
443             final BgpNeighborTransportConfig.LocalAddress localAddress = transport.getConfig().getLocalAddress();
444             if (localAddress != null ) {
445                 return localAddress.getIpAddress();
446             }
447         }
448         return null;
449     }
450 }