Add new revision for pcep types model
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.CNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.CNotificationBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.c.notification.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.notification.object.c.notification.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.duration.tlv.OverloadDuration;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
30
31 /**
32  * Parser for {@link CNotification}
33  */
34 public final class PCEPNotificationObjectParser extends AbstractObjectWithTlvsParser<CNotificationBuilder> {
35
36     private static final int CLASS = 12;
37     private static final int TYPE = 1;
38
39     /*
40      * offsets of fields
41      */
42     private static final int NT_F_OFFSET = 2;
43
44     public PCEPNotificationObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
45         super(tlvReg, viTlvReg, CLASS, TYPE);
46     }
47
48     @Override
49     public CNotification parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
50         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
51             "Array of bytes is mandatory. Can't be null or empty.");
52         final CNotificationBuilder builder = new CNotificationBuilder();
53         builder.setIgnore(header.isIgnore());
54         builder.setProcessingRule(header.isProcessingRule());
55         bytes.skipBytes(NT_F_OFFSET);
56         builder.setType(bytes.readUnsignedByte());
57         builder.setValue(bytes.readUnsignedByte());
58         parseTlvs(builder, bytes.slice());
59         return builder.build();
60     }
61
62     @Override
63     public void addTlv(final CNotificationBuilder builder, final Tlv tlv) {
64         if (tlv instanceof OverloadDuration && builder.getType() == 2 && builder.getValue() == 1) {
65             builder.setTlvs(new TlvsBuilder().setOverloadDuration((OverloadDuration) tlv).build());
66         }
67     }
68
69     @Override
70     public void serializeObject(final Object object, final ByteBuf buffer) {
71         Preconditions.checkArgument(object instanceof CNotification,
72             "Wrong instance of PCEPObject. Passed %s. Needed CNotificationObject.",
73             object.getClass());
74         final CNotification notObj = (CNotification) object;
75         final ByteBuf body = Unpooled.buffer();
76         body.writeZero(NT_F_OFFSET);
77         Preconditions.checkArgument(notObj.getType() != null, "Type is mandatory.");
78         writeUnsignedByte(notObj.getType(), body);
79         Preconditions.checkArgument(notObj.getValue() != null, "Value is mandatory.");
80         writeUnsignedByte(notObj.getValue(), body);
81         serializeTlvs(notObj.getTlvs(), body);
82         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
83     }
84
85     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
86         if (tlvs == null) {
87             return;
88         }
89         if (tlvs.getOverloadDuration() != null) {
90             serializeTlv(tlvs.getOverloadDuration(), body);
91         }
92         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
93     }
94
95     @Override
96     protected final void addVendorInformationTlvs(
97         final CNotificationBuilder builder,
98         final List<VendorInformationTlv> tlvs) {
99         if (!tlvs.isEmpty()) {
100             builder.setTlvs(new TlvsBuilder(builder.getTlvs()).setVendorInformationTlv(tlvs).build());
101         }
102     }
103 }