Merge "BUG-730 : added test for bgp/concepts"
[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 com.google.common.primitives.UnsignedBytes;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.Tlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
28
29 /**
30  * Parser for {@link ErrorObject}
31  */
32 public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
33
34     public static final int CLASS = 13;
35
36     public static final int TYPE = 1;
37
38     private static final int FLAGS_F_LENGTH = 1;
39
40     private static final int RESERVED  = 1;
41
42     public PCEPErrorObjectParser(final TlvRegistry tlvReg) {
43         super(tlvReg);
44     }
45
46     @Override
47     public ErrorObject parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
48         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
49         final ErrorObjectBuilder builder = new ErrorObjectBuilder();
50         builder.setIgnore(header.isIgnore());
51         builder.setProcessingRule(header.isProcessingRule());
52         bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
53         builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
54         builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
55         parseTlvs(builder, bytes.slice());
56         return builder.build();
57     }
58
59     @Override
60     public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
61         if (tlv instanceof ReqMissing && builder.getType() == 7) {
62             builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
63         }
64     }
65
66     @Override
67     public void serializeObject(final Object object, final ByteBuf buffer) {
68         Preconditions.checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
69         final ErrorObject errObj = (ErrorObject) object;
70         final ByteBuf body = Unpooled.buffer();
71         body.writeZero(FLAGS_F_LENGTH + RESERVED);
72         Preconditions.checkArgument(errObj.getType() != null, "Type is mandatory.");
73         writeUnsignedByte(errObj.getType(), body);
74         Preconditions.checkArgument(errObj.getValue() != null, "Value is mandatory.");
75         writeUnsignedByte(errObj.getValue(), body);
76         serializeTlvs(errObj.getTlvs(), body);
77         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
78     }
79
80     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
81         if (tlvs == null) {
82             return;
83         } else if (tlvs.getReqMissing() != null) {
84             serializeTlv(tlvs.getReqMissing(), body);
85         }
86     }
87 }