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 org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CClose;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CCloseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.c.close.Tlvs;
20 import com.google.common.primitives.UnsignedBytes;
23 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPCloseObject PCEPCloseObject}
25 public class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<CCloseBuilder> {
27 public static final int CLASS = 15;
29 public static final int TYPE = 1;
32 * lengths of fields in bytes
34 private static final int FLAGS_F_LENGTH = 1;
35 private static final int REASON_F_LENGTH = 1;
38 * offsets of fields in bytes
40 private static final int FLAGS_F_OFFSET = 2;
41 private static final int REASON_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
44 * total size of object in bytes
46 private static final int TLVS_OFFSET = REASON_F_OFFSET + REASON_F_LENGTH;
48 public PCEPCloseObjectParser(final TlvHandlerRegistry tlvReg) {
53 public CClose parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
55 throw new IllegalArgumentException("Byte array is mandatory.");
57 final CCloseBuilder builder = new CCloseBuilder();
58 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
59 builder.setIgnore(header.isIgnore());
60 builder.setProcessingRule(header.isProcessingRule());
61 builder.setReason((short) UnsignedBytes.toInt(bytes[REASON_F_OFFSET]));
62 return builder.build();
66 public void addTlv(final CCloseBuilder builder, final Tlv tlv) {
71 public byte[] serializeObject(final Object object) {
72 if (!(object instanceof CClose)) {
73 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed CloseObject.");
75 final CClose obj = (CClose) object;
77 final byte[] tlvs = serializeTlvs(obj.getTlvs());
80 tlvsLength = tlvs.length;
82 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
85 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
87 retBytes[REASON_F_OFFSET] = UnsignedBytes.checkedCast(obj.getReason());
91 public byte[] serializeTlvs(final Tlvs tlvs) {
97 public int getObjectType() {
102 public int getObjectClass() {