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 com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedBytes;
14 import io.netty.buffer.ByteBuf;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
21 import org.opendaylight.protocol.pcep.spi.UnknownObject;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Parser for {@link Open}
38 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
39 private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
41 public static final int CLASS = 1;
43 public static final int TYPE = 1;
46 * lengths of fields in bytes
48 private static final int VER_FLAGS_MF_LENGTH = 1;
49 private static final int KEEPALIVE_F_LENGTH = 1;
50 private static final int DEAD_TIMER_LENGTH = 1;
51 private static final int SID_F_LENGTH = 1;
54 * lengths of subfields inside multi-field in bits
56 private static final int VERSION_SF_LENGTH = 3;
59 * offsets of field in bytes
61 private static final int VER_FLAGS_MF_OFFSET = 0;
62 private static final int KEEPALIVE_F_OFFSET = VER_FLAGS_MF_OFFSET + VER_FLAGS_MF_LENGTH;
63 private static final int DEAD_TIMER_OFFSET = KEEPALIVE_F_OFFSET + KEEPALIVE_F_LENGTH;
64 private static final int SID_F_OFFSET = DEAD_TIMER_OFFSET + DEAD_TIMER_LENGTH;
65 private static final int TLVS_OFFSET = SID_F_OFFSET + SID_F_LENGTH;
68 * offsets of subfields inside multi-field in bits
70 private static final int VERSION_SF_OFFSET = 0;
72 private static final int PCEP_VERSION = 1;
74 public PCEPOpenObjectParser(final TlvRegistry tlvReg) {
79 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
80 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
81 final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), 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.setKeepalive((short) UnsignedBytes.toInt(bytes.readByte()));
88 builder.setDeadTimer((short) UnsignedBytes.toInt(bytes.readByte()));
89 builder.setSessionId((short) UnsignedBytes.toInt(bytes.readByte()));
91 final TlvsBuilder tbuilder = new TlvsBuilder();
92 parseTlvs(tbuilder, bytes.slice());
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);
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);
132 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), bytes);
135 public byte[] serializeTlvs(final Tlvs tlvs) {
139 byte[] ofListBytes = null;
140 if (tlvs.getOfList() != null) {
141 ofListBytes = serializeTlv(tlvs.getOfList());
143 byte[] result = new byte[0];
144 if (ofListBytes != null) {
145 result = new byte[ofListBytes.length];
146 ByteArray.copyWhole(ofListBytes, result, 0);