2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.ietf.stateful07;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.BitSet;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvParser;
17 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
18 import org.opendaylight.protocol.pcep.spi.TlvUtil;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.protocol.util.Ipv6Util;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.ErrorType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpError;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpErrorBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserError;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserErrorBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags;
39 * Parser for {@link RsvpErrorSpec}
41 public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
43 public static final int TYPE = 21;
45 private static final int FLAGS_F_LENGTH = 1;
47 private static final int RSVP_ERROR_CLASS_NUM = 6;
48 private static final int RSVP_IPV4_ERROR_CLASS_TYPE = 1;
49 private static final int RSVP_IPV6_ERROR_CLASS_TYPE = 2;
51 private static final int USER_ERROR_CLASS_NUM = 194;
52 private static final int USER_ERROR_CLASS_TYPE = 1;
54 private static final int IN_PLACE_FLAG_OFFSET = 7;
55 private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
58 public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
62 final int classNum = UnsignedBytes.toInt(buffer.readByte());
63 final int classType = UnsignedBytes.toInt(buffer.readByte());
64 ErrorType errorType = null;
65 if (classNum == RSVP_ERROR_CLASS_NUM) {
66 errorType = parseRsvp(classType, buffer.slice());
67 } else if (classNum == USER_ERROR_CLASS_NUM && classType == USER_ERROR_CLASS_TYPE) {
68 errorType = parseUserError(buffer.slice());
70 return new RsvpErrorSpecBuilder().setErrorType(errorType).build();
74 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
75 Preconditions.checkArgument(tlv != null, "RSVPErrorSpecTlv is mandatory.");
76 final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv;
77 final ByteBuf body = Unpooled.buffer();
78 if (rsvp.getErrorType().getImplementedInterface().equals(RsvpCase.class)) {
79 final RsvpCase r = (RsvpCase) rsvp.getErrorType();
80 serializeRsvp(r.getRsvpError(), body);
81 TlvUtil.formatTlv(TYPE, body, buffer);
83 final UserCase u = (UserCase) rsvp.getErrorType();
84 serializerUserError(u.getUserError(), body);
85 TlvUtil.formatTlv(TYPE, body, buffer);
89 private UserCase parseUserError(final ByteBuf buffer) {
90 final UserErrorBuilder error = new UserErrorBuilder();
91 error.setEnterprise(new EnterpriseNumber(buffer.readUnsignedInt()));
92 error.setSubOrg((short) UnsignedBytes.toInt(buffer.readByte()));
93 final int errDescrLength = UnsignedBytes.toInt(buffer.readByte());
94 error.setValue(buffer.readUnsignedShort());
95 error.setDescription(ByteArray.bytesToHRString(ByteArray.readBytes(buffer, errDescrLength)));
96 // if we have any subobjects, place the implementation here
97 return new UserCaseBuilder().setUserError(error.build()).build();
100 private void serializerUserError(final UserError ue, final ByteBuf body) {
101 byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
102 body.writeByte(USER_ERROR_CLASS_NUM);
103 body.writeByte(USER_ERROR_CLASS_TYPE);
104 body.writeInt(ue.getEnterprise().getValue().intValue());
105 body.writeByte(ue.getSubOrg());
106 body.writeByte(desc.length);
107 body.writeShort(ue.getValue().shortValue());
108 body.writeBytes(desc);
111 private RsvpCase parseRsvp(final int classType, final ByteBuf buffer) {
112 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
113 if (classType == RSVP_IPV4_ERROR_CLASS_TYPE) {
114 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
115 } else if (classType == RSVP_IPV6_ERROR_CLASS_TYPE) {
116 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
118 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
119 builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
120 final short errorCode = buffer.readUnsignedByte();
121 builder.setCode(errorCode);
122 final int errorValue = buffer.readUnsignedShort();
123 builder.setValue(errorValue);
124 return new RsvpCaseBuilder().setRsvpError(builder.build()).build();
127 private void serializeRsvp(final RsvpError rsvp, final ByteBuf body) {
128 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
129 flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
130 flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
131 final IpAddress node = rsvp.getNode();
132 if (node.getIpv4Address() != null) {
133 body.writeByte(RSVP_ERROR_CLASS_NUM);
134 body.writeByte(RSVP_IPV4_ERROR_CLASS_TYPE);
135 body.writeBytes(Ipv4Util.bytesForAddress(node.getIpv4Address()));
137 body.writeByte(RSVP_ERROR_CLASS_NUM);
138 body.writeByte(RSVP_IPV6_ERROR_CLASS_TYPE);
139 body.writeBytes(Ipv6Util.bytesForAddress(node.getIpv6Address()));
141 body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
142 body.writeByte(rsvp.getCode());
143 body.writeShort(rsvp.getValue().shortValue());