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 com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
19 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.CNotification;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.CNotificationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.c.notification.Tlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.c.notification.TlvsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.duration.tlv.OverloadDuration;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
29 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
32 * Parser for {@link CNotification}.
34 public final class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
35 private static final int CLASS = 12;
36 private static final int TYPE = 1;
41 private static final int NT_F_OFFSET = 2;
43 public PCEPNotificationObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
44 super(tlvReg, viTlvReg, CLASS, TYPE);
48 public CNotification parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
49 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
50 bytes.skipBytes(NT_F_OFFSET);
51 final CNotificationBuilder builder = new CNotificationBuilder()
52 .setIgnore(header.getIgnore())
53 .setProcessingRule(header.getProcessingRule())
54 .setType(ByteBufUtils.readUint8(bytes))
55 .setValue(ByteBufUtils.readUint8(bytes));
56 parseTlvs(builder, bytes.slice());
57 return builder.build();
61 public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
62 if (tlv instanceof OverloadDuration && builder.getType().toJava() == 2 && builder.getValue().toJava() == 1) {
63 builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
68 public void serializeObject(final Object object, final ByteBuf buffer) {
69 checkArgument(object instanceof CNotification,
70 "Wrong instance of PCEPObject. Passed %s. Needed CNotificationObject.", object.getClass());
71 final CNotification notObj = (CNotification) object;
72 final ByteBuf body = Unpooled.buffer();
73 body.writeZero(NT_F_OFFSET);
74 ByteBufUtils.writeMandatory(body, notObj.getType(), "Type");
75 ByteBufUtils.writeMandatory(body, notObj.getValue(), "Value");
76 serializeTlvs(notObj.getTlvs(), body);
77 ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
80 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
82 if (tlvs.getOverloadDuration() != null) {
83 serializeTlv(tlvs.getOverloadDuration(), body);
85 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
90 protected void addVendorInformationTlvs(final CNotificationBuilder builder, final List<VendorInformationTlv> tlvs) {
91 if (!tlvs.isEmpty()) {
92 builder.setTlvs(new TlvsBuilder(builder.getTlvs()).setVendorInformationTlv(tlvs).build());