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.EROExplicitExclusionRouteSubobjectParser;
15 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
16 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith128PCEIDSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKeyWith32PCEIDSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
19 import org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject;
20 import org.opendaylight.protocol.pcep.subobject.EROExplicitExclusionRouteSubobject;
21 import org.opendaylight.protocol.pcep.subobject.EROLabelSubobject;
22 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith128PCEIDSubobject;
23 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith32PCEIDSubobject;
24 import org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject;
25 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Parser for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
33 public class PCEPEROSubobjectParser {
35 private static final Logger logger = LoggerFactory.getLogger(PCEPEROSubobjectParser.class);
38 * Type identifier for {@link org.opendaylight.protocol.pcep.PCEPSubobject PCEPSubobject}
40 public enum PCEPSubobjectType {
41 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);
43 private final int indicator;
45 PCEPSubobjectType(final int indicator) {
46 this.indicator = indicator;
49 public int getIndicator() {
50 return this.indicator;
53 public static PCEPSubobjectType getFromInt(final int type) throws PCEPDeserializerException {
55 for (final PCEPSubobjectType type_e : PCEPSubobjectType.values()) {
56 if (type_e.getIndicator() == type)
60 throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + "; Known: " + PCEPSubobjectType.values() + ".");
65 * Fields lengths in Bytes
67 public static final int TYPE_FLAG_F_LENGTH = 1;
68 public static final int LENGTH_F_LENGTH = 1;
71 * Fields offsets in Bytes
73 public static final int TYPE_FLAG_F_OFFSET = 0;
74 public static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + TYPE_FLAG_F_LENGTH;
75 public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + LENGTH_F_LENGTH;
77 public static List<ExplicitRouteSubobject> parse(final byte[] bytes) throws PCEPDeserializerException {
79 throw new IllegalArgumentException("Byte array is mandatory.");
81 final List<ExplicitRouteSubobject> subobjsList = new ArrayList<ExplicitRouteSubobject>();
83 PCEPSubobjectType type;
84 byte[] soContentsBytes;
88 while (offset < bytes.length) {
90 loose_flag = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
91 length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, LENGTH_F_LENGTH));
93 type = PCEPSubobjectType.getFromInt((bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7));
95 if (length > bytes.length - offset)
96 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
97 + (bytes.length - offset));
99 soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
100 System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
102 logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
103 final ExplicitRouteSubobject subObj = parseSpecificSubobject(type, soContentsBytes, loose_flag);
104 logger.debug("Subobject was parsed. {}", subObj);
106 subobjsList.add(subObj);
114 public static byte[] put(final List<ExplicitRouteSubobject> objsToSerialize) {
115 final List<byte[]> bytesList = new ArrayList<byte[]>(objsToSerialize.size());
118 for (final ExplicitRouteSubobject obj : objsToSerialize) {
119 final byte[] bytes = put(obj);
120 length += bytes.length;
121 bytesList.add(bytes);
124 final byte[] retBytes = new byte[length];
127 for (final byte[] bytes : bytesList) {
128 System.arraycopy(bytes, 0, retBytes, offset, bytes.length);
129 offset += bytes.length;
135 public static byte[] put(final ExplicitRouteSubobject objToSerialize) {
136 int typeIndicator = 0;
138 byte[] soContentsBytes = null;
140 // if (objToSerialize instanceof EROIPPrefixSubobject<?>
141 // && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv4Prefix) {
142 // typeIndicator = PCEPSubobjectType.IPv4_PREFIX.getIndicator();
143 // // soContentsBytes = EROIpPrefixSubobjectParser.put(objToSerialize);
144 // } else if (objToSerialize instanceof EROIPPrefixSubobject<?>
145 // && ((EROIPPrefixSubobject<?>) objToSerialize).getPrefix() instanceof IPv6Prefix) {
146 // typeIndicator = PCEPSubobjectType.IPv6_PREFIX.getIndicator();
147 // // soContentsBytes = EROIPv6PrefixSubobjectParser.put(objToSerialize);
149 if (objToSerialize instanceof EROAsNumberSubobject) {
150 typeIndicator = PCEPSubobjectType.AS_NUMBER.getIndicator();
151 // soContentsBytes = EROAsNumberSubobjectParser.put(objToSerialize);
152 } else if (objToSerialize instanceof EROUnnumberedInterfaceSubobject) {
153 typeIndicator = PCEPSubobjectType.UNNUMBERED_INTERFACE_ID.getIndicator();
154 soContentsBytes = EROUnnumberedInterfaceSubobjectParser.put(objToSerialize);
155 } else if (objToSerialize instanceof EROLabelSubobject) {
156 typeIndicator = PCEPSubobjectType.LABEL.getIndicator();
157 soContentsBytes = EROLabelSubobjectParser.put((EROLabelSubobject) objToSerialize);
158 } else if (objToSerialize instanceof EROExplicitExclusionRouteSubobject) {
159 typeIndicator = PCEPSubobjectType.EXRS.getIndicator();
160 soContentsBytes = EROExplicitExclusionRouteSubobjectParser.put((EROExplicitExclusionRouteSubobject) objToSerialize);
161 } else if (objToSerialize instanceof EROPathKeyWith32PCEIDSubobject) {
162 typeIndicator = PCEPSubobjectType.PK_32.getIndicator();
163 soContentsBytes = EROPathKeyWith32PCEIDSubobjectParser.put((EROPathKeyWith32PCEIDSubobject) objToSerialize);
164 } else if (objToSerialize instanceof EROPathKeyWith128PCEIDSubobject) {
165 typeIndicator = PCEPSubobjectType.PK_128.getIndicator();
166 soContentsBytes = EROPathKeyWith128PCEIDSubobjectParser.put((EROPathKeyWith128PCEIDSubobject) objToSerialize);
168 throw new IllegalArgumentException("Unknown instance of PCEPSubobject. Passed: " + objToSerialize.getClass() + ".");
170 final byte[] bytes = new byte[SO_CONTENTS_OFFSET + soContentsBytes.length];
172 bytes[TYPE_FLAG_F_OFFSET] = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(typeIndicator), (Integer.SIZE / 8) - TYPE_FLAG_F_LENGTH)[0] | (objToSerialize.isLoose() ? 1 << 7
174 bytes[LENGTH_F_OFFSET] = ByteArray.cutBytes(ByteArray.intToBytes(soContentsBytes.length + SO_CONTENTS_OFFSET), (Integer.SIZE / 8)
175 - LENGTH_F_LENGTH)[0];
177 System.arraycopy(soContentsBytes, 0, bytes, SO_CONTENTS_OFFSET, soContentsBytes.length);
182 private static ExplicitRouteSubobject parseSpecificSubobject(final PCEPSubobjectType type, final byte[] soContentsBytes,
183 final boolean loose_flag) throws PCEPDeserializerException {
187 // return EROIpPrefixSubobjectParser.parse(soContentsBytes, loose_flag);
189 // return EROIPv6PrefixSubobjectParser.parse(soContentsBytes, loose_flag);
190 case UNNUMBERED_INTERFACE_ID:
191 return EROUnnumberedInterfaceSubobjectParser.parse(soContentsBytes, loose_flag);
193 // return EROAsNumberSubobjectParser.parse(soContentsBytes, loose_flag);
195 return EROLabelSubobjectParser.parse(soContentsBytes, loose_flag);
197 return EROExplicitExclusionRouteSubobjectParser.parse(soContentsBytes, loose_flag);
199 return EROPathKeyWith32PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
201 return EROPathKeyWith128PCEIDSubobjectParser.parse(soContentsBytes, loose_flag);
203 throw new PCEPDeserializerException("Unknown Subobject type. Passed: " + type + ".");