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.spi.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.UnknownObject;
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 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.primitives.UnsignedBytes;
34 * Parser for {@link Open}
36 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37 private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
39 public static final int CLASS = 1;
41 public static final int TYPE = 1;
44 * lengths of fields in bytes
46 private static final int VER_FLAGS_MF_LENGTH = 1;
47 private static final int KEEPALIVE_F_LENGTH = 1;
48 private static final int DEAD_TIMER_LENGTH = 1;
49 private static final int SID_F_LENGTH = 1;
52 * lengths of subfields inside multi-field in bits
54 private static final int VERSION_SF_LENGTH = 3;
57 * offsets of field in bytes
59 private static final int VER_FLAGS_MF_OFFSET = 0;
60 private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
61 private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
62 private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
63 private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
66 * offsets of subfields inside multi-field in bits
68 private static final int VERSION_SF_OFFSET = 0;
70 private static final int PCEP_VERSION = 1;
72 public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
77 public Object parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
78 if (bytes == null || bytes.length == 0) {
79 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
81 final int versionValue = ByteArray.copyBitsRange(bytes[VER_FLAGS_MF_OFFSET], VERSION_SF_OFFSET, VERSION_SF_LENGTH);
83 final OpenBuilder builder = new OpenBuilder();
84 builder.setVersion(new ProtocolVersion((short) versionValue));
85 builder.setProcessingRule(header.isProcessingRule());
86 builder.setIgnore(header.isIgnore());
87 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes[DEAD_TIMER_OFFSET]));
88 builder.setKeepalive((short) UnsignedBytes.toInt(bytes[KEEPALIVE_F_OFFSET]));
89 builder.setSessionId((short) UnsignedBytes.toInt(bytes[SID_F_OFFSET]));
91 final TlvsBuilder tbuilder = new TlvsBuilder();
92 parseTlvs(tbuilder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
93 builder.setTlvs(tbuilder.build());
95 final Open obj = builder.build();
96 if (versionValue != PCEP_VERSION) {
97 // TODO: Should we move this check into the negotiator
98 LOG.debug("Unsupported PCEP version {}", versionValue);
99 return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
106 public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
107 if (tlv instanceof OfList) {
108 tbuilder.setOfList((OfList) tlv);
109 } else if (tlv instanceof Stateful) {
110 tbuilder.setStateful((Stateful) tlv);
111 } else if (tlv instanceof PredundancyGroupId) {
112 tbuilder.setPredundancyGroupId((PredundancyGroupId) tlv);
113 } else if (tlv instanceof LspDbVersion) {
114 tbuilder.setLspDbVersion((LspDbVersion) tlv);
119 public byte[] serializeObject(final Object object) {
120 if (!(object instanceof Open)) {
121 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
123 final Open open = (Open) object;
125 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
127 final byte[] tlvs = serializeTlvs(open.getTlvs());
129 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
131 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
132 bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
133 bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
134 bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
135 if (tlvs.length != 0) {
136 ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
138 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
141 public byte[] serializeTlvs(final Tlvs tlvs) {
146 byte[] ofListBytes = null;
147 byte[] statefulBytes = null;
148 byte[] predundancyBytes = null;
149 byte[] lspDbBytes = null;
150 if (tlvs.getOfList() != null) {
151 ofListBytes = serializeTlv(tlvs.getOfList());
152 finalLength += ofListBytes.length;
154 if (tlvs.getStateful() != null) {
155 statefulBytes = serializeTlv(tlvs.getStateful());
156 finalLength += statefulBytes.length;
158 if (tlvs.getPredundancyGroupId() != null) {
159 predundancyBytes = serializeTlv(tlvs.getPredundancyGroupId());
160 finalLength += predundancyBytes.length;
162 if (tlvs.getLspDbVersion() != null) {
163 lspDbBytes = serializeTlv(tlvs.getLspDbVersion());
164 finalLength += lspDbBytes.length;
167 final byte[] result = new byte[finalLength];
168 if (ofListBytes != null) {
169 ByteArray.copyWhole(ofListBytes, result, offset);
170 offset += ofListBytes.length;
172 if (statefulBytes != null) {
173 ByteArray.copyWhole(statefulBytes, result, offset);
174 offset += statefulBytes.length;
176 if (lspDbBytes != null) {
177 ByteArray.copyWhole(lspDbBytes, result, offset);
178 offset += lspDbBytes.length;
180 if (predundancyBytes != null) {
181 ByteArray.copyWhole(predundancyBytes, result, offset);
182 offset += predundancyBytes.length;
188 public int getObjectType() {
193 public int getObjectClass() {