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.PCEPObject;
12 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
13 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
14 import org.opendaylight.protocol.pcep.object.PCEPNotificationObject;
15 import org.opendaylight.protocol.util.ByteArray;
18 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPNotificationObject
19 * PCEPNotificationObject}
21 public class PCEPNotificationObjectParser implements PCEPObjectParser {
26 public static final int FLAGS_F_LENGTH = 1;
27 public static final int NT_F_LENGTH = 1;
28 public static final int NV_F_LENGTH = 1;
33 public static final int FLAGS_F_OFFSET = 1; //added reserved filed of size 1
34 public static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
35 public static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
36 public static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
39 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
40 if (bytes == null || bytes.length == 0)
41 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
43 if (bytes.length < TLVS_OFFSET)
44 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
46 return new PCEPNotificationObject((short) (bytes[NT_F_OFFSET] & 0xFF), (short) (bytes[NV_F_OFFSET] & 0xFF), PCEPTlvParser.parse(ByteArray.cutBytes(
47 bytes, TLVS_OFFSET)));
51 public byte[] put(PCEPObject obj) {
52 if (!(obj instanceof PCEPNotificationObject))
53 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPNotificationObject.");
55 final PCEPNotificationObject notObj = (PCEPNotificationObject) obj;
57 final byte[] tlvs = PCEPTlvParser.put(notObj.getTlvs());
58 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
60 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
62 retBytes[NT_F_OFFSET] = ByteArray.shortToBytes(notObj.getType())[1];
63 retBytes[NV_F_OFFSET] = ByteArray.shortToBytes(notObj.getValue())[1];