Fix more rib-impl checkstyle
[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.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;
71
72 final class OpenConfigMappingUtil {
73
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);
81
82     private OpenConfigMappingUtil() {
83         throw new UnsupportedOperationException();
84     }
85
86     static String getRibInstanceName(final InstanceIdentifier<?> rootIdentifier) {
87         return rootIdentifier.firstKeyOf(Protocol.class).getName();
88     }
89
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);
95             }
96         }
97         return null;
98     }
99
100     static InstanceIdentifier<Neighbor> getNeighborInstanceIdentifier(
101             final InstanceIdentifier<Bgp> rootIdentifier,
102             final NeighborKey neighborKey) {
103         return rootIdentifier.child(Neighbors.class).child(Neighbor.class, neighborKey);
104     }
105
106     static String getNeighborInstanceName(final InstanceIdentifier<?> rootIdentifier) {
107         return Ipv4Util.toStringIP(rootIdentifier.firstKeyOf(Neighbor.class).getNeighborAddress());
108     }
109
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();
115         }
116         final List<AfiSafi> afiSafi = afiSAfis.getAfiSafi();
117         if (setDeafultIPv4) {
118             final boolean anyMatch = afiSafi.stream()
119                     .anyMatch(input -> input.getAfiSafiName().equals(IPV4UNICAST.class));
120             if (!anyMatch) {
121                 afiSafi.add(IPV4_AFISAFI);
122             }
123         }
124         return afiSafi;
125     }
126
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());
133         }
134         return new ClusterIdentifier(globalConfig.getRouterId());
135     }
136
137     @Nullable
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) {
144                 return clusteriId;
145             }
146         }
147
148         return extractClusterId(routeReflector);
149     }
150
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());
157             }
158         }
159         return null;
160     }
161
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;
172                     if (sendMax > 1) {
173                         selectionMode = new AddPathBestNPathSelection(sendMax);
174                     } else {
175                         selectionMode = new AllPathSelection();
176                     }
177                     pathSelectionModes.put(bgpTableType.get(), selectionMode);
178                 }
179             }
180         }
181         return pathSelectionModes;
182     }
183
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);
192             }
193         }
194         return false;
195     }
196
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());
207             }
208         }
209         return addPathCapability;
210     }
211
212     private static SendReceive toSendReceiveMode(final BgpNeighborAddPathsConfig addPath) {
213         if (addPath.isReceive() && addPath.getSendMax() != null) {
214             return SendReceive.Both;
215         }
216         if (addPath.getSendMax() != null) {
217             return SendReceive.Send;
218         }
219         return SendReceive.Receive;
220     }
221
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();
226         }
227         return false;
228     }
229
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)
235                 .map(Optional::get)
236                 .collect(Collectors.toList());
237     }
238
239     static Set<TablesKey> toTableKey(final List<AfiSafi> afiSafis, final BGPTableTypeRegistryConsumer
240             tableTypeRegistry) {
241         return afiSafis.stream()
242                 .map(afiSafi -> tableTypeRegistry.getTableKey(afiSafi.getAfiSafiName()))
243                 .filter(Optional::isPresent)
244                 .map(Optional::get)
245                 .collect(Collectors.toSet());
246     }
247
248     static boolean isActive(final Neighbor neighbor, final PeerGroup peerGroup) {
249         Boolean activeConnection = null;
250         if (peerGroup != null) {
251             activeConnection = isActive(peerGroup.getTransport());
252         }
253
254         if (activeConnection == null) {
255             activeConnection = isActive(neighbor.getTransport());
256         }
257         if (activeConnection == null) {
258             return true;
259         }
260         return activeConnection;
261     }
262
263     @Nullable
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();
269             }
270         }
271         return null;
272     }
273
274     static PeerRole toPeerRole(final BgpNeighborGroup neighbor) {
275         if (isRrClient(neighbor)) {
276             return PeerRole.RrClient;
277         }
278
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;
285             }
286         }
287         return null;
288     }
289
290     @Nonnull
291     static PeerRole toPeerRole(final Neighbor neighbor, final PeerGroup peerGroup) {
292         PeerRole role = null;
293         if (peerGroup != null) {
294             role = toPeerRole(peerGroup);
295         }
296
297         if (role == null) {
298             role = toPeerRole(neighbor);
299         }
300
301         if (role == null) {
302             return PeerRole.Ibgp;
303         }
304         return role;
305     }
306
307     static int getHoldTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
308         Integer hold = null;
309         if (peerGroup != null) {
310             hold = getHoldTimer(peerGroup.getTimers());
311         }
312
313         if (hold == null) {
314             hold = getHoldTimer(neighbor.getTimers());
315         }
316
317         if (hold == null) {
318             return HOLDTIMER;
319         }
320
321         return hold;
322     }
323
324     @Nullable
325     private static Integer getHoldTimer(final Timers timers) {
326         if (timers == null) {
327             return null;
328         }
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();
333         }
334         return null;
335     }
336
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());
341         }
342
343         if (timer == null) {
344             timer = getGracefulRestartTimer(neighbor.getGracefulRestart());
345         }
346
347         /*
348          * RFC4724: "A suggested default for the Restart Time is a value less than or
349          * equal to the HOLDTIME carried in the OPEN."
350          */
351         if (timer == null) {
352             timer = holdTimer;
353         }
354
355         return timer;
356     }
357
358     @Nullable
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();
365             }
366         }
367         return null;
368     }
369
370     @Nonnull
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());
376         }
377
378         if (neighborAs == null) {
379             neighborAs = getRemotePeerAs(config);
380         }
381
382         if (neighborAs == null) {
383             return localAs;
384         }
385         return neighborAs;
386     }
387
388     @Nullable
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();
392     }
393
394     @Nonnull
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) {
400                 return peerAs;
401             }
402         }
403         return globalAs;
404     }
405
406     static int getRetryTimer(final Neighbor neighbor, final PeerGroup peerGroup) {
407         Integer retryTimer = null;
408         if (peerGroup != null) {
409             retryTimer = getRetryTimer(peerGroup.getTimers());
410         }
411
412         if (retryTimer == null) {
413             retryTimer = getRetryTimer(neighbor.getTimers());
414         }
415
416         if (retryTimer == null) {
417             return CONNECT_RETRY;
418         }
419
420         return retryTimer;
421     }
422
423     @Nullable
424     private static Integer getRetryTimer(final Timers timers) {
425         if (timers == null) {
426             return null;
427         }
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();
432         }
433         return null;
434     }
435
436     @Nonnull
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);
441         }
442
443         if (port == null) {
444             port = getPort(neighbor.getTransport(), NeighborTransportConfig.class);
445         }
446
447         if (port == null) {
448             return PORT;
449         }
450
451         return port;
452     }
453
454     @Nullable
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();
463                 }
464             }
465         }
466         return null;
467     }
468
469     @Nullable
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();
475             }
476         }
477         return null;
478     }
479
480     @Nullable
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);
486         }
487         if (!Boolean.TRUE.equals(enabled)) {
488             return null;
489         }
490         switch (role) {
491             case Ebgp:
492                 return RevisedErrorHandlingSupportImpl.forExternalPeer();
493             case Ibgp:
494             case Internal:
495             case RrClient:
496                 return RevisedErrorHandlingSupportImpl.forInternalPeer();
497             default:
498                 throw new IllegalStateException("Unhandled role " + role);
499         }
500     }
501
502     @Nullable
503     private static @org.eclipse.jdt.annotation.Nullable Boolean getRevisedErrorHandling(final BgpNeighborGroup group) {
504         if (group == null) {
505             return null;
506         }
507         final ErrorHandling errorHandling = group.getErrorHandling();
508         if (errorHandling == null) {
509             return null;
510         }
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();
514     }
515 }