Removed checkstyle warnings.
[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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
19 import org.opendaylight.protocol.util.ByteArray;
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     private static final int ET_F_LENGTH = 1;
40     private static final int EV_F_LENGTH = 1;
41
42     private static final int FLAGS_F_OFFSET = 1;
43     private static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
44     private static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
45     private static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
46
47     public PCEPErrorObjectParser(final TlvRegistry tlvReg) {
48         super(tlvReg);
49     }
50
51     @Override
52     public ErrorObject parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
53         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
54         final ErrorObjectBuilder builder = new ErrorObjectBuilder();
55         builder.setIgnore(header.isIgnore());
56         builder.setProcessingRule(header.isProcessingRule());
57         bytes.readerIndex(bytes.readerIndex() + ET_F_OFFSET);
58         builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
59         builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
60         parseTlvs(builder, bytes.slice());
61         return builder.build();
62     }
63
64     @Override
65     public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
66         if (tlv instanceof ReqMissing && builder.getType() == 7) {
67             builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
68         }
69     }
70
71     @Override
72     public byte[] serializeObject(final Object object) {
73         if (!(object instanceof ErrorObject)) {
74             throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
75         }
76         final ErrorObject errObj = (ErrorObject) object;
77
78         final byte[] tlvs = serializeTlvs(errObj.getTlvs());
79
80         final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
81         if (tlvs.length != 0) {
82             ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
83         }
84         retBytes[ET_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getType());
85         retBytes[EV_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getValue());
86         return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
87     }
88
89     public byte[] serializeTlvs(final Tlvs tlvs) {
90         if (tlvs == null) {
91             return new byte[0];
92         } else if (tlvs.getReqMissing() != null) {
93             return serializeTlv(tlvs.getReqMissing());
94         }
95         return new byte[0];
96     }
97 }