61e7714b43e5f661d3376dab25185a1b24f0eaee
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPObjectiveFunctionObjectParser.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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
11
12 import com.google.common.base.Preconditions;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.OfId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.object.Of;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.object.OfBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.object.of.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.object.of.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
29 import org.opendaylight.yangtools.yang.common.Uint16;
30 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
31
32 /**
33  * Parser for {@link Of}.
34  */
35 public final class PCEPObjectiveFunctionObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36     private static final int CLASS = 21;
37     private static final int TYPE = 1;
38     private static final int RESERVED = 2;
39
40     public PCEPObjectiveFunctionObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
41         super(tlvReg, viTlvReg, CLASS, TYPE);
42     }
43
44     @Override
45     public Of parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
46         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
47             "Array of bytes is mandatory. Can't be null or empty.");
48         final Uint16 ofId = ByteBufUtils.readUint16(bytes);
49         bytes.readBytes(RESERVED);
50         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
51         parseTlvs(tlvsBuilder, bytes.slice());
52         final OfBuilder builder = new OfBuilder()
53                 .setIgnore(header.isIgnore())
54                 .setProcessingRule(header.isProcessingRule())
55                 .setCode(new OfId(ofId))
56                 .setTlvs(tlvsBuilder.build());
57         return builder.build();
58     }
59
60     @Override
61     public void serializeObject(final Object object, final ByteBuf buffer) {
62         Preconditions.checkArgument(object instanceof Of,
63             "Wrong instance of PCEPObject. Passed %s. Needed OfObject.", object.getClass());
64         final Of specObj = (Of) object;
65         final ByteBuf body = Unpooled.buffer();
66         Preconditions.checkArgument(specObj.getCode() != null, "Code is mandatory");
67         writeUnsignedShort(specObj.getCode().getValue(), body);
68         body.writeZero(RESERVED);
69         serializeTlvs(specObj.getTlvs(), body);
70         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
71     }
72
73     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
74         if (tlvs == null) {
75             return;
76         }
77         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
78     }
79
80     @Override
81     protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
82         if (!tlvs.isEmpty()) {
83             builder.setVendorInformationTlv(tlvs);
84         }
85     }
86 }