BUG-113: split HandlerRegistry into per-class registries
[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.PCEPDocumentedException;
13 import org.opendaylight.protocol.pcep.PCEPErrors;
14 import org.opendaylight.protocol.pcep.impl.Util;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
16 import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspDbVersionTlv;
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.OfListTlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenObject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PredundancyGroupIdTlv;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.open.message.OpenBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Tlvs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.TlvsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.LspDbVersionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.OfListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.PredundancyGroupIdBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.StatefulBuilder;
35
36 import com.google.common.primitives.UnsignedBytes;
37
38 /**
39  * Parser for {@link OpenObject}
40  */
41
42 public class PCEPOpenObjectParser extends AbstractObjectParser<OpenBuilder> {
43
44         public static final int CLASS = 1;
45
46         public static final int TYPE = 1;
47
48         /*
49          * lengths of fields in bytes
50          */
51         public static final int VER_FLAGS_MF_LENGTH = 1; // multi-field
52         public static final int KEEPALIVE_F_LENGTH = 1;
53         public static final int DEAD_TIMER_LENGTH = 1;
54         public static final int SID_F_LENGTH = 1;
55
56         /*
57          * lengths of subfields inside multi-field in bits
58          */
59         public static final int VERSION_SF_LENGTH = 3;
60         public static final int FLAGS_SF_LENGTH = 5;
61
62         /*
63          * offsets of field in bytes
64          */
65
66         public static final int VER_FLAGS_MF_OFFSET = 0;
67         public static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
68         public static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
69         public static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
70         public static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
71
72         /*
73          * offsets of subfields inside multi-field in bits
74          */
75
76         public static final int VERSION_SF_OFFSET = 0;
77         public static final int FLAGS_SF_OFFSET = VERSION_SF_LENGTH + VERSION_SF_OFFSET;
78
79         private static final int PCEP_VERSION = 1;
80
81         public PCEPOpenObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
82                 super(subobjReg, tlvReg);
83         }
84
85         @Override
86         public OpenObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
87                 if (bytes == null || bytes.length == 0) {
88                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
89                 }
90
91                 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
92
93                 if (versionValue != PCEP_VERSION) {
94                         throw new PCEPDocumentedException("Unsupported PCEP version " + versionValue, PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
95                 }
96
97                 final OpenBuilder builder = new OpenBuilder();
98
99                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
100
101                 builder.setVersion(new ProtocolVersion((short) versionValue));
102                 builder.setProcessingRule(header.isProcessingRule());
103                 builder.setIgnore(header.isIgnore());
104                 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
105                 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
106                 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
107                 return builder.build();
108         }
109
110         @Override
111         public void addTlv(final OpenBuilder builder, final Tlv tlv) {
112                 final TlvsBuilder tbuilder = new TlvsBuilder();
113                 if (tlv instanceof OfListTlv) {
114                         tbuilder.setOfList(new OfListBuilder().setCodes(((OfListTlv) tlv).getCodes()).build());
115                 } else if (tlv instanceof StatefulCapabilityTlv) {
116                         tbuilder.setStateful(new StatefulBuilder().setFlags(((StatefulCapabilityTlv) tlv).getFlags()).build());
117                 } else if (tlv instanceof PredundancyGroupIdTlv) {
118                         tbuilder.setPredundancyGroupId(new PredundancyGroupIdBuilder().setIdentifier(((PredundancyGroupIdTlv) tlv).getIdentifier()).build());
119                 } else if (tlv instanceof LspDbVersionTlv) {
120                         tbuilder.setLspDbVersion(new LspDbVersionBuilder().build());
121                 }
122                 builder.setTlvs(tbuilder.build());
123         }
124
125         @Override
126         public byte[] serializeObject(final Object object) {
127                 if (!(object instanceof OpenObject)) {
128                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
129                 }
130                 final OpenObject open = (OpenObject) object;
131
132                 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
133
134                 final byte[] tlvs = serializeTlvs(open.getTlvs());
135
136                 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
137
138                 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
139                 bytes[KEEPALIVE_F_OFFSET] = ByteArray.shortToBytes(open.getKeepalive())[1];
140                 bytes[DEAD_TIMER_OFFSET] = ByteArray.shortToBytes(open.getDeadTimer())[1];
141                 bytes[SID_F_OFFSET] = ByteArray.shortToBytes(open.getSessionId())[1];
142                 ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
143
144                 return bytes;
145         }
146
147         public byte[] serializeTlvs(final Tlvs tlvs) {
148                 int finalLength = 0;
149                 if (tlvs.getLspDbVersion() != null) {
150                         final byte[] lspDbBytes = serializeTlv(new LspDbVersionBuilder().setVersion(tlvs.getLspDbVersion().getVersion()).build());
151                         finalLength = lspDbBytes.length;
152                 }
153                 if (tlvs.getOfList() != null) {
154                         final byte[] ofListBytes = serializeTlv(new OfListBuilder().setCodes(tlvs.getOfList().getCodes()).build());
155                         finalLength = ofListBytes.length;
156                 }
157
158                 // FIXME: finish
159
160                 final byte[] bytes = new byte[finalLength];
161
162                 // FIXME copy result bytes
163                 return bytes;
164         }
165
166         @Override
167         public int getObjectType() {
168                 return TYPE;
169         }
170
171         @Override
172         public int getObjectClass() {
173                 return CLASS;
174         }
175 }