import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
+import javax.annotation.Nullable;
import org.apache.commons.lang3.ArrayUtils;
import org.opendaylight.openflowplugin.libraries.liblldp.HexEncode;
import org.opendaylight.openflowplugin.libraries.liblldp.NetUtils;
// It's unclear from all the callers if returning an empty array would be safe.
@SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
+ @Nullable
public byte[] getOptionBytes(byte code) {
DhcpOption option = this.getOption(code);
return option != null ? option.getValue() : null;
this.setOption(new DhcpOption(code, DHCPUtils.inetAddrToByteArray(opt)));
}
+ @Nullable
public InetAddress getOptionInetAddr(byte code) {
byte[] opt = this.getOptionBytes(code);
try {
this.setOption(new DhcpOption(code, DHCPUtils.strAddrToByteArray(opt)));
}
+ @Nullable
public String getOptionStrAddr(byte code) {
byte[] opt = this.getOptionBytes(code);
try {
package org.opendaylight.netvirt.dhcpservice.api;
+import static java.util.Collections.emptyList;
+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
public abstract class DHCPUtils {
return result;
}
- public static short byteArrayToShort(byte[] ba) {
+ public static short byteArrayToShort(@Nullable byte[] ba) {
if (ba == null || ba.length != 2) {
return 0;
}
return (short) ((0xff & ba[0]) << 8 | 0xff & ba[1]);
}
+ @Nullable
public static InetAddress byteArrayToInetAddr(byte[] ba) {
try {
return InetAddress.getByAddress(ba);
// An empty byte[] would technically be an invalid MAC address and it's unclear if we can force callers to pass
// a non-null String.
@SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
+ @Nullable
public static byte[] strMacAddrtoByteArray(String macAddress) {
if (macAddress == null) {
return null;
str.deleteCharAt(str.lastIndexOf(":"));
return str.toString();
}
+
+ // TODO Replace this with mdsal's DataObjectUtils.nullToEmpty when upgrading to mdsal 3
+ @Nonnull
+ public static <T> List<T> nullToEmpty(final @Nullable List<T> input) {
+ return input != null ? input : emptyList();
+ }
}
*/
package org.opendaylight.netvirt.dhcpservice;
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty;
+
import com.google.common.base.Optional;
import java.math.BigInteger;
import java.util.EventListener;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
+import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
}
}
+ @Nullable
public IpAddress getIpAllocation(String networkId, AllocationPool pool, String macAddress) {
String poolIdKey = getPoolKeyIdByAllocationPool(networkId, pool);
long allocatedIpLong = createIdAllocation(poolIdKey, macAddress);
LOG.debug("allocated id {} for mac {}, from pool {}", allocatedIpLong, macAddress, poolIdKey);
- IpAddress allocatedIpAddress = allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong)
- : null;
- return allocatedIpAddress;
+ return allocatedIpLong != 0 ? DhcpServiceUtils.convertLongToIp(allocatedIpLong) : null;
}
public void releaseIpAllocation(String networkId, AllocationPool pool, String macAddress) {
releaseIdAllocation(poolIdKey, macAddress);
}
+ @Nullable
public AllocationPool getAllocationPoolByNetwork(String networkId) throws ReadFailedException {
InstanceIdentifier<Network> network = InstanceIdentifier.builder(DhcpAllocationPool.class)
.child(Network.class, new NetworkKey(networkId)).build();
}
}
+ @Nullable
public Map<BigInteger, List<String>> getElanDpnInterfacesByName(DataBroker broker, String elanInstanceName) {
InstanceIdentifier<ElanDpnInterfacesList> elanDpnIfacesIid = InstanceIdentifier.builder(ElanDpnInterfaces.class)
.child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
return null;
}
- return elanDpnIfacesOpc.get().getDpnInterfaces().stream()
- .collect(Collectors.toMap(DpnInterfaces::getDpId, DpnInterfaces::getInterfaces));
+ return nullToEmpty(elanDpnIfacesOpc.get().getDpnInterfaces()).stream()
+ .collect(Collectors.toMap(DpnInterfaces::getDpId, value -> nullToEmpty(value.getInterfaces())));
}
+ @Nullable
public String getNetworkByPort(String portUuid) throws ReadFailedException {
InstanceIdentifier<ElanInterface> elanInterfaceName = InstanceIdentifier.builder(ElanInterfaces.class)
.child(ElanInterface.class, new ElanInterfaceKey(portUuid)).build();
*/
package org.opendaylight.netvirt.dhcpservice;
+import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
updateConfig(add);
}
- private void updateConfig(DhcpConfig update) {
+ private void updateConfig(@Nullable DhcpConfig update) {
//TODO: Update operational with actual values
if (update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
dhcpManager.setLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
package org.opendaylight.netvirt.dhcpservice;
import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
Optional<DesignatedSwitchesForExternalTunnels> designatedSwitchForTunnelOptional =
MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
if (designatedSwitchForTunnelOptional.isPresent()) {
- List<DesignatedSwitchForTunnel> list =
- designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel();
+ List<DesignatedSwitchForTunnel> list = nullToEmpty(
+ designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel());
for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) {
Set<Pair<IpAddress, String>> setOfTunnelIpElanNamePair =
designatedDpnsToTunnelIpElanNameCache
InstanceIdentifier<Ports> inst = InstanceIdentifier.builder(Neutron.class).child(Ports.class).build();
Optional<Ports> optionalPorts = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
if (optionalPorts.isPresent()) {
- List<Port> list = optionalPorts.get().getPort();
+ List<Port> list = nullToEmpty(optionalPorts.get().getPort());
for (Port port : list) {
if (NeutronUtils.isPortVnicTypeNormal(port)) {
continue;
tunnelIpElanNameToVmMacCache.remove(tunnelIpElanNamePair);
}
+ @Nullable
public BigInteger readDesignatedSwitchesForExternalTunnel(IpAddress tunnelIp, String elanInstanceName) {
if (tunnelIp == null || elanInstanceName == null || elanInstanceName.isEmpty()) {
return null;
MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
if (designatedSwitchForTunnelOptional.isPresent()) {
List<DesignatedSwitchForTunnel> list =
- designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel();
+ nullToEmpty(designatedSwitchForTunnelOptional.get().getDesignatedSwitchForTunnel());
for (DesignatedSwitchForTunnel designatedSwitchForTunnel : list) {
if (dpId.equals(BigInteger.valueOf(designatedSwitchForTunnel.getDpId()))) {
return true;
public java.util.Optional<SubnetToDhcpPort> getSubnetDhcpPortData(String elanInstanceName) {
java.util.Optional<SubnetToDhcpPort> optSubnetDhcp = java.util.Optional.empty();
Uuid nwUuid = new Uuid(elanInstanceName);
- List<Uuid> subnets = DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid);
+ List<Uuid> subnets = nullToEmpty(DhcpServiceUtils.getSubnetIdsFromNetworkId(broker, nwUuid));
for (Uuid subnet : subnets) {
if (DhcpServiceUtils.isIpv4Subnet(broker, subnet)) {
optSubnetDhcp = DhcpServiceUtils.getSubnetDhcpPortData(broker, subnet.getValue());
ConcurrentMap<String, L2GatewayDevice> l2GwDevices =
ElanL2GwCacheUtils.getInvolvedL2GwDevices(tunnelElanPair.getRight());
for (L2GatewayDevice device : l2GwDevices.values()) {
- if (device.getTunnelIp().equals(tunnelElanPair.getLeft())) {
+ if (Objects.equals(device.getTunnelIp(), tunnelElanPair.getLeft())) {
return true;
}
}
vniMacAddressToPortCache.remove(vniMacAddressPair);
}
+ @Nullable
public Port readVniMacToPortCache(BigInteger vni, String macAddress) {
if (macAddress == null) {
return null;
return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, psNodeId, dataBroker);
}
+ @Nullable
public RemoteMcastMacs createRemoteMcastMac(Node dstDevice, String logicalSwitchName, IpAddress internalTunnelIp) {
Set<LocatorSet> locators = new HashSet<>();
TerminationPointKey terminationPointKey = HwvtepSouthboundUtils.getTerminationPointKey(
private void putRemoteMcastMac(TypedWriteTransaction<Configuration> transaction, String elanName,
L2GatewayDevice device, IpAddress internalTunnelIp) {
Optional<Node> optionalNode = getNode(broker, device.getHwvtepNodeId());
- Node dstNode = optionalNode.get();
- if (dstNode == null) {
+ if (!optionalNode.isPresent()) {
LOG.trace("could not get device node {} ", device.getHwvtepNodeId());
return;
}
+ Node dstNode = optionalNode.get();
RemoteMcastMacs macs = createRemoteMcastMac(dstNode, elanName, internalTunnelIp);
HwvtepUtils.addRemoteMcastMac(transaction, dstNode.getNodeId(), macs);
}
});
}
+ @Nullable
private L2GatewayDevice getDeviceFromTunnelIp(IpAddress tunnelIp) {
Collection<L2GatewayDevice> devices = l2GatewayCache.getAll();
LOG.trace("In getDeviceFromTunnelIp devices {}", devices);
});
}
+ @Nullable
public IpAddress getTunnelIpBasedOnElan(String elanInstanceName, String vmMacAddress) {
LOG.trace("DhcpExternalTunnelManager getTunnelIpBasedOnElan elanInstanceName {}", elanInstanceName);
IpAddress tunnelIp = null;
import com.google.common.base.Optional;
import java.util.List;
import java.util.function.Predicate;
+import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
this.l2GatewayCache = l2GatewayCache;
}
+ @Nullable
public IpAddress getHwvtepNodeTunnelIp(InstanceIdentifier<Node> nodeIid) {
String nodeId = nodeIid.firstKeyOf(Node.class).getNodeId().getValue();
L2GatewayDevice targetDevice = null;
}
+ @Nullable
private IpAddress getTunnelIp(InstanceIdentifier<Node> nodeIid) {
Optional<Node> nodeOptional =
MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, nodeIid);
this.dhcpOptLeaseTime = leaseTime;
}
+ @Nullable
public Subnet getNeutronSubnet(Port port) {
if (port != null) {
// DHCP Service is only interested in IPv4 IPs/Subnets
return null;
}
+ @Nullable
public Subnet getNeutronSubnet(List<FixedIps> fixedIps) {
for (FixedIps fixedIp: fixedIps) {
if (fixedIp.getIpAddress().getIpv4Address() != null) {
return null;
}
+ @Nullable
private Subnet getNeutronSubnet(Uuid subnetId) {
Subnet subnet = null;
InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet
return subnet;
}
+ @Nullable
public Port getNeutronPort(String name) {
Port prt = null;
InstanceIdentifier<Port> inst = InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class,
import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty;
import java.math.BigInteger;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
LOG.trace("Port changed to {}", update);
//With Ipv6 changes we can get ipv4 subnets later. The below check is to support such scenario.
- if (original.getFixedIps().size() < update.getFixedIps().size()) {
+ if (nullToEmpty(original.getFixedIps()).size() < nullToEmpty(update.getFixedIps()).size()) {
final String interfaceName = update.getUuid().getValue();
- List<FixedIps> updatedFixedIps = update.getFixedIps();
+ List<FixedIps> updatedFixedIps = new ArrayList<>(nullToEmpty(update.getFixedIps()));
// Need to check only the newly added fixed ip.
- updatedFixedIps.removeAll(original.getFixedIps());
+ updatedFixedIps.removeAll(nullToEmpty(original.getFixedIps()));
Subnet subnet = dhcpManager.getNeutronSubnet(updatedFixedIps);
if (null == subnet || !subnet.isEnableDhcp()) {
LOG.trace("Subnet is null/not ipv4 or not enabled {}", subnet);
*/
package org.opendaylight.netvirt.dhcpservice;
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty;
+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.net.util.SubnetUtils;
JdkFutures.addErrorLogging(pktService.transmitPacket(output), LOG, "Transmit packet");
}
+ @Nullable
private DHCP handleDhcpPacket(DHCP dhcpPkt, String interfaceName, String macAddress, Port interfacePort,
Subnet subnet, String serverIp) {
LOG.trace("DHCP pkt rcvd {}", dhcpPkt);
return reply;
}
+ @Nullable
private DhcpInfo handleDhcpNeutronPacket(byte msgType, Port port, Subnet subnet, String serverIp) {
if (msgType == DHCPConstants.MSG_DECLINE) {
LOG.trace("DHCPDECLINE received");
}
+ @Nullable
private DhcpInfo handleDhcpAllocationPoolPacket(byte msgType, String interfaceName, String macAddress) {
try {
String networkId = dhcpAllocationPoolMgr.getNetworkByPort(interfaceName);
return dhcpInfo;
}
+ @Nullable
private DhcpInfo getDhcpInfo(Port port, Subnet subnet, String serverIp) {
DhcpInfo dhcpInfo = null;
if (port != null && subnet != null) {
return dhcpInfo;
}
+ @Nonnull
private static DhcpInfo getApDhcpInfo(AllocationPool ap, IpAddress allocatedIp) {
- DhcpInfo dhcpInfo = null;
-
String clientIp = allocatedIp.stringValue();
String serverIp = ap.getGateway().stringValue();
List<IpAddress> dnsServers = ap.getDnsServers();
- dhcpInfo = new DhcpInfo();
+ DhcpInfo dhcpInfo = new DhcpInfo();
dhcpInfo.setClientIp(clientIp).setServerIp(serverIp).setCidr(ap.getSubnet().stringValue())
.setHostRoutes(Collections.emptyList()).setDnsServersIpAddrs(dnsServers).setGatewayIp(serverIp);
* Many other modules use/need similar methods. Should
* be refactored to a common NeutronUtils module. *
*/
+ @Nullable
private static String getIpv4Address(Port port) {
- for (FixedIps fixedIp : port.getFixedIps()) {
+ for (FixedIps fixedIp : nullToEmpty(port.getFixedIps())) {
if (isIpv4Address(fixedIp.getIpAddress())) {
return fixedIp.getIpAddress().getIpv4Address().getValue();
}
return null;
}
- private static boolean isIpv4Address(IpAddress ip) {
+ private static boolean isIpv4Address(@Nullable IpAddress ip) {
return ip != null && ip.getIpv4Address() != null;
}
+ @Nullable
private Subnet getNeutronSubnet(Port port) {
return dhcpMgr.getNeutronSubnet(port);
}
+ @Nullable
private Port getNeutronPort(String interfaceName) {
return dhcpMgr.getNeutronPort(interfaceName);
}
+ @Nullable
private static DHCP getDhcpPktIn(Ethernet actualEthernetPacket) {
Ethernet ethPkt = actualEthernetPacket;
if (ethPkt.getEtherType() == (short)NwConstants.ETHTYPE_802_1Q) {
// "Consider returning a zero length array rather than null" - the eventual user of the returned byte[] likely
// expects null and it's unclear what the behavior would be if empty array was returned.
@SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
+ @Nullable
protected byte[] getDhcpPacketOut(DHCP reply, Ethernet etherPkt, String phyAddrees) {
if (reply == null) {
/*
return pktInReason == SendToController.class;
}
+ @Nullable
private String getInterfaceNameFromTag(long portTag) {
String interfaceName = null;
GetInterfaceFromIfIndexInput input =
package org.opendaylight.netvirt.dhcpservice;
import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
+import static org.opendaylight.netvirt.dhcpservice.api.DHCPUtils.nullToEmpty;
import com.google.common.base.Optional;
import java.math.BigInteger;
}
public static void setupDhcpArpRequest(BigInteger dpId, short tableId, BigInteger vni, String dhcpIpAddress,
- int lportTag, Long elanTag, boolean add, IMdsalApiManager mdsalUtil,
- TypedReadWriteTransaction<Configuration> tx)
+ int lportTag, @Nullable Long elanTag, boolean add,
+ IMdsalApiManager mdsalUtil, TypedReadWriteTransaction<Configuration> tx)
throws ExecutionException, InterruptedException {
List<MatchInfo> matches = getDhcpArpMatch(vni, dhcpIpAddress);
if (add) {
@Nonnull
private static List<BigInteger> extractDpnsFromNodes(Optional<Nodes> optionalNodes) {
return optionalNodes.toJavaUtil().map(
- nodes -> nodes.getNode().stream().map(Node::getId).filter(Objects::nonNull).map(
+ nodes -> nullToEmpty(nodes.getNode()).stream().map(Node::getId).filter(Objects::nonNull).map(
MDSALUtil::getDpnIdFromNodeName).collect(
Collectors.toList())).orElse(Collections.emptyList());
}
Optional<ElanDpnInterfacesList> elanDpnOptional =
MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInstanceIdentifier);
if (elanDpnOptional.isPresent()) {
- List<DpnInterfaces> dpns = elanDpnOptional.get().getDpnInterfaces();
+ List<DpnInterfaces> dpns = nullToEmpty(elanDpnOptional.get().getDpnInterfaces());
for (DpnInterfaces dpnInterfaces : dpns) {
elanDpns.add(dpnInterfaces.getDpId());
}
return elanDpns;
}
+ @Nullable
public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
.state.Interface getInterfaceFromOperationalDS(String interfaceName, DataBroker dataBroker) {
org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
}
+ @Nullable
public static String getSegmentationId(Uuid networkId, DataBroker broker) {
InstanceIdentifier<Network> inst = InstanceIdentifier.create(Neutron.class)
.child(Networks.class).child(Network.class, new NetworkKey(networkId));
return MDSALUtil.NODE_PREFIX + MDSALUtil.SEPARATOR + dpnId.toString();
}
+ @Nullable
public static String getTrunkPortMacAddress(String parentRefName,
DataBroker broker) {
InstanceIdentifier<Port> portInstanceIdentifier =
return existingEntry.get().getMacAddress();
}
+ @Nullable
private static String getNeutronMacAddress(String interfaceName, DhcpManager dhcpManager) {
Port port = dhcpManager.getNeutronPort(interfaceName);
if (port != null) {
return null;
}
+ @Nullable
public static List<Uuid> getSubnetIdsFromNetworkId(DataBroker broker, Uuid networkId) {
InstanceIdentifier id = buildNetworkMapIdentifier(networkId);
Optional<NetworkMap> optionalNetworkMap = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
}
static InstanceIdentifier<NetworkMap> buildNetworkMapIdentifier(Uuid networkId) {
- InstanceIdentifier<NetworkMap> id = InstanceIdentifier.builder(NetworkMaps.class).child(NetworkMap.class, new
- NetworkMapKey(networkId)).build();
- return id;
+ return InstanceIdentifier.builder(NetworkMaps.class).child(NetworkMap.class,
+ new NetworkMapKey(networkId)).build();
}
public static boolean isIpv4Subnet(DataBroker broker, Uuid subnetUuid) {
final Optional<Subnet> subnet = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, subnetidentifier);
if (subnet.isPresent()) {
Class<? extends IpVersionBase> ipVersionBase = subnet.get().getIpVersion();
- if (ipVersionBase.equals(IpVersionV4.class)) {
- return true;
- }
+ return IpVersionV4.class.equals(ipVersionBase);
}
return false;
}
import com.google.common.base.Optional;
import java.math.BigInteger;
+import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
DhcpServiceUtils.getListOfDpns(broker), designatedDpnId, macAddress);
}
+ @Nullable
private LogicalSwitches getLogicalSwitches(LocalUcastMacs ucastMacs) {
InstanceIdentifier<LogicalSwitches> logicalSwitchRef =
(InstanceIdentifier<LogicalSwitches>)ucastMacs.getLogicalSwitchRef().getValue();
*/
package org.opendaylight.netvirt.dhcpservice.shell;
+import javax.annotation.Nullable;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.opendaylight.netvirt.dhcpservice.api.IDhcpExternalTunnelManager;
}
@Override
+ @Nullable
protected Object doExecute() {
session.getConsole().println("Printing Designated Dpns To TunnelIp and ElanName cache "
+ "(DesignatedDpnsToTunnelIpElanNameCache)");