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
9 package org.opendaylight.protocol.pcep.impl.object;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.BitSet;
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.ByteArray;
22 import org.opendaylight.protocol.util.ByteBufWriteUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.Monitoring;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.Monitoring.Flags;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.MonitoringBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.monitoring.Tlvs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.monitoring.TlvsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
33 * Parser for {@link Monitoring}
34 * @see https://tools.ietf.org/html/rfc5886#section-4.1
36 public class PCEPMonitoringObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38 public static final int CLASS = 19;
40 public static final int TYPE = 1;
42 private static final int FLAGS = 3;
43 private static final int RESERVED = 1;
44 private static final int L_FLAG_POS = 23;
45 private static final int G_FLAG_POS = 22;
46 private static final int P_FLAG_POS = 21;
47 private static final int C_FLAG_POS = 20;
48 private static final int I_FLAG_POS = 19;
50 public PCEPMonitoringObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
51 super(tlvReg, viTlvReg);
55 public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
56 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
57 final MonitoringBuilder builder = new MonitoringBuilder();
58 buffer.readBytes(RESERVED);
59 final BitSet flagBits = ByteArray.bytesToBitSet(buffer.readBytes(FLAGS).array());
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 builder.setFlags(flags);
63 builder.setMonitoringId(buffer.readUnsignedInt());
64 final TlvsBuilder tbuilder = new TlvsBuilder();
65 parseTlvs(tbuilder, buffer.slice());
66 builder.setTlvs(tbuilder.build());
67 return builder.build();
71 public void serializeObject(final Object object, final ByteBuf buffer) {
72 Preconditions.checkArgument(object instanceof Monitoring, "Wrong instance of PCEPObject. Passed %s. Needed MonitoringObject.", object.getClass());
73 final Monitoring monitoring = (Monitoring) object;
74 final ByteBuf body = Unpooled.buffer();
75 body.writeZero(RESERVED);
76 final Flags flags = monitoring.getFlags();
77 final BitSet flagBits = new BitSet(FLAGS);
78 flagBits.set(I_FLAG_POS, flags.isIncomplete());
79 flagBits.set(C_FLAG_POS, flags.isOverload());
80 flagBits.set(P_FLAG_POS, flags.isProcessingTime());
81 flagBits.set(G_FLAG_POS, flags.isGeneral());
82 flagBits.set(L_FLAG_POS, flags.isLiveness());
83 ByteBufWriteUtil.writeBitSet(flagBits, FLAGS, body);
84 ByteBufWriteUtil.writeUnsignedInt(monitoring.getMonitoringId(), body);
85 serializeTlvs(monitoring.getTlvs(), body);
86 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
90 protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
91 if (!tlvs.isEmpty()) {
92 builder.setVendorInformationTlv(tlvs);
96 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
100 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);