--- /dev/null
+/*
+ * Copyright (c) 2018 AT&T Intellectual Property. 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
+ */
+
+package org.opendaylight.bgp.concepts;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import javax.annotation.Nonnull;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+
+/**
+ * Utility class for IpAddress models type(like Originator Route Ip) serialization and parsing.
+ *
+ * @author Claudio D. Gasparini
+ */
+public final class IpAddressUtil {
+ private static final int ZERO_BYTE = 1;
+
+ private IpAddressUtil() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Returns IpAddress from byte array containing ipAddress lenght + ipAddress.
+ *
+ * @param buffer containing ip address
+ * @return IpAddress
+ */
+ @Nonnull
+ public static IpAddress addressForByteBuf(@Nonnull final ByteBuf buffer) {
+ final int ipLength = buffer.readUnsignedByte();
+ if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
+ return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
+ } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
+ return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
+ }
+ throw new IllegalStateException("Unexpected size");
+ }
+
+ /**
+ * Returns IpAddress from byte array containing ipAddress based on ByteArray length.
+ *
+ * @param buffer containing ip address
+ * @return IpAddress
+ */
+ @Nonnull
+ public static IpAddress addressForByteBufWOLength(@Nonnull final ByteBuf buffer) {
+ final int ipLength = buffer.readableBytes();
+ if (ipLength == Ipv6Util.IPV6_LENGTH) {
+ return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
+ } else if (ipLength == Ipv4Util.IP4_LENGTH) {
+ return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
+ }
+ throw new IllegalStateException("Unexpected size");
+ }
+
+ /**
+ * Returns byte array containing IpAddress length and IpAddress.
+ *
+ * @param address containing ipv4 or ipv6 address
+ * @return byte array
+ */
+ @Nonnull
+ public static ByteBuf bytesFor(@Nonnull final IpAddress address) {
+ final ByteBuf body = Unpooled.buffer();
+ if (address.getIpv4Address() != null) {
+ body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
+ body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
+ } else if (address.getIpv6Address() != null) {
+ body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
+ body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
+ } else {
+ body.writeZero(ZERO_BYTE);
+ }
+ return body;
+ }
+
+ /**
+ * Returns byte array containing IpAddress.
+ *
+ * @param address containing ipv4 or ipv6 address
+ * @return byte array
+ */
+ @Nonnull
+ public static ByteBuf bytesWOLengthFor(@Nonnull final IpAddress address) {
+ final ByteBuf body = Unpooled.buffer();
+ if (address.getIpv4Address() != null) {
+ body.writeBytes(Ipv4Util.bytesForAddress(address.getIpv4Address()));
+ } else if (address.getIpv6Address() != null) {
+ body.writeBytes(Ipv6Util.bytesForAddress(address.getIpv6Address()));
+ } else {
+ body.writeZero(ZERO_BYTE);
+ }
+ return body;
+ }
+
+
+ public static IpAddress extractIpAddress(@Nonnull final DataContainerNode<?> route,
+ @Nonnull final NodeIdentifier rdNid) {
+ final NormalizedNode<?, ?> rdNode = NormalizedNodes.findNode(route, rdNid).orElse(null);
+ if (rdNode != null) {
+ return IpAddressBuilder.getDefaultInstance((String) rdNode.getValue());
+ }
+ return null;
+ }
+}
package org.opendaylight.protocol.bgp.evpn.impl.nlri;
-import static java.util.Objects.requireNonNull;
import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractOrigRouteIp;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+import org.opendaylight.bgp.concepts.IpAddressUtil;
import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEsiTypeRegistry;
-import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.es.route.EsRoute;
"Wrong length of array of bytes. Passed: %s ;", buffer);
final Esi esi = SimpleEsiTypeRegistry.getInstance().parseEsi(buffer.readSlice(ESI_SIZE));
- final IpAddress ip = requireNonNull(parseOrigRouteIp(buffer));
+ final IpAddress ip = IpAddressUtil.addressForByteBuf(buffer);
final EsRouteBuilder builder = new EsRouteBuilder().setEsi(esi).setOrigRouteIp(ip);
return new EsRouteCaseBuilder().setEsRoute(builder.build()).build();
final EsRoute evpn = ((EsRouteCase) evpnInput).getEsRoute();
final ByteBuf body = Unpooled.buffer();
SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
- final ByteBuf orig = serializeOrigRouteIp(evpn.getOrigRouteIp());
+ final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
Preconditions.checkArgument(orig.readableBytes() > 0);
body.writeBytes(orig);
return body;
builder.setOrigRouteIp(extractOrigRouteIp(evpn));
return new EsRouteCaseBuilder().setEsRoute(builder.build()).build();
}
-
- static IpAddress parseOrigRouteIp(final ByteBuf buffer) {
- final int ipLength = buffer.readUnsignedByte();
- if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
- return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
- } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
- return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
- }
- return null;
- }
-
- static ByteBuf serializeOrigRouteIp(final IpAddress origRouteIp) {
- final ByteBuf body = Unpooled.buffer();
- if (origRouteIp.getIpv4Address() != null) {
- body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
- body.writeBytes(Ipv4Util.bytesForAddress(origRouteIp.getIpv4Address()));
- } else if (origRouteIp.getIpv6Address() != null) {
- body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
- body.writeBytes(Ipv6Util.bytesForAddress(origRouteIp.getIpv6Address()));
- } else {
- body.writeZero(ZERO_BYTE);
- }
- return body;
- }
}
package org.opendaylight.protocol.bgp.evpn.impl.nlri;
-import static java.util.Objects.requireNonNull;
import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractETI;
import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractOrigRouteIp;
import com.google.common.base.Preconditions;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+import org.opendaylight.bgp.concepts.IpAddressUtil;
import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
"Wrong length of array of bytes. Passed: %s ;", buffer);
final EthernetTagId eti = new EthernetTagIdBuilder().setVlanId(buffer.readUnsignedInt()).build();
- IpAddress ip = requireNonNull(EthSegRParser.parseOrigRouteIp(buffer));
+ IpAddress ip = IpAddressUtil.addressForByteBuf(buffer);
final IncMultiEthernetTagResBuilder builder = new IncMultiEthernetTagResBuilder()
.setEthernetTagId(eti).setOrigRouteIp(ip);
return new IncMultiEthernetTagResCaseBuilder().setIncMultiEthernetTagRes(builder.build()).build();
final IncMultiEthernetTagRes evpn = ((IncMultiEthernetTagResCase) evpnChoice).getIncMultiEthernetTagRes();
final ByteBuf body = Unpooled.buffer();
ByteBufWriteUtil.writeUnsignedInt(evpn.getEthernetTagId().getVlanId(), body);
- final ByteBuf orig = EthSegRParser.serializeOrigRouteIp(evpn.getOrigRouteIp());
+ final ByteBuf orig = IpAddressUtil.bytesFor(evpn.getOrigRouteIp());
Preconditions.checkArgument(orig.readableBytes() > 0);
body.writeBytes(orig);
return body;