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