Clean pcep/base-parser code
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
11
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;
31
32 /**
33  * Parser for {@link CNotification}.
34  */
35 public final class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
36
37     private static final int CLASS = 12;
38     private static final int TYPE = 1;
39
40     /*
41      * offsets of fields
42      */
43     private static final int NT_F_OFFSET = 2;
44
45     public PCEPNotificationObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
46         super(tlvReg, viTlvReg, CLASS, TYPE);
47     }
48
49     @Override
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();
61     }
62
63     @Override
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());
67         }
68     }
69
70     @Override
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.",
74             object.getClass());
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);
84     }
85
86     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
87         if (tlvs == null) {
88             return;
89         }
90         if (tlvs.getOverloadDuration() != null) {
91             serializeTlv(tlvs.getOverloadDuration(), body);
92         }
93         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
94     }
95
96     @Override
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());
102         }
103     }
104 }