2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.pcep.impl.object;
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;
29 import com.google.common.primitives.UnsignedBytes;
32 * Parser for {@link Open}
35 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37 public static final int CLASS = 1;
39 public static final int TYPE = 1;
42 * lengths of fields in bytes
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;
50 * lengths of subfields inside multi-field in bits
52 private static final int VERSION_SF_LENGTH = 3;
55 * offsets of field in bytes
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;
64 * offsets of subfields inside multi-field in bits
66 private static final int VERSION_SF_OFFSET = 0;
68 private static final int PCEP_VERSION = 1;
70 public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
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.");
79 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
81 if (versionValue != PCEP_VERSION) {
82 throw new PCEPDocumentedException("Unsupported PCEP version " + versionValue, PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
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]));
92 final TlvsBuilder tbuilder = new TlvsBuilder();
93 parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
94 builder.setTlvs(tbuilder.build());
95 return builder.build();
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);
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.");
116 final Open open = (Open) object;
118 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
120 final byte[] tlvs = serializeTlvs(open.getTlvs());
122 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
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);
134 public byte[] serializeTlvs(final Tlvs tlvs) {
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;
147 if (tlvs.getStateful() != null) {
148 statefulBytes = serializeTlv(tlvs.getStateful());
149 finalLength += statefulBytes.length;
151 if (tlvs.getPredundancyGroupId() != null) {
152 predundancyBytes = serializeTlv(tlvs.getPredundancyGroupId());
153 finalLength += predundancyBytes.length;
155 if (tlvs.getLspDbVersion() != null) {
156 lspDbBytes = serializeTlv(tlvs.getLspDbVersion());
157 finalLength += lspDbBytes.length;
160 final byte[] result = new byte[finalLength];
161 if (ofListBytes != null) {
162 ByteArray.copyWhole(ofListBytes, result, offset);
163 offset += ofListBytes.length;
165 if (statefulBytes != null) {
166 ByteArray.copyWhole(statefulBytes, result, offset);
167 offset += statefulBytes.length;
169 if (lspDbBytes != null) {
170 ByteArray.copyWhole(lspDbBytes, result, offset);
171 offset += lspDbBytes.length;
173 if (predundancyBytes != null) {
174 ByteArray.copyWhole(predundancyBytes, result, offset);
175 offset += predundancyBytes.length;
181 public int getObjectType() {
186 public int getObjectClass() {