Merge "Initial support for RFC2385"
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02RSVPErrorSpecTlvParser.java
1 /*
2  * Copyright (c) 2014 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.stateful02;
9
10 import java.util.BitSet;
11
12 import org.opendaylight.protocol.concepts.Ipv4Util;
13 import org.opendaylight.protocol.concepts.Ipv6Util;
14 import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
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.util.ByteArray;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.ErrorType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpError;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpErrorBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserError;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserErrorBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags;
35
36 import com.google.common.primitives.UnsignedBytes;
37
38 /**
39  * Parser for {@link RsvpErrorSpec}
40  */
41 public final class Stateful02RSVPErrorSpecTlvParser implements TlvParser, TlvSerializer {
42
43         public static final int TYPE = 21;
44
45         private static final int IP4_F_LENGTH = 4;
46         private static final int IP6_F_LENGTH = 16;
47         private static final int FLAGS_F_LENGTH = 1;
48         private static final int ERROR_CODE_F_LENGTH = 1;
49         private static final int ERROR_VALUE_F_LENGTH = 2;
50
51         private static final int ENTERPRISE_F_LENGTH = 4;
52         private static final int SUB_ORG_F_LENGTH = 1;
53         private static final int ERR_DESCR_LENGTH_F_LENGTH = 1;
54         private static final int USER_VALUE_F_LENGTH = 2;
55
56         private static final int RSVP_ERROR_CLASS_NUM = 6;
57         private static final int RSVP_IPV4_ERROR_CLASS_TYPE = 1;
58         private static final int RSVP_IPV6_ERROR_CLASS_TYPE = 2;
59
60         private static final int USER_ERROR_CLASS_NUM = 194;
61         private static final int USER_ERROR_CLASS_TYPE = 1;
62
63         private static final int IN_PLACE_FLAG_OFFSET = 7;
64         private static final int NOT_GUILTY_FLAGS_OFFSET = 6;
65
66         private static final int V4_RSVP_LENGTH = 10;
67         private static final int V6_RSVP_LENGTH = 22;
68
69         @Override
70         public RsvpErrorSpec parseTlv(final byte[] valueBytes) throws PCEPDeserializerException {
71                 if (valueBytes == null || valueBytes.length == 0) {
72                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
73                 }
74
75                 final int classNum = ByteArray.bytesToInt(ByteArray.subByte(valueBytes, 0, 1));
76                 final int classType = ByteArray.bytesToInt(ByteArray.subByte(valueBytes, 1, 1));
77
78                 ErrorType errorType = null;
79                 final int byteOffset = 2;
80
81                 if (classNum == RSVP_ERROR_CLASS_NUM) {
82                         errorType = parseRsvp(classType, ByteArray.cutBytes(valueBytes, byteOffset));
83                 } else if (classNum == USER_ERROR_CLASS_NUM && classType == USER_ERROR_CLASS_TYPE) {
84                         errorType = parseUserError(ByteArray.cutBytes(valueBytes, byteOffset));
85                 }
86                 return new RsvpErrorSpecBuilder().setErrorType(errorType).build();
87         }
88
89         @Override
90         public byte[] serializeTlv(final Tlv tlv) {
91                 if (tlv == null) {
92                         throw new IllegalArgumentException("RSVPErrorSpecTlv is mandatory.");
93                 }
94                 final RsvpErrorSpec rsvp = (RsvpErrorSpec) tlv;
95
96                 if (rsvp.getErrorType().getImplementedInterface().equals(RsvpCase.class)) {
97                         final RsvpCase r = (RsvpCase) rsvp.getErrorType();
98                         return TlvUtil.formatTlv(TYPE, serializeRsvp(r.getRsvpError()));
99                 } else {
100                         final UserCase u = (UserCase) rsvp.getErrorType();
101                         return TlvUtil.formatTlv(TYPE, serializerUserError(u.getUserError()));
102                 }
103         }
104
105         private UserCase parseUserError(final byte[] valueBytes) {
106                 final UserErrorBuilder error = new UserErrorBuilder();
107                 int byteOffset = 0;
108                 error.setEnterprise(new EnterpriseNumber(ByteArray.bytesToLong(ByteArray.subByte(valueBytes, byteOffset, ENTERPRISE_F_LENGTH))));
109                 byteOffset += ENTERPRISE_F_LENGTH;
110                 error.setSubOrg((short) UnsignedBytes.toInt(valueBytes[byteOffset]));
111                 byteOffset += SUB_ORG_F_LENGTH;
112                 final int errDescrLength = UnsignedBytes.toInt(valueBytes[byteOffset]);
113                 byteOffset += ERR_DESCR_LENGTH_F_LENGTH;
114                 error.setValue(ByteArray.bytesToInt(ByteArray.subByte(valueBytes, byteOffset, USER_VALUE_F_LENGTH)));
115                 byteOffset += USER_VALUE_F_LENGTH;
116                 error.setDescription(ByteArray.bytesToHRString(ByteArray.subByte(valueBytes, byteOffset, errDescrLength)));
117                 byteOffset += errDescrLength;
118                 // if we have any subobjects, place the implementation here
119                 return new UserCaseBuilder().setUserError(error.build()).build();
120         }
121
122         private byte[] serializerUserError(final UserError ue) {
123                 final byte[] enterprise = ByteArray.longToBytes(ue.getEnterprise().getValue(), ENTERPRISE_F_LENGTH);
124                 final byte suborg = UnsignedBytes.checkedCast(ue.getSubOrg());
125                 final byte[] value = ByteArray.intToBytes(ue.getValue(), USER_VALUE_F_LENGTH);
126                 final byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
127                 final byte descLen = UnsignedBytes.checkedCast(desc.length);
128                 // if we have any subobjects, place the implementation here
129                 final byte[] bytes = new byte[2 + ENTERPRISE_F_LENGTH + SUB_ORG_F_LENGTH + USER_VALUE_F_LENGTH + ERR_DESCR_LENGTH_F_LENGTH
130                                               + desc.length];
131                 bytes[0] = UnsignedBytes.checkedCast(USER_ERROR_CLASS_NUM);
132                 bytes[1] = UnsignedBytes.checkedCast(USER_ERROR_CLASS_TYPE);
133                 int offset = 2;
134                 ByteArray.copyWhole(enterprise, bytes, offset);
135                 offset += ENTERPRISE_F_LENGTH;
136                 bytes[offset] = suborg;
137                 offset += SUB_ORG_F_LENGTH;
138                 bytes[offset] = descLen;
139                 offset += ERR_DESCR_LENGTH_F_LENGTH;
140                 ByteArray.copyWhole(value, bytes, offset);
141                 offset += USER_VALUE_F_LENGTH;
142                 ByteArray.copyWhole(desc, bytes, offset);
143                 return bytes;
144         }
145
146         private RsvpCase parseRsvp(final int classType, final byte[] valueBytes) {
147                 int byteOffset = 0;
148                 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
149                 if (classType == RSVP_IPV4_ERROR_CLASS_TYPE) {
150                         builder.setNode(new IpAddress(Ipv4Util.addressForBytes(ByteArray.subByte(valueBytes, byteOffset, IP4_F_LENGTH))));
151                         byteOffset += IP4_F_LENGTH;
152                 } else if (classType == RSVP_IPV6_ERROR_CLASS_TYPE) {
153                         builder.setNode(new IpAddress(Ipv6Util.addressForBytes(ByteArray.subByte(valueBytes, byteOffset, IP6_F_LENGTH))));
154                         byteOffset += IP6_F_LENGTH;
155                 }
156                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(valueBytes, byteOffset, FLAGS_F_LENGTH));
157                 byteOffset += FLAGS_F_LENGTH;
158                 builder.setFlags(new Flags(flags.get(IN_PLACE_FLAG_OFFSET), flags.get(NOT_GUILTY_FLAGS_OFFSET)));
159                 final short errorCode = (short) (valueBytes[byteOffset] & Values.BYTE_MAX_VALUE_BYTES);
160                 byteOffset += ERROR_CODE_F_LENGTH;
161                 builder.setCode(errorCode);
162                 final int errorValue = (ByteArray.bytesToShort(ByteArray.subByte(valueBytes, byteOffset, ERROR_VALUE_F_LENGTH)) & 0xFFFF);
163                 builder.setValue(errorValue);
164                 return new RsvpCaseBuilder().setRsvpError(builder.build()).build();
165         }
166
167         private byte[] serializeRsvp(final RsvpError rsvp) {
168                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
169                 flags.set(IN_PLACE_FLAG_OFFSET, rsvp.getFlags().isInPlace());
170                 flags.set(NOT_GUILTY_FLAGS_OFFSET, rsvp.getFlags().isNotGuilty());
171                 int offset = 0;
172                 final IpAddress node = rsvp.getNode();
173                 byte[] bytes;
174                 if (node.getIpv4Address() != null) {
175                         bytes = new byte[V4_RSVP_LENGTH];
176                         bytes[0] = RSVP_ERROR_CLASS_NUM;
177                         bytes[1] = RSVP_IPV4_ERROR_CLASS_TYPE;
178                         offset += 2;
179                         ByteArray.copyWhole(Ipv4Util.bytesForAddress(node.getIpv4Address()), bytes, offset);
180                         offset += IP4_F_LENGTH;
181                 } else {
182                         bytes = new byte[V6_RSVP_LENGTH];
183                         bytes[0] = RSVP_ERROR_CLASS_NUM;
184                         bytes[1] = RSVP_IPV6_ERROR_CLASS_TYPE;
185                         offset += 2;
186                         ByteArray.copyWhole(Ipv6Util.bytesForAddress(node.getIpv6Address()), bytes, offset);
187                         offset += IP6_F_LENGTH;
188                 }
189                 bytes[offset] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
190                 offset += FLAGS_F_LENGTH;
191                 bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
192                 offset += ERROR_CODE_F_LENGTH;
193                 final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
194                 ByteArray.copyWhole(value, bytes, offset);
195                 return bytes;
196         }
197
198         @Override
199         public int getType() {
200                 return TYPE;
201         }
202 }