Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPMonitoringObjectParser.java
1 /*
2  * Copyright (c) 2014 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.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
19 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
20 import org.opendaylight.protocol.util.BitArray;
21 import org.opendaylight.protocol.util.ByteBufUtils;
22 import org.opendaylight.protocol.util.ByteBufWriteUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.Monitoring;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.Monitoring.Flags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.MonitoringBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.monitoring.Tlvs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.monitoring.TlvsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
31 import org.opendaylight.yangtools.yang.common.Uint32;
32
33 /**
34  * Parser for {@link Monitoring}.
35  * @see <a href="https://tools.ietf.org/html/rfc5886#section-4.1">Monitoring Object</a>
36  *
37  */
38 public class PCEPMonitoringObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
39
40     private static final int CLASS = 19;
41     private static final int TYPE = 1;
42
43     private static final int FLAGS_SIZE = 24;
44     private static final int RESERVED = 1;
45     private static final int L_FLAG_POS = 23;
46     private static final int G_FLAG_POS = 22;
47     private static final int P_FLAG_POS = 21;
48     private static final int C_FLAG_POS = 20;
49     private static final int I_FLAG_POS = 19;
50
51     public PCEPMonitoringObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
52         super(tlvReg, viTlvReg, CLASS, TYPE);
53     }
54
55     @Override
56     public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
57         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
58         buffer.readBytes(RESERVED);
59         final BitArray flagBits = BitArray.valueOf(buffer, FLAGS_SIZE);
60         final Flags flags = new Flags(flagBits.get(G_FLAG_POS), flagBits.get(I_FLAG_POS), flagBits.get(L_FLAG_POS),
61                 flagBits.get(C_FLAG_POS), flagBits.get(P_FLAG_POS));
62         final Uint32 monitoring = ByteBufUtils.readUint32(buffer);
63         final TlvsBuilder tbuilder = new TlvsBuilder();
64         parseTlvs(tbuilder, buffer.slice());
65         final MonitoringBuilder builder = new MonitoringBuilder()
66                 .setFlags(flags)
67                 .setMonitoringId(monitoring)
68                 .setTlvs(tbuilder.build());
69         return builder.build();
70     }
71
72     @Override
73     public void serializeObject(final Object object, final ByteBuf buffer) {
74         checkArgument(object instanceof Monitoring, "Wrong instance of PCEPObject. Passed %s. Needed MonitoringObject.",
75             object.getClass());
76         final Monitoring monitoring = (Monitoring) object;
77         final ByteBuf body = Unpooled.buffer();
78         body.writeZero(RESERVED);
79         final Flags flags = monitoring.getFlags();
80         final BitArray flagBits = new BitArray(FLAGS_SIZE);
81         flagBits.set(I_FLAG_POS, flags.isIncomplete());
82         flagBits.set(C_FLAG_POS, flags.isOverload());
83         flagBits.set(P_FLAG_POS, flags.isProcessingTime());
84         flagBits.set(G_FLAG_POS, flags.isGeneral());
85         flagBits.set(L_FLAG_POS, flags.isLiveness());
86         flagBits.toByteBuf(body);
87         ByteBufWriteUtil.writeUnsignedInt(monitoring.getMonitoringId(), body);
88         serializeTlvs(monitoring.getTlvs(), body);
89         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
90     }
91
92     @Override
93     protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
94         if (!tlvs.isEmpty()) {
95             builder.setVendorInformationTlv(tlvs);
96         }
97     }
98
99     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
100         if (tlvs == null) {
101             return;
102         }
103         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
104     }
105
106 }