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.object;
10 import java.util.Arrays;
11 import java.util.BitSet;
12 import java.util.List;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
17 import org.opendaylight.protocol.pcep.PCEPObject;
18 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
19 import org.opendaylight.protocol.pcep.impl.PCEPXROSubobjectParser;
20 import org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject;
21 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
24 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject
25 * PCEPExcludeRouteObject}
27 public class PCEPExcludeRouteObjectParser implements PCEPObjectParser {
30 * lengths of fields in bytes
32 public final int FLAGS_F_LENGTH = 2;
35 * offsets of fields in bytes
37 public final int FLAGS_F_OFFSET = 2; // added reserved 2 bytes
38 public final int SO_F_OFFSET = this.FLAGS_F_OFFSET + this.FLAGS_F_LENGTH;
41 * Flag offsets inside flags field in bits
43 public final int F_FLAG_OFFSET = 15;
46 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
47 if (bytes == null || bytes.length == 0)
48 throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
50 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(bytes, this.FLAGS_F_OFFSET, this.FLAGS_F_LENGTH));
52 final List<ExcludeRouteSubobject> subobjects = PCEPXROSubobjectParser.parse(ByteArray.cutBytes(bytes, this.SO_F_OFFSET));
53 if (subobjects.isEmpty())
54 throw new PCEPDeserializerException("Empty Exclude Route Object.");
56 return new PCEPExcludeRouteObject(subobjects, flags.get(this.F_FLAG_OFFSET), processed, ignored);
60 public byte[] put(PCEPObject obj) {
61 if (!(obj instanceof PCEPExcludeRouteObject))
62 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPExcludeRouteObject.");
64 assert !(((PCEPExcludeRouteObject) obj).getSubobjects().isEmpty()) : "Empty Exclude Route Object.";
66 final byte[] subObjsBytes = PCEPXROSubobjectParser.put(((PCEPExcludeRouteObject) obj).getSubobjects());
67 final byte[] retBytes = new byte[this.SO_F_OFFSET + subObjsBytes.length];
68 final BitSet flags = new BitSet(this.FLAGS_F_LENGTH * Byte.SIZE);
69 flags.set(this.F_FLAG_OFFSET, ((PCEPExcludeRouteObject) obj).isFail());
70 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, this.FLAGS_F_LENGTH), retBytes, this.FLAGS_F_OFFSET);
71 ByteArray.copyWhole(subObjsBytes, retBytes, this.SO_F_OFFSET);