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.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.ByteBufWriteUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.Monitoring;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.Monitoring.Flags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.MonitoringBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.monitoring.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.object.monitoring.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
32 * Parser for {@link Monitoring}
33 * @see https://tools.ietf.org/html/rfc5886#section-4.1
35 public class PCEPMonitoringObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37 public static final int CLASS = 19;
39 public 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);
54 public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
55 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
56 final MonitoringBuilder builder = new MonitoringBuilder();
57 buffer.readBytes(RESERVED);
58 final BitArray flagBits = BitArray.valueOf(buffer, FLAGS_SIZE);
59 final Flags flags = new Flags(flagBits.get(G_FLAG_POS), flagBits.get(I_FLAG_POS), flagBits.get(L_FLAG_POS),
60 flagBits.get(C_FLAG_POS), flagBits.get(P_FLAG_POS));
61 builder.setFlags(flags);
62 builder.setMonitoringId(buffer.readUnsignedInt());
63 final TlvsBuilder tbuilder = new TlvsBuilder();
64 parseTlvs(tbuilder, buffer.slice());
65 builder.setTlvs(tbuilder.build());
66 return builder.build();
70 public void serializeObject(final Object object, final ByteBuf buffer) {
71 Preconditions.checkArgument(object instanceof Monitoring, "Wrong instance of PCEPObject. Passed %s. Needed MonitoringObject.", object.getClass());
72 final Monitoring monitoring = (Monitoring) object;
73 final ByteBuf body = Unpooled.buffer();
74 body.writeZero(RESERVED);
75 final Flags flags = monitoring.getFlags();
76 final BitArray flagBits = new BitArray(FLAGS_SIZE);
77 flagBits.set(I_FLAG_POS, flags.isIncomplete());
78 flagBits.set(C_FLAG_POS, flags.isOverload());
79 flagBits.set(P_FLAG_POS, flags.isProcessingTime());
80 flagBits.set(G_FLAG_POS, flags.isGeneral());
81 flagBits.set(L_FLAG_POS, flags.isLiveness());
82 flagBits.toByteBuf(body);
83 ByteBufWriteUtil.writeUnsignedInt(monitoring.getMonitoringId(), body);
84 serializeTlvs(monitoring.getTlvs(), body);
85 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
89 protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
90 if (!tlvs.isEmpty()) {
91 builder.setVendorInformationTlv(tlvs);
95 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
99 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);