d45af743da731e81e802697b93c4d6cebdb9ba88
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPGlobalConstraintsObjectParser.java
1 /*
2  * Copyright (c) 2013 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 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
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.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.gc.object.Gc;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.gc.object.GcBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.gc.object.gc.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.gc.object.gc.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
29
30 /**
31  * Parser for {@link Gc}.
32  */
33 public class PCEPGlobalConstraintsObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
34
35     private static final int CLASS = 24;
36     private static final int TYPE = 1;
37
38     public PCEPGlobalConstraintsObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
39         super(tlvReg, viTlvReg, CLASS, TYPE);
40     }
41
42     @Override
43     public Gc parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
44         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
45         final GcBuilder builder = new GcBuilder()
46                 .setIgnore(header.isIgnore())
47                 .setProcessingRule(header.isProcessingRule())
48                 .setMaxHop(ByteBufUtils.readUint8(bytes))
49                 .setMaxUtilization(ByteBufUtils.readUint8(bytes))
50                 .setMinUtilization(ByteBufUtils.readUint8(bytes))
51                 .setOverBookingFactor(ByteBufUtils.readUint8(bytes));
52         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
53         parseTlvs(tlvsBuilder, bytes.slice());
54         builder.setTlvs(tlvsBuilder.build());
55         return builder.build();
56     }
57
58     @Override
59     public void serializeObject(final Object object, final ByteBuf buffer) {
60         checkArgument(object instanceof Gc,
61             "Wrong instance of PCEPObject. Passed %s. Needed GcObject.", object.getClass());
62         final Gc specObj = (Gc) object;
63         final ByteBuf body = Unpooled.buffer();
64         writeUnsignedByte(specObj.getMaxHop(), body);
65         writeUnsignedByte(specObj.getMaxUtilization(), body);
66         writeUnsignedByte(specObj.getMinUtilization(), body);
67         writeUnsignedByte(specObj.getOverBookingFactor(), body);
68         serializeTlvs(specObj.getTlvs(), body);
69         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
70     }
71
72     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
73         if (tlvs == null) {
74             return;
75         }
76         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
77     }
78
79     @Override
80     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
81         if (!tlvs.isEmpty()) {
82             builder.setVendorInformationTlv(tlvs);
83         }
84     }
85 }