2 * Copyright (c) 2014 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.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.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.monitoring.object.Monitoring;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.Monitoring.Flags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.MonitoringBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.monitoring.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.object.monitoring.TlvsBuilder;
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.Uint32;
30 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
33 * Parser for {@link Monitoring}.
34 * @see <a href="https://tools.ietf.org/html/rfc5886#section-4.1">Monitoring Object</a>
37 public class PCEPMonitoringObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38 private static final int CLASS = 19;
39 private static final int TYPE = 1;
41 private static final int FLAGS_SIZE = 24;
42 private static final int RESERVED = 1;
43 private static final int L_FLAG_POS = 23;
44 private static final int G_FLAG_POS = 22;
45 private static final int P_FLAG_POS = 21;
46 private static final int C_FLAG_POS = 20;
47 private static final int I_FLAG_POS = 19;
49 public PCEPMonitoringObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
50 super(tlvReg, viTlvReg, CLASS, TYPE);
54 public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
55 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
56 buffer.skipBytes(RESERVED);
57 final BitArray flagBits = BitArray.valueOf(buffer, FLAGS_SIZE);
58 final Flags flags = new Flags(flagBits.get(G_FLAG_POS), flagBits.get(I_FLAG_POS), flagBits.get(L_FLAG_POS),
59 flagBits.get(C_FLAG_POS), flagBits.get(P_FLAG_POS));
60 final Uint32 monitoring = ByteBufUtils.readUint32(buffer);
61 final TlvsBuilder tbuilder = new TlvsBuilder();
62 parseTlvs(tbuilder, buffer.slice());
63 return new MonitoringBuilder()
65 .setMonitoringId(monitoring)
66 .setTlvs(tbuilder.build())
71 public void serializeObject(final Object object, final ByteBuf buffer) {
72 checkArgument(object instanceof Monitoring, "Wrong instance of PCEPObject. Passed %s. Needed MonitoringObject.",
74 final Monitoring monitoring = (Monitoring) object;
75 final ByteBuf body = Unpooled.buffer();
76 body.writeZero(RESERVED);
77 final Flags flags = monitoring.getFlags();
78 final BitArray flagBits = new BitArray(FLAGS_SIZE);
79 flagBits.set(I_FLAG_POS, flags.getIncomplete());
80 flagBits.set(C_FLAG_POS, flags.getOverload());
81 flagBits.set(P_FLAG_POS, flags.getProcessingTime());
82 flagBits.set(G_FLAG_POS, flags.getGeneral());
83 flagBits.set(L_FLAG_POS, flags.getLiveness());
84 flagBits.toByteBuf(body);
85 ByteBufUtils.writeOrZero(body, monitoring.getMonitoringId());
86 serializeTlvs(monitoring.getTlvs(), body);
87 ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
91 protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
92 if (!tlvs.isEmpty()) {
93 builder.setVendorInformationTlv(tlvs);
97 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
99 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);