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