use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher in project bgpcep by opendaylight.
the class RouteDistinguisherUtil method serializeRouteDistinquisher.
/**
* Serializes route distinguisher according to type and writes into ByteBuf.
*/
public static void serializeRouteDistinquisher(final RouteDistinguisher distinguisher, final ByteBuf byteAggregator) {
requireNonNull(distinguisher);
Preconditions.checkState(byteAggregator != null && byteAggregator.isWritable(RD_LENGTH), "Cannot write Route Distinguisher to provided buffer.");
if (distinguisher.getRdTwoOctetAs() != null) {
final String[] values = distinguisher.getRdTwoOctetAs().getValue().split(SEPARATOR);
byteAggregator.writeShort(RDType.AS_2BYTE.value);
ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
final long assignedNumber = Integer.parseUnsignedInt(values[2]);
ByteBufWriteUtil.writeUnsignedInt(assignedNumber, byteAggregator);
} else if (distinguisher.getRdAs() != null) {
final String[] values = distinguisher.getRdAs().getValue().split(SEPARATOR);
byteAggregator.writeShort(RDType.AS_4BYTE.value);
final long admin = Integer.parseUnsignedInt(values[0]);
ByteBufWriteUtil.writeUnsignedInt(admin, byteAggregator);
ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
} else if (distinguisher.getRdIpv4() != null) {
final String[] values = distinguisher.getRdIpv4().getValue().split(SEPARATOR);
final Ipv4Address ip = new Ipv4Address(values[0]);
byteAggregator.writeShort(RDType.IPV4.value);
ByteBufWriteUtil.writeIpv4Address(ip, byteAggregator);
ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
} else {
LOG.warn("Unable to serialize Route Distinguisher. Invalid RD value found. RD={}", distinguisher);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher in project bgpcep by opendaylight.
the class AbstractFlowspecL3vpnNlriParser method stringNlri.
@Override
public String stringNlri(final DataContainerNode<?> flowspec) {
final StringBuilder buffer = new StringBuilder();
final RouteDistinguisher rd = extractRouteDistinguisher(flowspec, RD_NID);
if (rd != null) {
buffer.append("[l3vpn with route-distinguisher ").append(rd.getValue()).append("] ");
}
buffer.append(super.stringNlri(flowspec));
return buffer.toString();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher in project netvirt by opendaylight.
the class BgpUtil method getVrfFromRd.
/**
* get the vrf with the RouterDistinguisher pass in param.
* @param rd is the RouteDistinguisher of vrf
* @return the vrf of rd or null if no exist
*/
public Vrfs getVrfFromRd(String rd) {
Vrfs vrfs = null;
KeyedInstanceIdentifier<Vrfs, VrfsKey> id = InstanceIdentifier.create(Bgp.class).child(VrfsContainer.class).child(Vrfs.class, new VrfsKey(rd));
Optional<Vrfs> vrfsFromDs = Optional.empty();
try {
vrfsFromDs = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, id);
} catch (ExecutionException | InterruptedException e) {
LOG.error("Exception while reading BGP VRF table for the Vpn Rd {}", rd, e);
}
if (vrfsFromDs.isPresent()) {
vrfs = vrfsFromDs.get();
}
return vrfs;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher in project bgpcep by opendaylight.
the class RouteDistinguisherUtil method parseRouteDistinguisher.
/**
* Parses three types of route distinguisher from given ByteBuf.
*/
public static RouteDistinguisher parseRouteDistinguisher(final ByteBuf buffer) {
Preconditions.checkState(buffer != null && buffer.isReadable(RD_LENGTH), "Cannot read Route Distinguisher from provided buffer.");
final int type = buffer.readUnsignedShort();
final RDType rdType = RDType.valueOf(type);
switch(rdType) {
case AS_2BYTE:
return new RouteDistinguisher(new RdTwoOctetAs(new StringBuilder().append(type).append(SEPARATOR).append(buffer.readUnsignedShort()).append(SEPARATOR).append(buffer.readUnsignedInt()).toString()));
case IPV4:
return new RouteDistinguisher(new RdIpv4(new StringBuilder().append(Ipv4Util.addressForByteBuf(buffer).getValue()).append(SEPARATOR).append(buffer.readUnsignedShort()).toString()));
case AS_4BYTE:
return new RouteDistinguisher(new RdAs(new StringBuilder().append(buffer.readUnsignedInt()).append(SEPARATOR).append(buffer.readUnsignedShort()).toString()));
default:
// now that this RD type is not supported, we want to read the remain 6 bytes
// in order to get the byte index correct
final StringBuilder routeDistiguisher = new StringBuilder();
for (int i = 0; i < 6; i++) {
routeDistiguisher.append("0x").append(Integer.toHexString(buffer.readByte() & 0xFF)).append(' ');
}
LOG.debug("Invalid Route Distinguisher: type={}, rawRouteDistinguisherValue={}", type, routeDistiguisher);
throw new IllegalArgumentException("Invalid Route Distinguisher type " + type);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher in project bgpcep by opendaylight.
the class RouteDistinguisherUtilTest method testIpv4RouteDistinguisher.
@Test
public void testIpv4RouteDistinguisher() {
final RouteDistinguisher expected = createRouteDistinguisher(1, IP_ADDRESS, IP_PORT);
final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(IP_BYTES));
assertEquals(expected.getRdIpv4(), parsed.getRdIpv4());
final ByteBuf byteAggregator = Unpooled.buffer(IP_BYTES.length);
RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
assertArrayEquals(IP_BYTES, byteAggregator.array());
assertEquals(IP_ADDRESS + SEPARATOR + IP_PORT, parsed.getRdIpv4().getValue());
}
Aggregations