Merge "BUG-1287: migrate tests to new APIs"
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07RSVPErrorSpecTlvParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.ietf.stateful07;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
13 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
14 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
15 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
16
17 import com.google.common.base.Preconditions;
18 import com.google.common.primitives.UnsignedBytes;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.util.BitSet;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.spi.TlvParser;
24 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
25 import org.opendaylight.protocol.pcep.spi.TlvUtil;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.protocol.util.Ipv4Util;
28 import org.opendaylight.protocol.util.Ipv6Util;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.ErrorType;
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.RsvpCase;
35 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;
36 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;
37 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;
38 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;
39 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;
40 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;
41 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;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags;
44
45 /**
46  * Parser for {@link RsvpErrorSpec}
47  */
48 public final class Stateful07RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
49
50     public static final int TYPE = 21;
51
52     private static final int FLAGS_F_LENGTH = 1;
53
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;
57
58     private static final int USER_ERROR_CLASS_NUM = 194;
59     private static final int USER_ERROR_CLASS_TYPE = 1;
60
61     private static final int IN_PLACE_FLAG_OFFSET = 7;
62     private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
63
64     @Override
65     public RsvpErrorSpec parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
66         if (buffer == null) {
67             return null;
68         }
69         final int classNum = UnsignedBytes.toInt(buffer.readByte());
70         final int classType = UnsignedBytes.toInt(buffer.readByte());
71         ErrorType errorType = null;
72         if (classNum == RSVP_ERROR_CLASS_NUM) {
73             errorType = parseRsvp(classType, buffer.slice());
74         } else if (classNum == USER_ERROR_CLASS_NUM && classType == USER_ERROR_CLASS_TYPE) {
75             errorType = parseUserError(buffer.slice());
76         }
77         return new RsvpErrorSpecBuilder().setErrorType(errorType).build();
78     }
79
80     @Override
81     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
82         Preconditions.checkArgument(tlv instanceof RsvpErrorSpec, "RSVPErrorSpecTlv is mandatory.");
83         final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv;
84         final ByteBuf body = Unpooled.buffer();
85         if (rsvp.getErrorType().getImplementedInterface().equals(RsvpCase.class)) {
86             final RsvpCase r = (RsvpCase) rsvp.getErrorType();
87             serializeRsvp(r.getRsvpError(), body);
88             TlvUtil.formatTlv(TYPE, body, buffer);
89         } else {
90             final UserCase u = (UserCase) rsvp.getErrorType();
91             serializerUserError(u.getUserError(), body);
92             TlvUtil.formatTlv(TYPE, body, buffer);
93         }
94     }
95
96     private UserCase parseUserError(final ByteBuf buffer) {
97         final UserErrorBuilder error = new UserErrorBuilder();
98         error.setEnterprise(new EnterpriseNumber(buffer.readUnsignedInt()));
99         error.setSubOrg((short) UnsignedBytes.toInt(buffer.readByte()));
100         final int errDescrLength = UnsignedBytes.toInt(buffer.readByte());
101         error.setValue(buffer.readUnsignedShort());
102         error.setDescription(ByteArray.bytesToHRString(ByteArray.readBytes(buffer, errDescrLength)));
103         // if we have any subobjects, place the implementation here
104         return new UserCaseBuilder().setUserError(error.build()).build();
105     }
106
107     private void serializerUserError(final UserError ue, final ByteBuf body) {
108         byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
109         body.writeByte(USER_ERROR_CLASS_NUM);
110         body.writeByte(USER_ERROR_CLASS_TYPE);
111         Preconditions.checkArgument(ue.getEnterprise() != null, "EnterpriseNumber is mandatory");
112         writeUnsignedInt(ue.getEnterprise().getValue(), body);
113         writeUnsignedByte(ue.getSubOrg(), body);
114         body.writeByte(desc.length);
115         Preconditions.checkArgument(ue.getValue() != null, "Value is mandatory.");
116         writeUnsignedShort(ue.getValue(), body);
117         body.writeBytes(desc);
118     }
119
120     private RsvpCase parseRsvp(final int classType, final ByteBuf buffer) {
121         final RsvpErrorBuilder builder = new RsvpErrorBuilder();
122         if (classType == RSVP_IPV4_ERROR_CLASS_TYPE) {
123             builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))));
124         } else if (classType == RSVP_IPV6_ERROR_CLASS_TYPE) {
125             builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH))));
126         }
127         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
128         builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
129         final short errorCode = buffer.readUnsignedByte();
130         builder.setCode(errorCode);
131         final int errorValue = buffer.readUnsignedShort();
132         builder.setValue(errorValue);
133         return new RsvpCaseBuilder().setRsvpError(builder.build()).build();
134     }
135
136     private void serializeRsvp(final RsvpError rsvp, final ByteBuf body) {
137         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
138         flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
139         flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
140         final IpAddress node = rsvp.getNode();
141         Preconditions.checkArgument(node != null, "Node is mandatory.");
142         if (node.getIpv4Address() != null) {
143             body.writeByte(RSVP_ERROR_CLASS_NUM);
144             body.writeByte(RSVP_IPV4_ERROR_CLASS_TYPE);
145             writeIpv4Address(node.getIpv4Address(), body);
146         } else {
147             body.writeByte(RSVP_ERROR_CLASS_NUM);
148             body.writeByte(RSVP_IPV6_ERROR_CLASS_TYPE);
149             writeIpv6Address(node.getIpv6Address(), body);
150         }
151         writeBitSet(flags, FLAGS_F_LENGTH, body);
152         Preconditions.checkArgument(rsvp.getCode() != null, "Code is mandatory.");
153         writeUnsignedByte(rsvp.getCode(), body);
154         Preconditions.checkArgument(rsvp.getValue() != null, "Value is mandatory.");
155         writeUnsignedShort(rsvp.getValue(), body);
156     }
157 }