Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPOpenObjectParser.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
9 package org.opendaylight.protocol.pcep.impl.object;
10
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.PCEPErrors;
13 import org.opendaylight.protocol.pcep.UnknownObject;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.db.version.tlv.LspDbVersion;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.predundancy.group.id.tlv.PredundancyGroupId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.stateful.capability.tlv.Stateful;
28
29 import com.google.common.primitives.UnsignedBytes;
30
31 /**
32  * Parser for {@link Open}
33  */
34
35 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36
37         public static final int CLASS = 1;
38
39         public static final int TYPE = 1;
40
41         /*
42          * lengths of fields in bytes
43          */
44         private static final int VER_FLAGS_MF_LENGTH = 1;
45         private static final int KEEPALIVE_F_LENGTH = 1;
46         private static final int DEAD_TIMER_LENGTH = 1;
47         private static final int SID_F_LENGTH = 1;
48
49         /*
50          * lengths of subfields inside multi-field in bits
51          */
52         private static final int VERSION_SF_LENGTH = 3;
53
54         /*
55          * offsets of field in bytes
56          */
57         private static final int VER_FLAGS_MF_OFFSET = 0;
58         private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
59         private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
60         private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
61         private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
62
63         /*
64          * offsets of subfields inside multi-field in bits
65          */
66         private static final int VERSION_SF_OFFSET = 0;
67
68         private static final int PCEP_VERSION = 1;
69
70         public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
71                 super(tlvReg);
72         }
73
74         @Override
75         public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
76                 if (bytes == null || bytes.length == 0) {
77                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
78                 }
79                 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
80
81                 if (versionValue != PCEP_VERSION) {
82                         // LOG.info("Unsupported PCEP version {}", versionValue);
83                         return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
84                 }
85                 final OpenBuilder builder = new OpenBuilder();
86                 builder.setVersion(new ProtocolVersion((short) versionValue));
87                 builder.setProcessingRule(header.isProcessingRule());
88                 builder.setIgnore(header.isIgnore());
89                 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
90                 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
91                 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
92
93                 final TlvsBuilder tbuilder = new TlvsBuilder();
94                 parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
95                 builder.setTlvs(tbuilder.build());
96                 return builder.build();
97         }
98
99         @Override
100         public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
101                 if (tlv instanceof OfList) {
102                         tbuilder.setOfList((OfList) tlv);
103                 } else if (tlv instanceof Stateful) {
104                         tbuilder.setStateful((Stateful) tlv);
105                 } else if (tlv instanceof PredundancyGroupId) {
106                         tbuilder.setPredundancyGroupId((PredundancyGroupId) tlv);
107                 } else if (tlv instanceof LspDbVersion) {
108                         tbuilder.setLspDbVersion((LspDbVersion) tlv);
109                 }
110         }
111
112         @Override
113         public byte[] serializeObject(final Object object) {
114                 if (!(object instanceof Open)) {
115                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
116                 }
117                 final Open open = (Open) object;
118
119                 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
120
121                 final byte[] tlvs = serializeTlvs(open.getTlvs());
122
123                 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
124
125                 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
126                 bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
127                 bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
128                 bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
129                 if (tlvs.length != 0) {
130                         ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
131                 }
132                 return bytes;
133         }
134
135         public byte[] serializeTlvs(final Tlvs tlvs) {
136                 if (tlvs == null) {
137                         return new byte[0];
138                 }
139                 int finalLength = 0;
140                 byte[] ofListBytes = null;
141                 byte[] statefulBytes = null;
142                 byte[] predundancyBytes = null;
143                 byte[] lspDbBytes = null;
144                 if (tlvs.getOfList() != null) {
145                         ofListBytes = serializeTlv(tlvs.getOfList());
146                         finalLength += ofListBytes.length;
147                 }
148                 if (tlvs.getStateful() != null) {
149                         statefulBytes = serializeTlv(tlvs.getStateful());
150                         finalLength += statefulBytes.length;
151                 }
152                 if (tlvs.getPredundancyGroupId() != null) {
153                         predundancyBytes = serializeTlv(tlvs.getPredundancyGroupId());
154                         finalLength += predundancyBytes.length;
155                 }
156                 if (tlvs.getLspDbVersion() != null) {
157                         lspDbBytes = serializeTlv(tlvs.getLspDbVersion());
158                         finalLength += lspDbBytes.length;
159                 }
160                 int offset = 0;
161                 final byte[] result = new byte[finalLength];
162                 if (ofListBytes != null) {
163                         ByteArray.copyWhole(ofListBytes, result, offset);
164                         offset += ofListBytes.length;
165                 }
166                 if (statefulBytes != null) {
167                         ByteArray.copyWhole(statefulBytes, result, offset);
168                         offset += statefulBytes.length;
169                 }
170                 if (lspDbBytes != null) {
171                         ByteArray.copyWhole(lspDbBytes, result, offset);
172                         offset += lspDbBytes.length;
173                 }
174                 if (predundancyBytes != null) {
175                         ByteArray.copyWhole(predundancyBytes, result, offset);
176                         offset += predundancyBytes.length;
177                 }
178                 return result;
179         }
180
181         @Override
182         public int getObjectType() {
183                 return TYPE;
184         }
185
186         @Override
187         public int getObjectClass() {
188                 return CLASS;
189         }
190 }