use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.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);
final StringBuilder routeDistiguisher = new StringBuilder();
switch(rdType) {
case AS_2BYTE:
routeDistiguisher.append(type);
routeDistiguisher.append(SEPARATOR);
routeDistiguisher.append(buffer.readUnsignedShort());
routeDistiguisher.append(SEPARATOR);
routeDistiguisher.append(buffer.readUnsignedInt());
return new RouteDistinguisher(new RdTwoOctetAs(routeDistiguisher.toString()));
case IPV4:
routeDistiguisher.append(Ipv4Util.addressForByteBuf(buffer).getValue());
routeDistiguisher.append(SEPARATOR);
routeDistiguisher.append(buffer.readUnsignedShort());
return new RouteDistinguisher(new RdIpv4(routeDistiguisher.toString()));
case AS_4BYTE:
routeDistiguisher.append(buffer.readUnsignedInt());
routeDistiguisher.append(SEPARATOR);
routeDistiguisher.append(buffer.readUnsignedShort());
return new RouteDistinguisher(new RdAs(routeDistiguisher.toString()));
default:
// in order to get the byte index correct
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.toString());
throw new IllegalArgumentException("Invalid Route Distinguisher type " + type);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.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.linkstate.rev171207.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());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.RouteDistinguisher in project bgpcep by opendaylight.
the class FlowspecL3vpnIpv6NlriParserTest method testParseMpReachNlri.
@Test
public void testParseMpReachNlri() throws BGPParsingException {
final List<Flowspec> fs = new ArrayList<>();
final MpReachNlriBuilder mp = new MpReachNlriBuilder();
final FlowspecBuilder builder = new FlowspecBuilder();
final DestinationIpv6PrefixCase destinationPrefix = new DestinationIpv6PrefixCaseBuilder().setDestinationPrefix(new Ipv6Prefix("102:304:500::/40")).build();
builder.setFlowspecType(destinationPrefix);
fs.add(builder.build());
final SourceIpv6PrefixCase sourcePrefix = new SourceIpv6PrefixCaseBuilder().setSourcePrefix(new Ipv6Prefix("102:304:600::/40")).build();
builder.setFlowspecType(sourcePrefix);
fs.add(builder.build());
final List<NextHeaders> nextheaders = Lists.newArrayList(new NextHeadersBuilder().setOp(new NumericOperand(false, true, true, false, false)).setValue((short) 6).build());
final NextHeaderCase headersCase = new NextHeaderCaseBuilder().setNextHeaders(nextheaders).build();
builder.setFlowspecType(headersCase);
fs.add(builder.build());
final MpReachNlriBuilder result = new MpReachNlriBuilder();
result.setAfi(Ipv6AddressFamily.class);
result.setSafi(FlowspecL3vpnSubsequentAddressFamily.class);
this.FS_PARSER.parseNlri(Unpooled.wrappedBuffer(REACHED_NLRI), result);
DestinationFlowspecL3vpnIpv6 flowspecDst = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv6Case) result.getAdvertizedRoutes().getDestinationType()).getDestinationFlowspecL3vpnIpv6();
final List<Flowspec> flows = flowspecDst.getFlowspec();
final RouteDistinguisher rd = flowspecDst.getRouteDistinguisher();
assertEquals(3, flows.size());
assertEquals(ROUTE_DISTINGUISHER, new String(rd.getValue()));
assertEquals(destinationPrefix, flows.get(0).getFlowspecType());
assertEquals(sourcePrefix, flows.get(1).getFlowspecType());
assertEquals(headersCase, flows.get(2).getFlowspecType());
mp.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecL3vpnIpv6CaseBuilder().setDestinationFlowspecL3vpnIpv6(new DestinationFlowspecL3vpnIpv6Builder().setRouteDistinguisher(rd).setFlowspec(fs).build()).build()).build());
final ByteBuf buffer = Unpooled.buffer();
this.FS_PARSER.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mp.setAfi(Ipv6AddressFamily.class).build()).build()).build(), buffer);
assertArrayEquals(REACHED_NLRI, ByteArray.readAllBytes(buffer));
assertEquals("all packets to 102:304:500::/40 AND from 102:304:600::/40 AND where next header equals to 6 ", this.FS_PARSER.stringNlri(flows));
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.RouteDistinguisher in project bgpcep by opendaylight.
the class FlowspecL3vpnIpv6NlriParserTest method testParseMpUnreachNlriConstraint.
@Test
public void testParseMpUnreachNlriConstraint() throws BGPParsingException {
final List<Flowspec> fs = new ArrayList<>();
final MpUnreachNlriBuilder mp = new MpUnreachNlriBuilder();
final FlowspecBuilder builder = new FlowspecBuilder();
final FragmentCase fragment = createFragment();
builder.setFlowspecType(fragment);
fs.add(builder.build());
final FlowspecType label = createLabel();
builder.setFlowspecType(label);
fs.add(builder.build());
final MpUnreachNlriBuilder result = new MpUnreachNlriBuilder();
result.setAfi(Ipv6AddressFamily.class);
result.setSafi(FlowspecL3vpnSubsequentAddressFamily.class);
this.FS_PARSER.parseNlri(Unpooled.wrappedBuffer(UNREACHED_NLRI_ADD_PATH), result, this.constraint);
final DestinationFlowspecL3vpnIpv6 flowspecDst = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecL3vpnIpv6Case) result.getWithdrawnRoutes().getDestinationType()).getDestinationFlowspecL3vpnIpv6();
final List<Flowspec> flows = flowspecDst.getFlowspec();
final RouteDistinguisher rd = flowspecDst.getRouteDistinguisher();
assertEquals(2, flows.size());
assertEquals(ROUTE_DISTINGUISHER, new String(rd.getValue()));
assertEquals(fragment, flows.get(0).getFlowspecType());
assertEquals(label, flows.get(1).getFlowspecType());
mp.setAfi(Ipv6AddressFamily.class).setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecL3vpnIpv6CaseBuilder().setDestinationFlowspecL3vpnIpv6(new DestinationFlowspecL3vpnIpv6Builder().setRouteDistinguisher(rd).setPathId(PATH_ID).setFlowspec(fs).build()).build()).build());
final ByteBuf buffer = Unpooled.buffer();
this.FS_PARSER.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mp.build()).build()).build(), buffer);
assertArrayEquals(UNREACHED_NLRI_ADD_PATH, ByteArray.readAllBytes(buffer));
assertEquals("all packets where fragment does match 'IS FIRST' 'IS LAST' 'IS A' AND where flow label equals to 16777222 or equals to 258 ", this.FS_PARSER.stringNlri(flows));
}
Aggregations