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