use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open in project bgpcep by opendaylight.
the class BGPOpenMessageParser method parseMessageBody.
/**
* Parses given byte array to BGP Open message
*
* @param body byte array representing BGP Open message, without header
* @param messageLength the length of the message
* @return {@link Open} BGP Open Message
* @throws BGPDocumentedException if the parsing was unsuccessful
*/
@Override
public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
Preconditions.checkArgument(body != null, "Buffer cannot be null.");
if (body.readableBytes() < MIN_MSG_LENGTH) {
throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
}
final int version = body.readUnsignedByte();
if (version != BGP_VERSION) {
throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
}
final AsNumber as = new AsNumber((long) body.readUnsignedShort());
final int holdTime = body.readUnsignedShort();
if (holdTime == 1 || holdTime == 2) {
throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
}
Ipv4Address bgpId = null;
try {
bgpId = Ipv4Util.addressForByteBuf(body);
} catch (final IllegalArgumentException e) {
throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
}
final int optLength = body.readUnsignedByte();
final List<BgpParameters> optParams = new ArrayList<>();
if (optLength > 0) {
fillParams(body.slice(), optParams);
}
LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime).setBgpIdentifier(bgpId).setBgpParameters(optParams).build();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open in project bgpcep by opendaylight.
the class BGPOpenMessageParser method serializeMessage.
/**
* Serializes given BGP Open message to byte array, without the header.
*
* @param msg BGP Open message to be serialized.
* @param bytes ByteBuf where the message will be serialized
*/
@Override
public void serializeMessage(final Notification msg, final ByteBuf bytes) {
Preconditions.checkArgument(msg instanceof Open, "Message needs to be of type Open");
final Open open = (Open) msg;
final ByteBuf msgBody = Unpooled.buffer();
msgBody.writeByte(BGP_VERSION);
// When our AS number does not fit into two bytes, we report it as AS_TRANS
int openAS = open.getMyAsNumber();
if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) {
openAS = AS_TRANS;
}
msgBody.writeShort(openAS);
msgBody.writeShort(open.getHoldTimer());
msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
final ByteBuf paramsBuffer = Unpooled.buffer();
if (open.getBgpParameters() != null) {
for (final BgpParameters param : open.getBgpParameters()) {
this.reg.serializeParameter(param, paramsBuffer);
}
}
msgBody.writeByte(paramsBuffer.writerIndex());
msgBody.writeBytes(paramsBuffer);
MessageUtil.formatMessage(TYPE, msgBody, bytes);
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open in project bgpcep by opendaylight.
the class OpenTest method testSerializeOpen.
@Test
public void testSerializeOpen() throws Exception {
final List<OptionalCapabilities> optionalCapas = Lists.newArrayList();
final OptionalCapabilities optionalCapabilitiesBuilder = new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(1000L)).build()).addAugmentation(CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().setRestartFlags(new GracefulRestartCapability.RestartFlags(false)).setRestartTime(0).setTables(Collections.emptyList()).build()).build()).build()).build();
optionalCapas.add(optionalCapabilitiesBuilder);
final List<BgpParameters> tlvs = Lists.newArrayList(new BgpParametersBuilder().setOptionalCapabilities(optionalCapas).build());
final Open open = new OpenBuilder().setBgpIdentifier(new Ipv4Address("127.0.0.1")).setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).build();
final ByteBuf msg = Unpooled.buffer();
new BGPOpenMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getParameterRegistry()).serializeMessage(open, msg);
final byte[] temp = ByteArray.cutBytes(ByteArray.getAllBytes(msg), 19);
final Open openResult = new BGPOpenMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getParameterRegistry()).parseMessageBody(Unpooled.copiedBuffer(temp), temp.length);
assertEquals(open.getBgpIdentifier(), openResult.getBgpIdentifier());
assertEquals(open.getHoldTimer(), openResult.getHoldTimer());
assertEquals(open.getMyAsNumber(), openResult.getMyAsNumber());
boolean grace = false;
if (openResult.getBgpParameters().get(0).getOptionalCapabilities().get(0).getCParameters().getAs4BytesCapability() != null) {
grace = true;
}
assertEquals(open.getBgpParameters().get(0).getOptionalCapabilities().get(0).getCParameters().getAs4BytesCapability(), openResult.getBgpParameters().get(0).getOptionalCapabilities().get(!grace ? 1 : 0).getCParameters().getAs4BytesCapability());
assertEquals(open.getBgpParameters().get(0).getOptionalCapabilities().get(0).getCParameters().getAugmentation(CParameters1.class).getGracefulRestartCapability(), openResult.getBgpParameters().get(0).getOptionalCapabilities().get(grace ? 1 : 0).getCParameters().getAugmentation(CParameters1.class).getGracefulRestartCapability());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open in project bgpcep by opendaylight.
the class PCEPOpenMessageParser method serializeMessage.
@Override
public void serializeMessage(final Message message, final ByteBuf out) {
Preconditions.checkArgument(message instanceof OpenMessage, "Wrong instance of Message. Passed instance of %s. Need OpenMessage.", message.getClass());
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
Preconditions.checkArgument(open.getOpen() != null, "Open Object must be present in Open Message.");
final ByteBuf buffer = Unpooled.buffer();
serializeObject(open.getOpen(), buffer);
MessageUtil.formatMessage(TYPE, buffer, out);
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open in project bgpcep by opendaylight.
the class PCEPOpenObjectParser method serializeObject.
@Override
public void serializeObject(final Object object, final ByteBuf buffer) {
Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
final Open open = (Open) object;
final ByteBuf body = Unpooled.buffer();
writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
writeUnsignedByte(open.getKeepalive(), body);
writeUnsignedByte(open.getDeadTimer(), body);
Preconditions.checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
writeUnsignedByte(open.getSessionId(), body);
serializeTlvs(open.getTlvs(), body);
ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
Aggregations