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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
14 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
16 import com.google.common.base.Preconditions;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.nio.charset.StandardCharsets;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.TlvParser;
22 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
23 import org.opendaylight.protocol.pcep.spi.TlvUtil;
24 import org.opendaylight.protocol.util.BitArray;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.protocol.util.Ipv4Util;
27 import org.opendaylight.protocol.util.Ipv6Util;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.RsvpErrorSpec;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.ErrorType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpError;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpErrorBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserError;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserErrorBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ErrorSpec.Flags;
45 * Parser for {@link RsvpErrorSpec}.
47 public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
49 public static final int TYPE = 21;
51 private static final int FLAGS_SIZE = 8;
52 private static final int HEADER_LENGTH = 4;
54 private static final int RSVP_ERROR_CLASS_NUM = 6;
55 private static final int RSVP_IPV4_ERROR_CLASS_TYPE = 1;
56 private static final int RSVP_IPV6_ERROR_CLASS_TYPE = 2;
58 private static final int USER_ERROR_CLASS_NUM = 194;
59 private static final int USER_ERROR_CLASS_TYPE = 1;
61 private static final int IN_PLACE = 7;
62 private static final int NOT_GUILTY = 6;
65 public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
69 // throw away contents of length field
70 buffer.readUnsignedShort();
71 final int classNum = buffer.readUnsignedByte();
72 final int classType = buffer.readUnsignedByte();
73 ErrorType errorType = null;
74 if (classNum == RSVP_ERROR_CLASS_NUM) {
75 errorType = parseRsvp(classType, buffer.slice());
76 } else if (classNum == USER_ERROR_CLASS_NUM && classType == USER_ERROR_CLASS_TYPE) {
77 errorType = parseUserError(buffer.slice());
79 return new RsvpErrorSpecBuilder().setErrorType(errorType).build();
83 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
84 Preconditions.checkArgument(tlv instanceof RsvpErrorSpec, "RSVPErrorSpecTlv is mandatory.");
85 final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv;
86 final ByteBuf body = Unpooled.buffer();
87 if (rsvp.getErrorType().implementedInterface().equals(RsvpCase.class)) {
88 final RsvpCase r = (RsvpCase) rsvp.getErrorType();
89 serializeRsvp(r.getRsvpError(), body);
90 TlvUtil.formatTlv(TYPE, body, buffer);
92 final UserCase u = (UserCase) rsvp.getErrorType();
93 serializerUserError(u.getUserError(), body);
94 TlvUtil.formatTlv(TYPE, body, buffer);
98 private static UserCase parseUserError(final ByteBuf buffer) {
99 final UserErrorBuilder error = new UserErrorBuilder();
100 error.setEnterprise(new EnterpriseNumber(buffer.readUnsignedInt()));
101 error.setSubOrg(buffer.readUnsignedByte());
102 final int errDescrLength = buffer.readUnsignedByte();
103 error.setValue(buffer.readUnsignedShort());
104 error.setDescription(ByteArray.bytesToHRString(ByteArray.readBytes(buffer, errDescrLength)));
105 // if we have any subobjects, place the implementation here
106 return new UserCaseBuilder().setUserError(error.build()).build();
109 private static void serializerUserError(final UserError ue, final ByteBuf body) {
110 final byte[] desc = ue.getDescription() == null ? new byte[0]
111 : ue.getDescription().getBytes(StandardCharsets.UTF_8);
112 final ByteBuf userErrorBuf = Unpooled.buffer();
113 Preconditions.checkArgument(ue.getEnterprise() != null, "EnterpriseNumber is mandatory");
114 writeUnsignedInt(ue.getEnterprise().getValue(), userErrorBuf);
115 writeUnsignedByte(ue.getSubOrg(), userErrorBuf);
116 userErrorBuf.writeByte(desc.length);
117 Preconditions.checkArgument(ue.getValue() != null, "Value is mandatory.");
118 writeUnsignedShort(ue.getValue(), userErrorBuf);
119 userErrorBuf.writeBytes(desc);
120 userErrorBuf.writeZero(TlvUtil.getPadding(desc.length, TlvUtil.PADDED_TO));
121 formatRSVPObject(USER_ERROR_CLASS_NUM, USER_ERROR_CLASS_TYPE, userErrorBuf, body);
124 private static RsvpCase parseRsvp(final int classType, final ByteBuf buffer) {
125 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
126 if (classType == RSVP_IPV4_ERROR_CLASS_TYPE) {
127 builder.setNode(new IpAddressNoZone(Ipv4Util.noZoneAddressForByteBuf(buffer)));
128 } else if (classType == RSVP_IPV6_ERROR_CLASS_TYPE) {
129 builder.setNode(new IpAddressNoZone(Ipv6Util.noZoneAddressForByteBuf(buffer)));
131 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
132 builder.setFlags(new Flags(flags.get(IN_PLACE), flags.get(NOT_GUILTY)));
133 final short errorCode = buffer.readUnsignedByte();
134 builder.setCode(errorCode);
135 final int errorValue = buffer.readUnsignedShort();
136 builder.setValue(errorValue);
137 return new RsvpCaseBuilder().setRsvpError(builder.build()).build();
140 private static void serializeRsvp(final RsvpError rsvp, final ByteBuf body) {
141 final BitArray flags = new BitArray(FLAGS_SIZE);
142 flags.set(IN_PLACE, rsvp.getFlags().isInPlace());
143 flags.set(NOT_GUILTY, rsvp.getFlags().isNotGuilty());
144 final IpAddressNoZone node = rsvp.getNode();
145 Preconditions.checkArgument(node != null, "Node is mandatory.");
146 final ByteBuf rsvpObjBuf = Unpooled.buffer();
148 if (node.getIpv4AddressNoZone() != null) {
149 type = RSVP_IPV4_ERROR_CLASS_TYPE;
150 writeIpv4Address(node.getIpv4AddressNoZone(), rsvpObjBuf);
152 type = RSVP_IPV6_ERROR_CLASS_TYPE;
153 writeIpv6Address(node.getIpv6AddressNoZone(), rsvpObjBuf);
155 flags.toByteBuf(rsvpObjBuf);
156 Preconditions.checkArgument(rsvp.getCode() != null, "Code is mandatory.");
157 writeUnsignedByte(rsvp.getCode(), rsvpObjBuf);
158 Preconditions.checkArgument(rsvp.getValue() != null, "Value is mandatory.");
159 writeUnsignedShort(rsvp.getValue(), rsvpObjBuf);
160 formatRSVPObject(RSVP_ERROR_CLASS_NUM, type, rsvpObjBuf, body);
163 private static void formatRSVPObject(final int objClass, final int type, final ByteBuf body, final ByteBuf out) {
164 out.writeShort(body.writerIndex() + HEADER_LENGTH);
165 out.writeByte(objClass);
167 out.writeBytes(body);