Enable restart of CSS modules on blueprint container restart
[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
9 package org.opendaylight.protocol.pcep.parser.object;
10
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;
30
31 /**
32  * Parser for {@link Monitoring}
33  * @see <a href="https://tools.ietf.org/html/rfc5886#section-4.1">Monitoring Object</a>
34  *
35  */
36 public class PCEPMonitoringObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37
38     public static final int CLASS = 19;
39
40     public static final int TYPE = 1;
41
42     private static final int FLAGS_SIZE = 24;
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;
49
50     public PCEPMonitoringObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
51         super(tlvReg, viTlvReg);
52     }
53
54     @Override
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 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         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();
68     }
69
70     @Override
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 BitArray flagBits = new BitArray(FLAGS_SIZE);
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         flagBits.toByteBuf(body);
84         ByteBufWriteUtil.writeUnsignedInt(monitoring.getMonitoringId(), body);
85         serializeTlvs(monitoring.getTlvs(), body);
86         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
87     }
88
89     @Override
90     protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
91         if (!tlvs.isEmpty()) {
92             builder.setVendorInformationTlv(tlvs);
93         }
94     }
95
96     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
97         if (tlvs == null) {
98             return;
99         }
100         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
101     }
102
103 }