4271137ca866a07ddd69d9cd4be654da5db3e695
[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.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 Open parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
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                         throw new PCEPDocumentedException("Unsupported PCEP version " + versionValue, PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
83                 }
84                 final OpenBuilder builder = new OpenBuilder();
85                 builder.setVersion(new ProtocolVersion((short) versionValue));
86                 builder.setProcessingRule(header.isProcessingRule());
87                 builder.setIgnore(header.isIgnore());
88                 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
89                 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
90                 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
91
92                 final TlvsBuilder tbuilder = new TlvsBuilder();
93                 parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
94                 builder.setTlvs(tbuilder.build());
95                 return builder.build();
96         }
97
98         @Override
99         public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
100                 if (tlv instanceof OfList) {
101                         tbuilder.setOfList((OfList) tlv);
102                 } else if (tlv instanceof Stateful) {
103                         tbuilder.setStateful((Stateful) tlv);
104                 } else if (tlv instanceof PredundancyGroupId) {
105                         tbuilder.setPredundancyGroupId((PredundancyGroupId) tlv);
106                 } else if (tlv instanceof LspDbVersion) {
107                         tbuilder.setLspDbVersion((LspDbVersion) tlv);
108                 }
109         }
110
111         @Override
112         public byte[] serializeObject(final Object object) {
113                 if (!(object instanceof Open)) {
114                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
115                 }
116                 final Open open = (Open) object;
117
118                 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
119
120                 final byte[] tlvs = serializeTlvs(open.getTlvs());
121
122                 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
123
124                 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
125                 bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
126                 bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
127                 bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
128                 if (tlvs.length != 0) {
129                         ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
130                 }
131                 return bytes;
132         }
133
134         public byte[] serializeTlvs(final Tlvs tlvs) {
135                 if (tlvs == null) {
136                         return new byte[0];
137                 }
138                 int finalLength = 0;
139                 byte[] ofListBytes = null;
140                 byte[] statefulBytes = null;
141                 byte[] predundancyBytes = null;
142                 byte[] lspDbBytes = null;
143                 if (tlvs.getOfList() != null) {
144                         ofListBytes = serializeTlv(tlvs.getOfList());
145                         finalLength += ofListBytes.length;
146                 }
147                 if (tlvs.getStateful() != null) {
148                         statefulBytes = serializeTlv(tlvs.getStateful());
149                         finalLength += statefulBytes.length;
150                 }
151                 if (tlvs.getPredundancyGroupId() != null) {
152                         predundancyBytes = serializeTlv(tlvs.getPredundancyGroupId());
153                         finalLength += predundancyBytes.length;
154                 }
155                 if (tlvs.getLspDbVersion() != null) {
156                         lspDbBytes = serializeTlv(tlvs.getLspDbVersion());
157                         finalLength += lspDbBytes.length;
158                 }
159                 int offset = 0;
160                 final byte[] result = new byte[finalLength];
161                 if (ofListBytes != null) {
162                         ByteArray.copyWhole(ofListBytes, result, offset);
163                         offset += ofListBytes.length;
164                 }
165                 if (statefulBytes != null) {
166                         ByteArray.copyWhole(statefulBytes, result, offset);
167                         offset += statefulBytes.length;
168                 }
169                 if (lspDbBytes != null) {
170                         ByteArray.copyWhole(lspDbBytes, result, offset);
171                         offset += lspDbBytes.length;
172                 }
173                 if (predundancyBytes != null) {
174                         ByteArray.copyWhole(predundancyBytes, result, offset);
175                         offset += predundancyBytes.length;
176                 }
177                 return result;
178         }
179
180         @Override
181         public int getObjectType() {
182                 return TYPE;
183         }
184
185         @Override
186         public int getObjectClass() {
187                 return CLASS;
188         }
189 }