package org.opendaylight.openflowplugin.applications.lldpspeaker;
import static org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils.getValueForLLDPPacketIntegrityEnsuring;
-import static org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV.CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
//Create LLDP CustomSec TLV
byte[] pureValue = getValueForLLDPPacketIntegrityEnsuring(nodeConnectorId);
- byte[] customSecValue = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC, pureValue);
+ byte[] customSecValue = LLDPTLV.createSecSubTypeCustomTLVValue(pureValue);
LLDPTLV customSecTlv = new LLDPTLV();
customSecTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customSecValue.length)
.setValue(customSecValue);
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.openflowplugin.applications.topology.lldp.LLDPActivator;
-import org.opendaylight.openflowplugin.libraries.liblldp.BitBufferHelper;
-import org.opendaylight.openflowplugin.libraries.liblldp.CustomTLVKey;
+import org.opendaylight.openflowplugin.libraries.liblldp.BufferException;
import org.opendaylight.openflowplugin.libraries.liblldp.Ethernet;
import org.opendaylight.openflowplugin.libraries.liblldp.LLDP;
import org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV;
throw new Exception("Node id wasn't specified via systemNameId in LLDP packet.");
}
- final LLDPTLV nodeConnectorIdLldptlv = lldp.getCustomTLV(new CustomTLVKey(
- BitBufferHelper.getInt(LLDPTLV.OFOUI), LLDPTLV.CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID[0]));
+ final LLDPTLV nodeConnectorIdLldptlv = lldp.getCustomTLV(LLDPTLV.createPortSubTypeCustomTLVKey());
if (nodeConnectorIdLldptlv != null) {
srcNodeConnectorId = new NodeConnectorId(LLDPTLV.getCustomString(
nodeConnectorIdLldptlv.getValue(), nodeConnectorIdLldptlv.getLength()));
}
private static boolean checkExtraAuthenticator(LLDP lldp, NodeConnectorId srcNodeConnectorId)
- throws NoSuchAlgorithmException {
- final LLDPTLV hashLldptlv = lldp.getCustomTLV(
- new CustomTLVKey(BitBufferHelper.getInt(LLDPTLV.OFOUI), LLDPTLV.CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC[0]));
+ throws NoSuchAlgorithmException, BufferException {
+ final LLDPTLV hashLldptlv = lldp.getCustomTLV(LLDPTLV.createSecSubTypeCustomTLVKey());
boolean secAuthenticatorOk = false;
if (hashLldptlv != null) {
byte[] rawTlvValue = hashLldptlv.getValue();
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
-/**
- *
- */
package org.opendaylight.openflowplugin.libraries.liblldp;
import java.util.Arrays;
+import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
LOG.error("getShort", new BufferException("Container is too small for the number of requested bits"));
}
int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
- byte[] bits = null;
try {
- bits = BitBufferHelper.getBits(data, startOffset, numBits);
+ byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
+ return (short) toNumber(bits, numBits);
} catch (final BufferException e) {
- LOG.error("", e);
+ LOG.error("getBits failed", e);
}
- return (short) toNumber(bits, numBits);
+ return 0;
}
/**
LOG.error("getInt", new BufferException("Container is too small for the number of requested bits"));
}
int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
- byte[] bits = null;
try {
- bits = BitBufferHelper.getBits(data, startOffset, numBits);
+ byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
+ return (int) toNumber(bits, numBits);
} catch (final BufferException e) {
- LOG.error("", e);
+ LOG.error("getBits failed", e);
}
- return (int) toNumber(bits, numBits);
+ return 0;
}
/**
}
}
int startOffset = data.length * NetUtils.NUM_BITS_IN_A_BYTE - numBits;
- byte[] bits = null;
try {
- bits = BitBufferHelper.getBits(data, startOffset, numBits);
+ byte[] bits = BitBufferHelper.getBits(data, startOffset, numBits);
+ return toNumber(bits, numBits);
} catch (final BufferException e) {
- LOG.error("", e);
+ LOG.error("getBits failed", e);
}
- return toNumber(bits, numBits);
+ return 0;
}
/**
* when the startOffset and numBits parameters are not congruent
* with the data buffer size
*/
+ @Nonnull
public static byte[] getBits(final byte[] data, final int startOffset, final int numBits) throws BufferException {
int startByteOffset = 0;
int extranumBits = numBits % NetUtils.NUM_BITS_IN_A_BYTE;
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-/**
- * @file ConstructionException.java
- *
- *
- * @brief Describe an exception that is raised when a construction
- * for a Node/NodeConnector/Edge or any of the SAL basic object fails
- * because input passed are not valid or compatible
- *
- *
- */
-package org.opendaylight.openflowplugin.libraries.liblldp;
-
-public class ConstructionException extends Exception {
- private static final long serialVersionUID = 1L;
-
- public ConstructionException(final String message) {
- super(message);
- }
-}
* Abstract base class for a Datalink Address.
*/
@XmlRootElement
-public abstract class DataLinkAddress implements Serializable {
+public abstract class DataLinkAddress implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String name;
this.name = name;
}
- /**
- * Used to copy the DataLinkAddress in a polymorphic way.
- *
- * @return A clone of this DataLinkAddress
- */
- @Override
- public abstract DataLinkAddress clone();
-
/**
* Allow to distinguish among different data link addresses.
*
public static int getEtherTypeNumberInt(final String name) {
if (name.matches(REGEX_NUMBER_STRING)) {
- return Integer.valueOf(name);
+ return Integer.parseInt(name);
}
for (EtherTypes type : EtherTypes.values()) {
if (type.description.equalsIgnoreCase(name)) {
// TODO: This has to be outside and it should be possible for osgi
// to add new coming packet classes
- public static final Map<Short, Class<? extends Packet>> ETHER_TYPE_CLASS_MAP = new HashMap<>();
+ private static final Map<Short, Class<? extends Packet>> ETHER_TYPE_CLASS_MAP = new HashMap<>();
static {
ETHER_TYPE_CLASS_MAP.put(EtherTypes.LLDP.shortValue(), LLDP.class);
package org.opendaylight.openflowplugin.libraries.liblldp;
import java.util.Arrays;
+import java.util.Objects;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.NONE)
public class EthernetAddress extends DataLinkAddress {
private static final long serialVersionUID = 1L;
- @XmlTransient
- private byte[] macAddress;
public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
public static final String ADDRESS_NAME = "Ethernet MAC Address";
public static final int SIZE = 6;
+ @XmlTransient
+ private byte[] macAddress;
+
private static EthernetAddress createWellKnownAddress(final byte[] mac) {
- try {
- return new EthernetAddress(mac);
- } catch (final ConstructionException ce) {
- return null;
- }
+ return new EthernetAddress(mac);
}
/* Private constructor to satisfy JAXB */
*
* @param macAddress A byte array in big endian format representing the Ethernet MAC Address
*/
- public EthernetAddress(final byte[] macAddress) throws ConstructionException {
+ public EthernetAddress(final byte[] macAddress) {
super(ADDRESS_NAME);
- if (macAddress == null) {
- throw new ConstructionException("Null input parameter passed");
- }
+ Objects.requireNonNull(macAddress);
if (macAddress.length != SIZE) {
- throw new ConstructionException(
+ throw new IllegalArgumentException(
"Wrong size of passed byte array, expected:" + SIZE
+ " got:" + macAddress.length);
}
@Override
public EthernetAddress clone() {
try {
- return new EthernetAddress(this.macAddress.clone());
- } catch (final ConstructionException ce) {
- return null;
+ EthernetAddress cloned = (EthernetAddress)super.clone();
+ cloned.macAddress = this.macAddress.clone();
+ return cloned;
+ } catch (CloneNotSupportedException e) {
+ // This should never happen
+ throw new AssertionError(e);
}
}
- /**
- * Return the Ethernet Mac address in byte array format.
- *
- * @return The Ethernet Mac address in byte array format
- */
- public byte[] getValue() {
- return this.macAddress;
- }
-
@Override
public int hashCode() {
final int prime = 31;
package org.opendaylight.openflowplugin.libraries.liblldp;
import com.google.common.collect.Iterables;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
private static final String TTL = "TTL";
private static final int LLDP_DEFAULT_TLVS = 3;
private static final LLDPTLV EMPTY_TLV = new LLDPTLV().setLength((short) 0).setType((byte) 0);
+ @SuppressFBWarnings("MS_PKGPROTECT")
public static final byte[] LLDP_MULTICAST_MAC = { 1, (byte) 0x80, (byte) 0xc2, 0, 0, (byte) 0xe };
private Map<Byte, LLDPTLV> mandatoryTLVs;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
private static final int LLDPTLV_FIELDS = 3;
/** OpenFlow OUI. */
- public static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26,
- (byte) 0xe1 };
+ static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26, (byte) 0xe1 };
/** Length of Organizationally defined subtype field of TLV in bytes. */
private static final byte CUSTOM_TLV_SUB_TYPE_LENGTH = (byte)1;
/** OpenFlow subtype: nodeConnectorId of source. */
- public static final byte[] CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID = new byte[] { 0 };
+ private static final byte[] CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID = new byte[] { 0 };
/** OpenFlow subtype: custom sec = hash code of verification of origin of LLDP. */
- public static final byte[] CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC = new byte[] { 1 };
+ private static final byte[] CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC = new byte[] { 1 };
- public static final int CUSTOM_TLV_OFFSET = OFOUI.length + CUSTOM_TLV_SUB_TYPE_LENGTH;
- public static final byte[] CHASSISID_SUB_TYPE = new byte[] { 4 }; // MAC address for the system
- public static final byte[] PORTID_SUB_TYPE = new byte[] { 7 }; // locally assigned
+ private static final int CUSTOM_TLV_OFFSET = OFOUI.length + CUSTOM_TLV_SUB_TYPE_LENGTH;
+ private static final byte[] CHASSISID_SUB_TYPE = new byte[] { 4 }; // MAC address for the system
+ private static final byte[] PORTID_SUB_TYPE = new byte[] { 7 }; // locally assigned
public enum TLVType {
Unknown((byte) 0), ChassisID((byte) 1), PortID((byte) 2), TTL((byte) 3), PortDesc(
* @return the SystemName TLV value in byte array
*/
public static byte[] createSystemNameTLVValue(final String nodeId) {
- byte[] nid = nodeId.getBytes();
- return nid;
+ return nodeId.getBytes(StandardCharsets.UTF_8);
}
/**
return customValue;
}
+ /**
+ * Creates a custom TLV value including OUI of sub type custom sec and custom bytes value.
+ *
+ * @param customValue the custom value
+ * @return the custom TLV value in byte array
+ */
+ public static byte[] createSecSubTypeCustomTLVValue(final byte[] customValue) {
+ return createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC, customValue);
+ }
+
/**
* Retrieves the string from TLV value and returns it in HexString format.
*
byte[] value = lldptlv.getValue();
return BitBufferHelper.getByte(ArrayUtils.subarray(value, 3, 4));
}
+
+ public static CustomTLVKey createPortSubTypeCustomTLVKey() throws BufferException {
+ return new CustomTLVKey(BitBufferHelper.getInt(OFOUI), CUSTOM_TLV_SUB_TYPE_NODE_CONNECTOR_ID[0]);
+ }
+
+ public static CustomTLVKey createSecSubTypeCustomTLVKey() throws BufferException {
+ return new CustomTLVKey(BitBufferHelper.getInt(LLDPTLV.OFOUI), LLDPTLV.CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC[0]);
+ }
}
return false;
}
if (values.length >= 2) {
- int prefix = Integer.valueOf(values[1]);
+ int prefix = Integer.parseInt(values[1]);
if (prefix < 0 || prefix > 32) {
return false;
}
}
if (values.length >= 2) {
- int prefix = Integer.valueOf(values[1]);
+ int prefix = Integer.parseInt(values[1]);
if (prefix < 0 || prefix > 128) {
return false;
}
package org.opendaylight.openflowplugin.libraries.liblldp;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
*
* @return The raw payload if not parsable as an array of bytes, null otherwise
*/
+ @SuppressFBWarnings("EI_EXPOSE_REP")
public byte[] getRawPayload() {
return rawPayload;
}
@Override
public boolean equals(final Object obj) {
+ if (obj == null) {
+ return false;
+ }
+
if (this == obj) {
return true;
}
if (hdrFieldsMap == null || other.hdrFieldsMap == null) {
return false;
}
- if (hdrFieldsMap != null && other.hdrFieldsMap != null) {
- for (String field : hdrFieldsMap.keySet()) {
- if (!Arrays.equals(hdrFieldsMap.get(field), other.hdrFieldsMap.get(field))) {
- return false;
- }
+ for (Entry<String, byte[]> entry : hdrFieldsMap.entrySet()) {
+ String field = entry.getKey();
+ if (!Arrays.equals(entry.getValue(), other.hdrFieldsMap.get(field))) {
+ return false;
}
- } else {
- return false;
}
return true;
}
*/
package org.opendaylight.openflowplugin.libraries.sal.packet.address;
+import static org.junit.Assert.fail;
+
import org.junit.Assert;
import org.junit.Test;
-import org.opendaylight.openflowplugin.libraries.liblldp.ConstructionException;
import org.opendaylight.openflowplugin.libraries.liblldp.EthernetAddress;
public class EthernetAddressTest {
ea1 = new EthernetAddress((byte[]) null);
// Exception is expected if NOT raised test will fail
- Assert.assertTrue(false);
- } catch (final ConstructionException e) {
+ fail("Expected NullPointerException");
+ } catch (final NullPointerException e) {
// expected
}
ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0 });
// Exception is expected if NOT raised test will fail
- Assert.assertTrue(false);
- } catch (final ConstructionException e) {
+ fail("Expected IllegalArgumentException");
+ } catch (final IllegalArgumentException e) {
// expected
}
(byte) 0x0, (byte) 0x0, (byte) 0x0 });
// Exception is expected if NOT raised test will fail
- Assert.assertTrue(false);
- } catch (final ConstructionException e) {
+ fail("Expected IllegalArgumentException");
+ } catch (final IllegalArgumentException e) {
// expected
}
}
@Test
public void testEquality() {
- EthernetAddress ea1;
- EthernetAddress ea2;
- try {
- ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
-
- ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
- Assert.assertTrue(ea1.equals(ea2));
- } catch (final ConstructionException e) {
- // Exception is NOT expected if raised test will fail
- Assert.assertTrue(false);
- }
+ EthernetAddress ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
- try {
- ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
-
- ea2 = ea1.clone();
- Assert.assertTrue(ea1.equals(ea2));
- } catch (final ConstructionException e) {
- // Exception is NOT expected if raised test will fail
- Assert.assertTrue(false);
- }
+ EthernetAddress ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+ Assert.assertTrue(ea1.equals(ea2));
+
+ ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+
+ ea2 = ea1.clone();
+ Assert.assertTrue(ea1.equals(ea2));
// Check for well knowns
- try {
- ea1 = EthernetAddress.BROADCASTMAC;
- ea2 = new EthernetAddress(new byte[] { (byte) 0xff, (byte) 0xff,
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
- Assert.assertTrue(ea1.equals(ea2));
- } catch (final ConstructionException e) {
- // Exception is NOT expected if raised test will fail
- Assert.assertTrue(false);
- }
+
+ ea1 = EthernetAddress.BROADCASTMAC;
+ ea2 = new EthernetAddress(new byte[] { (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
+ Assert.assertTrue(ea1.equals(ea2));
}
@Test
public void testUnEquality() {
- EthernetAddress ea1;
- EthernetAddress ea2;
- try {
- ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2 });
-
- ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
- (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
- Assert.assertTrue(!ea1.equals(ea2));
- } catch (final ConstructionException e) {
- // Exception is NOT expected if raised test will fail
- Assert.assertTrue(false);
- }
+ EthernetAddress ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2 });
+
+ EthernetAddress ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
+ (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
+ Assert.assertTrue(!ea1.equals(ea2));
}
}