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.parser.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
21 import org.opendaylight.protocol.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.CNotification;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.CNotificationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.c.notification.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.c.notification.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.duration.tlv.OverloadDuration;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
33 * Parser for {@link CNotification}.
35 public final class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
37 private static final int CLASS = 12;
38 private static final int TYPE = 1;
43 private static final int NT_F_OFFSET = 2;
45 public PCEPNotificationObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
46 super(tlvReg, viTlvReg, CLASS, TYPE);
50 public CNotification parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51 Preconditions.checkArgument(bytes != null && bytes.isReadable(),
52 "Array of bytes is mandatory. Can't be null or empty.");
53 bytes.skipBytes(NT_F_OFFSET);
54 final CNotificationBuilder builder = new CNotificationBuilder()
55 .setIgnore(header.isIgnore())
56 .setProcessingRule(header.isProcessingRule())
57 .setType(ByteBufUtils.readUint8(bytes))
58 .setValue(ByteBufUtils.readUint8(bytes));
59 parseTlvs(builder, bytes.slice());
60 return builder.build();
64 public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
65 if (tlv instanceof OverloadDuration && builder.getType().toJava() == 2 && builder.getValue().toJava() == 1) {
66 builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
71 public void serializeObject(final Object object, final ByteBuf buffer) {
72 Preconditions.checkArgument(object instanceof CNotification,
73 "Wrong instance of PCEPObject. Passed %s. Needed CNotificationObject.",
75 final CNotification notObj = (CNotification) object;
76 final ByteBuf body = Unpooled.buffer();
77 body.writeZero(NT_F_OFFSET);
78 Preconditions.checkArgument(notObj.getType() != null, "Type is mandatory.");
79 writeUnsignedByte(notObj.getType(), body);
80 Preconditions.checkArgument(notObj.getValue() != null, "Value is mandatory.");
81 writeUnsignedByte(notObj.getValue(), body);
82 serializeTlvs(notObj.getTlvs(), body);
83 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
86 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
90 if (tlvs.getOverloadDuration() != null) {
91 serializeTlv(tlvs.getOverloadDuration(), body);
93 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
97 protected void addVendorInformationTlvs(
98 final CNotificationBuilder builder,
99 final List<VendorInformationTlv> tlvs) {
100 if (!tlvs.isEmpty()) {
101 builder.setTlvs(new TlvsBuilder(builder.getTlvs()).setVendorInformationTlv(tlvs).build());