Removed unused PCEPDocumentedException from object parsers.
[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 org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.Tlvs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
21
22 import com.google.common.primitives.UnsignedBytes;
23
24 /**
25  * Parser for {@link ErrorObject}
26  */
27 public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
28
29         public static final int CLASS = 13;
30
31         public static final int TYPE = 1;
32
33         private static final int FLAGS_F_LENGTH = 1;
34         private static final int ET_F_LENGTH = 1;
35         private static final int EV_F_LENGTH = 1;
36
37         private static final int FLAGS_F_OFFSET = 1;
38         private static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
39         private static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
40         private static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
41
42         public PCEPErrorObjectParser(final TlvHandlerRegistry tlvReg) {
43                 super(tlvReg);
44         }
45
46         @Override
47         public ErrorObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
48                 if (bytes == null) {
49                         throw new IllegalArgumentException("Array of bytes is mandatory.");
50                 }
51
52                 final ErrorObjectBuilder builder = new ErrorObjectBuilder();
53                 builder.setIgnore(header.isIgnore());
54                 builder.setProcessingRule(header.isProcessingRule());
55                 builder.setType((short) UnsignedBytes.toInt(bytes[ET_F_OFFSET]));
56                 builder.setValue((short) UnsignedBytes.toInt(bytes[EV_F_OFFSET]));
57                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
58                 return builder.build();
59         }
60
61         @Override
62         public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
63                 if (tlv instanceof ReqMissing && builder.getType() == 7) {
64                         builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
65                 }
66         }
67
68         @Override
69         public byte[] serializeObject(final Object object) {
70                 if (!(object instanceof ErrorObject)) {
71                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
72                 }
73                 final ErrorObject errObj = (ErrorObject) object;
74
75                 final byte[] tlvs = serializeTlvs(errObj.getTlvs());
76
77                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
78                 if (tlvs.length != 0) {
79                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
80                 }
81                 retBytes[ET_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getType());
82                 retBytes[EV_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getValue());
83                 return retBytes;
84         }
85
86         public byte[] serializeTlvs(final Tlvs tlvs) {
87                 if (tlvs == null) {
88                         return new byte[0];
89                 } else if (tlvs.getReqMissing() != null) {
90                         return serializeTlv(tlvs.getReqMissing());
91                 }
92                 return new byte[0];
93         }
94
95         @Override
96         public int getObjectType() {
97                 return TYPE;
98         }
99
100         @Override
101         public int getObjectClass() {
102                 return CLASS;
103         }
104 }