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.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;
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 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.");
79 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
81 if (versionValue != PCEP_VERSION) {
82 // LOG.info("Unsupported PCEP version {}", versionValue);
83 return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED);
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]));
93 final TlvsBuilder tbuilder = new TlvsBuilder();
94 parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
95 builder.setTlvs(tbuilder.build());
96 return builder.build();
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);
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.");
117 final Open open = (Open) object;
119 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
121 final byte[] tlvs = serializeTlvs(open.getTlvs());
123 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
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);
135 public byte[] serializeTlvs(final Tlvs tlvs) {
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;
148 if (tlvs.getStateful() != null) {
149 statefulBytes = serializeTlv(tlvs.getStateful());
150 finalLength += statefulBytes.length;
152 if (tlvs.getPredundancyGroupId() != null) {
153 predundancyBytes = serializeTlv(tlvs.getPredundancyGroupId());
154 finalLength += predundancyBytes.length;
156 if (tlvs.getLspDbVersion() != null) {
157 lspDbBytes = serializeTlv(tlvs.getLspDbVersion());
158 finalLength += lspDbBytes.length;
161 final byte[] result = new byte[finalLength];
162 if (ofListBytes != null) {
163 ByteArray.copyWhole(ofListBytes, result, offset);
164 offset += ofListBytes.length;
166 if (statefulBytes != null) {
167 ByteArray.copyWhole(statefulBytes, result, offset);
168 offset += statefulBytes.length;
170 if (lspDbBytes != null) {
171 ByteArray.copyWhole(lspDbBytes, result, offset);
172 offset += lspDbBytes.length;
174 if (predundancyBytes != null) {
175 ByteArray.copyWhole(predundancyBytes, result, offset);
176 offset += predundancyBytes.length;
182 public int getObjectType() {
187 public int getObjectClass() {