Enable restart of CSS modules on blueprint container restart
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPErrorObjectParser.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.impl.object;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
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.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
21 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
31
32 /**
33  * Parser for {@link ErrorObject}
34  */
35 public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
36
37     public static final int CLASS = 13;
38
39     public static final int TYPE = 1;
40
41     private static final int FLAGS_F_LENGTH = 1;
42
43     private static final int RESERVED  = 1;
44
45     public PCEPErrorObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
46         super(tlvReg, viTlvReg);
47     }
48
49     @Override
50     public ErrorObject parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52         final ErrorObjectBuilder builder = new ErrorObjectBuilder();
53         builder.setIgnore(header.isIgnore());
54         builder.setProcessingRule(header.isProcessingRule());
55         bytes.skipBytes(FLAGS_F_LENGTH + RESERVED);
56         builder.setType(bytes.readUnsignedByte());
57         builder.setValue(bytes.readUnsignedByte());
58         parseTlvs(builder, bytes.slice());
59         return builder.build();
60     }
61
62     @Override
63     public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
64         if (tlv instanceof ReqMissing && builder.getType() == PCEPErrors.SYNC_PATH_COMP_REQ_MISSING.getErrorType()) {
65             builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
66         }
67     }
68
69     @Override
70     public void serializeObject(final Object object, final ByteBuf buffer) {
71         Preconditions.checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
72         final ErrorObject errObj = (ErrorObject) object;
73         final ByteBuf body = Unpooled.buffer();
74         body.writeZero(FLAGS_F_LENGTH + RESERVED);
75         Preconditions.checkArgument(errObj.getType() != null, "Type is mandatory.");
76         writeUnsignedByte(errObj.getType(), body);
77         Preconditions.checkArgument(errObj.getValue() != null, "Value is mandatory.");
78         writeUnsignedByte(errObj.getValue(), body);
79         serializeTlvs(errObj.getTlvs(), body);
80         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
81     }
82
83     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
84         if (tlvs == null) {
85             return;
86         }
87         if (tlvs.getReqMissing() != null) {
88             serializeTlv(tlvs.getReqMissing(), body);
89         }
90         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
91     }
92
93     @Override
94     protected final void addVendorInformationTlvs(final ErrorObjectBuilder builder, final List<VendorInformationTlv> tlvs) {
95         if (!tlvs.isEmpty()) {
96             builder.setTlvs(new TlvsBuilder(builder.getTlvs()).setVendorInformationTlv(tlvs).build());
97         }
98     }
99 }