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 io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotificationBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.Tlvs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.TlvsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
26 import com.google.common.base.Preconditions;
27 import com.google.common.primitives.UnsignedBytes;
30 * Parser for {@link CNotification}
32 public class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
34 public static final int CLASS = 12;
36 public static final int TYPE = 1;
41 private static final int FLAGS_F_LENGTH = 1;
42 private static final int NT_F_LENGTH = 1;
43 private static final int NV_F_LENGTH = 1;
48 private static final int FLAGS_F_OFFSET = 1;
49 private static final int NT_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
50 private static final int NV_F_OFFSET = NT_F_OFFSET + NT_F_LENGTH;
51 private static final int TLVS_OFFSET = NV_F_OFFSET + NV_F_LENGTH;
53 public PCEPNotificationObjectParser(final TlvRegistry tlvReg) {
58 public CNotification parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
59 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
60 final CNotificationBuilder builder = new CNotificationBuilder();
61 builder.setIgnore(header.isIgnore());
62 builder.setProcessingRule(header.isProcessingRule());
63 bytes.readerIndex(bytes.readerIndex() + NT_F_OFFSET);
64 builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
65 builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
66 parseTlvs(builder, bytes.slice());
67 return builder.build();
71 public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
72 if (tlv instanceof OverloadDuration && builder.getType() == 2 && builder.getValue() == 1) {
73 builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
78 public byte[] serializeObject(final Object object) {
79 if (!(object instanceof CNotification)) {
80 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NotificationObject.");
82 final CNotification notObj = (CNotification) object;
84 final byte[] tlvs = serializeTlvs(notObj.getTlvs());
86 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
87 if (tlvs.length != 0) {
88 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
90 retBytes[NT_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getType());
91 retBytes[NV_F_OFFSET] = UnsignedBytes.checkedCast(notObj.getValue());
92 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
95 public byte[] serializeTlvs(final Tlvs tlvs) {
98 } else if (tlvs.getOverloadDuration() != null) {
99 return serializeTlv(tlvs.getOverloadDuration());