Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPNotificationObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.parser.object;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
30
31 /**
32  * Parser for {@link CNotification}.
33  */
34 public final class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
35     private static final int CLASS = 12;
36     private static final int TYPE = 1;
37
38     /*
39      * offsets of fields
40      */
41     private static final int NT_F_OFFSET = 2;
42
43     public PCEPNotificationObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
44         super(tlvReg, viTlvReg, CLASS, TYPE);
45     }
46
47     @Override
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();
58     }
59
60     @Override
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());
64         }
65     }
66
67     @Override
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);
78     }
79
80     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
81         if (tlvs != null) {
82             if (tlvs.getOverloadDuration() != null) {
83                 serializeTlv(tlvs.getOverloadDuration(), body);
84             }
85             serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
86         }
87     }
88
89     @Override
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());
93         }
94     }
95 }