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.ietf.stateful.rev131222.Tlvs2;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2Builder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
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 final Tlvs2Builder statefulBuilder = new Tlvs2Builder();
108 if (tbuilder.getAugmentation(Tlvs2.class) != null) {
109 final Tlvs2 t = tbuilder.getAugmentation(Tlvs2.class);
110 if (t.getClass() != null) {
111 statefulBuilder.setStateful(t.getStateful());
114 if (tlv instanceof OfList) {
115 tbuilder.setOfList((OfList) tlv);
116 } else if (tlv instanceof Stateful) {
117 statefulBuilder.setStateful((Stateful) tlv);
119 tbuilder.addAugmentation(Tlvs2.class, statefulBuilder.build());
123 public byte[] serializeObject(final Object object) {
124 if (!(object instanceof Open)) {
125 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed OpenObject.");
127 final Open open = (Open) object;
129 final byte versionFlagMF = (byte) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH));
131 final byte[] tlvs = serializeTlvs(open.getTlvs());
133 final byte[] bytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
135 bytes[VER_FLAGS_MF_OFFSET] = versionFlagMF;
136 bytes[KEEPALIVE_F_OFFSET] = UnsignedBytes.checkedCast(open.getKeepalive());
137 bytes[DEAD_TIMER_OFFSET] = UnsignedBytes.checkedCast(open.getDeadTimer());
138 bytes[SID_F_OFFSET] = UnsignedBytes.checkedCast(open.getSessionId());
139 if (tlvs.length != 0) {
140 ByteArray.copyWhole(tlvs, bytes, TLVS_OFFSET);
142 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
145 public byte[] serializeTlvs(final Tlvs tlvs) {
150 byte[] ofListBytes = null;
151 byte[] statefulBytes = null;
152 if (tlvs.getOfList() != null) {
153 ofListBytes = serializeTlv(tlvs.getOfList());
154 finalLength += ofListBytes.length;
156 if (tlvs.getAugmentation(Tlvs2.class) != null) {
157 final Tlvs2 statefulTlvs = tlvs.getAugmentation(Tlvs2.class);
158 if (statefulTlvs.getStateful() != null) {
159 statefulBytes = serializeTlv(statefulTlvs.getStateful());
160 finalLength += statefulBytes.length;
165 final byte[] result = new byte[finalLength];
166 if (ofListBytes != null) {
167 ByteArray.copyWhole(ofListBytes, result, offset);
168 offset += ofListBytes.length;
170 if (statefulBytes != null) {
171 ByteArray.copyWhole(statefulBytes, result, offset);
172 offset += statefulBytes.length;
178 public int getObjectType() {
183 public int getObjectClass() {