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