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.concepts.IPv4Prefix;
14 import org.opendaylight.protocol.concepts.IPv6Prefix;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.impl.subobject.EROExplicitExclusionRouteSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith128PCEIDSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith32PCEIDSubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
21 import org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject;
22 import org.opendaylight.protocol.pcep.subobject.EROExplicitExclusionRouteSubobject;
23 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
24 import org.opendaylight.protocol.pcep.subobject.EROLabelSubobject;
25 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith128PCEIDSubobject;
26 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith32PCEIDSubobject;
27 import org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject;
28 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Parser for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
36 public class PCEPEROSubobjectParser {
38 private static final Logger logger = LoggerFactory.getLogger(PCEPEROSubobjectParser.class);
41 * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
43 public enum PCEPSubobjectType {
44 IPv4_PREFIX(1), IPv6_PREFIX(2), LABEL(3), UNNUMBERED_INTERFACE_ID(4), AS_NUMBER(32), EXRS(33), PROTECTION(37), PK_32(64), PK_128(65);
46 private final int indicator;
48 PCEPSubobjectType(final int indicator) {
49 this.indicator = indicator;
52 public int getIndicator() {
53 return this.indicator;
56 public static PCEPSubobjectType getFromInt(final int type) throws PCEPDeserializerException {
58 for (final PCEPSubobjectType type_e : PCEPSubobjectType.values()) {
59 if (type_e.getIndicator() == type)
63 throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + "; Known: " + PCEPSubobjectType.values() + ".");
68 * Fields lengths in Bytes
70 public static final int TYPE_FLAG_F_LENGTH = 1;
71 public static final int LENGTH_F_LENGTH = 1;
74 * Fields offsets in Bytes
76 public static final int TYPE_FLAG_F_OFFSET = 0;
77 public static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + TYPE_FLAG_F_LENGTH;
78 public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + LENGTH_F_LENGTH;
80 public static List<ExplicitRouteSubobject> parse(final byte[] bytes) throws PCEPDeserializerException {
82 throw new IllegalArgumentException("Byte array is mandatory.");
84 final List<ExplicitRouteSubobject> subobjsList = new ArrayList<ExplicitRouteSubobject>();
86 PCEPSubobjectType type;
87 byte[] soContentsBytes;
91 while (offset < bytes.length) {
93 loose_flag = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
94 length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, LENGTH_F_LENGTH));
96 type = PCEPSubobjectType.getFromInt((bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7));
98 if (length > bytes.length - offset)
99 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
100 + (bytes.length - offset));
102 soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
103 System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
105 logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
106 final ExplicitRouteSubobject subObj = parseSpecificSubobject(type, soContentsBytes, loose_flag);
107 logger.debug("Subobject was parsed. {}", subObj);
109 subobjsList.add(subObj);
117 public static byte[] put(final List<ExplicitRouteSubobject> objsToSerialize) {
118 final List<byte[]> bytesList = new ArrayList<byte[]>(objsToSerialize.size());
121 for (final ExplicitRouteSubobject obj : objsToSerialize) {
122 final byte[] bytes = put(obj);
123 length += bytes.length;
124 bytesList.add(bytes);
127 final byte[] retBytes = new byte[length];
130 for (final byte[] bytes : bytesList) {
131 System.arraycopy(bytes, 0, retBytes, offset, bytes.length);
132 offset += bytes.length;
138 public static byte[] put(final ExplicitRouteSubobject objToSerialize) {
139 int typeIndicator = 0;
141 byte[] soContentsBytes = null;
143 if (objToSerialize instanceof EROIPPrefixSubobject<?>
144 && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv4Prefix) {
145 typeIndicator = PCEPSubobjectType.IPv4_PREFIX.getIndicator();
146 // soContentsBytes = EROIpPrefixSubobjectParser.put(objToSerialize);
147 } else if (objToSerialize instanceof EROIPPrefixSubobject<?>
148 && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv6Prefix) {
149 typeIndicator = PCEPSubobjectType.IPv6_PREFIX.getIndicator();
150 // soContentsBytes = EROIPv6PrefixSubobjectParser.put(objToSerialize);
151 } else if (objToSerialize instanceof EROAsNumberSubobject) {
152 typeIndicator = PCEPSubobjectType.AS_NUMBER.getIndicator();
153 // soContentsBytes = EROAsNumberSubobjectParser.put(objToSerialize);
154 } else if (objToSerialize instanceof EROUnnumberedInterfaceSubobject) {
155 typeIndicator = PCEPSubobjectType.UNNUMBERED_INTERFACE_ID.getIndicator();
156 soContentsBytes = EROUnnumberedInterfaceSubobjectParser.put(objToSerialize);
157 } else if (objToSerialize instanceof EROLabelSubobject) {
158 typeIndicator = PCEPSubobjectType.LABEL.getIndicator();
159 soContentsBytes = EROLabelSubobjectParser.put((EROLabelSubobject) objToSerialize);
160 } else if (objToSerialize instanceof EROExplicitExclusionRouteSubobject) {
161 typeIndicator = PCEPSubobjectType.EXRS.getIndicator();
162 soContentsBytes = EROExplicitExclusionRouteSubobjectParser.put((EROExplicitExclusionRouteSubobject) objToSerialize);
163 } else if (objToSerialize instanceof EROPathKeyWith32PCEIDSubobject) {
164 typeIndicator = PCEPSubobjectType.PK_32.getIndicator();
165 soContentsBytes = EROPathKeyWith32PCEIDSubobjectParser.put((EROPathKeyWith32PCEIDSubobject) objToSerialize);
166 } else if (objToSerialize instanceof EROPathKeyWith128PCEIDSubobject) {
167 typeIndicator = PCEPSubobjectType.PK_128.getIndicator();
168 soContentsBytes = EROPathKeyWith128PCEIDSubobjectParser.put((EROPathKeyWith128PCEIDSubobject) objToSerialize);
170 throw new IllegalArgumentException("Unknown instance of PCEPSubobject. Passed: " + objToSerialize.getClass() + ".");
172 final byte[] bytes = new byte[SO_CONTENTS_OFFSET + soContentsBytes.length];
174 bytes[TYPE_FLAG_F_OFFSET] = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(typeIndicator), (Integer.SIZE / 8) - TYPE_FLAG_F_LENGTH)[0] | (objToSerialize.isLoose() ? 1 << 7
176 bytes[LENGTH_F_OFFSET] = ByteArray.cutBytes(ByteArray.intToBytes(soContentsBytes.length + SO_CONTENTS_OFFSET), (Integer.SIZE / 8)
177 - LENGTH_F_LENGTH)[0];
179 System.arraycopy(soContentsBytes, 0, bytes, SO_CONTENTS_OFFSET, soContentsBytes.length);
184 private static ExplicitRouteSubobject parseSpecificSubobject(final PCEPSubobjectType type, final byte[] soContentsBytes,
185 final boolean loose_flag) throws PCEPDeserializerException {
189 // return EROIpPrefixSubobjectParser.parse(soContentsBytes, loose_flag);
191 // return EROIPv6PrefixSubobjectParser.parse(soContentsBytes, loose_flag);
192 case UNNUMBERED_INTERFACE_ID:
193 return EROUnnumberedInterfaceSubobjectParser.parse(soContentsBytes, loose_flag);
195 // return EROAsNumberSubobjectParser.parse(soContentsBytes, loose_flag);
197 return EROLabelSubobjectParser.parse(soContentsBytes, loose_flag);
199 return EROExplicitExclusionRouteSubobjectParser.parse(soContentsBytes, loose_flag);
201 return EROPathKeyWith32PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
203 return EROPathKeyWith128PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
205 throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + ".");