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.spi.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.notification.object.CNotification;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotificationBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.Tlvs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.TlvsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
22 import com.google.common.primitives.UnsignedBytes;
25 * Parser for {@link CNotification}
27 public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
29 public static final int CLASS = 12;
31 public static final int TYPE = 1;
36 private static final int FLAGS_F_LENGTH = 1;
37 private static final int NT_F_LENGTH = 1;
38 private static final int NV_F_LENGTH = 1;
43 private static final int FLAGS_F_OFFSET = 1;
44 private static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
45 private static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
46 private static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
48 public PCEPNotificationObjectParser(final TlvHandlerRegistry tlvReg) {
53 public CNotification parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
54 if (bytes == null || bytes.length == 0) {
55 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
57 final CNotificationBuilder builder = new CNotificationBuilder();
58 builder.setIgnore(header.isIgnore());
59 builder.setProcessingRule(header.isProcessingRule());
60 builder.setType((short) UnsignedBytes.toInt(bytes[NT_F_OFFSET]));
61 builder.setValue((short) UnsignedBytes.toInt(bytes[NV_F_OFFSET]));
62 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
63 return builder.build();
67 public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
68 if (tlv instanceof OverloadDuration && builder.getType() == 2 && builder.getValue() == 1) {
69 builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
74 public byte[] serializeObject(final Object object) {
75 if (!(object instanceof CNotification)) {
76 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
78 final CNotification notObj = (CNotification) object;
80 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
82 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
83 if (tlvs.length != 0) {
84 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
86 retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
87 retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
91 public byte[] serializeTlvs(final Tlvs tlvs) {
94 } else if (tlvs.getOverloadDuration() != null) {
95 return serializeTlv(tlvs.getOverloadDuration());
101 public int getObjectType() {
106 public int getObjectClass() {