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.impl;
10 import java.util.ArrayList;
11 import java.util.List;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.impl.subobject.RROAttributesSubobjectParser;
15 import org.opendaylight.protocol.pcep.impl.subobject.RROIPv4AddressSubobjectParser;
16 import org.opendaylight.protocol.pcep.impl.subobject.RROIPv6AddressSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.RROLabelSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.RROPathKeyWith128PCEIDSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.RROPathKeyWith32PCEIDSubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.RROUnnumberedInterfaceSubobjectParser;
21 import org.opendaylight.protocol.pcep.subobject.RROAttributesSubobject;
22 import org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject;
23 import org.opendaylight.protocol.pcep.subobject.RROLabelSubobject;
24 import org.opendaylight.protocol.pcep.subobject.RROPathKeyWith128PCEIDSubobject;
25 import org.opendaylight.protocol.pcep.subobject.RROPathKeyWith32PCEIDSubobject;
26 import org.opendaylight.protocol.pcep.subobject.RROUnnumberedInterfaceSubobject;
27 import org.opendaylight.protocol.pcep.subobject.ReportedRouteSubobject;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Parser for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
35 public class PCEPRROSubobjectParser {
37 private static final Logger logger = LoggerFactory.getLogger(PCEPRROSubobjectParser.class);
40 * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
42 public enum PCEPSubobjectType {
43 IPv4_PREFIX(1), IPv6_PREFIX(2), LABEL(3), UNNUMBERED_INTERFACE_ID(4), ATTRIBUTES(5), PROTECTION(37), PK_32(64), PK_128(65);
45 private final int indicator;
47 PCEPSubobjectType(final int indicator) {
48 this.indicator = indicator;
51 public int getIndicator() {
52 return this.indicator;
55 public static PCEPSubobjectType getFromInt(final int type) throws PCEPDeserializerException {
57 for (final PCEPSubobjectType type_e : PCEPSubobjectType.values()) {
58 if (type_e.getIndicator() == type)
62 throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + "; Known: " + PCEPSubobjectType.values() + ".");
67 * Fields lengths in Bytes
69 public static final int TYPE_F_LENGTH = 1;
70 public static final int LENGTH_F_LENGTH = 1;
73 * Fields offsets in Bytes
75 public static final int TYPE_F_OFFSET = 0;
76 public static final int LENGTH_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
77 public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + LENGTH_F_LENGTH;
79 public static List<ReportedRouteSubobject> parse(final byte[] bytes) throws PCEPDeserializerException {
81 throw new IllegalArgumentException("Byte array is mandatory.");
83 final List<ReportedRouteSubobject> subobjsList = new ArrayList<ReportedRouteSubobject>();
84 PCEPSubobjectType type;
85 byte[] soContentsBytes;
89 while (offset < bytes.length) {
90 length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, LENGTH_F_LENGTH));
92 type = PCEPSubobjectType.getFromInt(bytes[offset + TYPE_F_OFFSET] & 0xff);
94 if (length > bytes.length - offset)
95 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
96 + (bytes.length - offset));
98 soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
99 System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
101 logger.trace("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
102 final ReportedRouteSubobject subObj = parseSpecificSubobject(type, soContentsBytes);
103 logger.trace("Subobject was parsed. {}", subObj);
105 subobjsList.add(subObj);
113 public static byte[] put(final List<ReportedRouteSubobject> objsToSerialize) {
114 final List<byte[]> bytesList = new ArrayList<byte[]>(objsToSerialize.size());
117 for (final ReportedRouteSubobject obj : objsToSerialize) {
118 final byte[] bytes = put(obj);
119 length += bytes.length;
120 bytesList.add(bytes);
123 final byte[] retBytes = new byte[length];
126 for (final byte[] bytes : bytesList) {
127 System.arraycopy(bytes, 0, retBytes, offset, bytes.length);
128 offset += bytes.length;
134 public static byte[] put(final ReportedRouteSubobject objToSerialize) {
135 int typeIndicator = 0;
137 final byte[] soContentsBytes;
139 if (objToSerialize instanceof RROIPAddressSubobject && ((RROIPAddressSubobject) objToSerialize).getPrefix().getIpv4Prefix() != null) {
140 typeIndicator = PCEPSubobjectType.IPv4_PREFIX.getIndicator();
141 soContentsBytes = RROIPv4AddressSubobjectParser.put(objToSerialize);
142 } else if (objToSerialize instanceof RROIPAddressSubobject
143 && ((RROIPAddressSubobject) objToSerialize).getPrefix().getIpv6Prefix() != null) {
144 typeIndicator = PCEPSubobjectType.IPv6_PREFIX.getIndicator();
145 soContentsBytes = RROIPv6AddressSubobjectParser.put(objToSerialize);
146 } else if (objToSerialize instanceof RROUnnumberedInterfaceSubobject) {
147 typeIndicator = PCEPSubobjectType.UNNUMBERED_INTERFACE_ID.getIndicator();
148 soContentsBytes = RROUnnumberedInterfaceSubobjectParser.put(objToSerialize);
149 } else if (objToSerialize instanceof RROLabelSubobject) {
150 typeIndicator = PCEPSubobjectType.LABEL.getIndicator();
151 soContentsBytes = RROLabelSubobjectParser.put((RROLabelSubobject) objToSerialize);
152 } else if (objToSerialize instanceof RROPathKeyWith32PCEIDSubobject) {
153 typeIndicator = PCEPSubobjectType.PK_32.getIndicator();
154 soContentsBytes = RROPathKeyWith32PCEIDSubobjectParser.put((RROPathKeyWith32PCEIDSubobject) objToSerialize);
155 } else if (objToSerialize instanceof RROPathKeyWith128PCEIDSubobject) {
156 typeIndicator = PCEPSubobjectType.PK_128.getIndicator();
157 soContentsBytes = RROPathKeyWith128PCEIDSubobjectParser.put((RROPathKeyWith128PCEIDSubobject) objToSerialize);
158 } else if (objToSerialize instanceof RROAttributesSubobject) {
159 typeIndicator = PCEPSubobjectType.ATTRIBUTES.getIndicator();
160 soContentsBytes = RROAttributesSubobjectParser.put((RROAttributesSubobject) objToSerialize);
162 throw new IllegalArgumentException("Unknown instance of PCEPSubobject. Passed: " + objToSerialize.getClass() + ".");
164 final byte[] bytes = new byte[SO_CONTENTS_OFFSET + soContentsBytes.length];
166 bytes[TYPE_F_OFFSET] = ByteArray.cutBytes(ByteArray.intToBytes(typeIndicator), (Integer.SIZE / 8) - TYPE_F_LENGTH)[0];
167 bytes[LENGTH_F_OFFSET] = ByteArray.cutBytes(ByteArray.intToBytes(soContentsBytes.length + SO_CONTENTS_OFFSET), (Integer.SIZE / 8)
168 - LENGTH_F_LENGTH)[0];
170 System.arraycopy(soContentsBytes, 0, bytes, SO_CONTENTS_OFFSET, soContentsBytes.length);
175 private static ReportedRouteSubobject parseSpecificSubobject(final PCEPSubobjectType type, final byte[] soContentsBytes)
176 throws PCEPDeserializerException {
180 return RROIPv4AddressSubobjectParser.parse(soContentsBytes);
182 return RROIPv6AddressSubobjectParser.parse(soContentsBytes);
183 case UNNUMBERED_INTERFACE_ID:
184 return RROUnnumberedInterfaceSubobjectParser.parse(soContentsBytes);
186 return RROLabelSubobjectParser.parse(soContentsBytes);
188 return RROPathKeyWith32PCEIDSubobjectParser.parse(soContentsBytes);
190 return RROPathKeyWith128PCEIDSubobjectParser.parse(soContentsBytes);
192 return RROAttributesSubobjectParser.parse(soContentsBytes);
194 throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + ".");