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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13 import com.google.common.base.Preconditions;
14 import com.google.common.primitives.UnsignedBytes;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
22 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
23 import org.opendaylight.protocol.pcep.spi.UnknownObject;
24 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Parser for {@link Open}
42 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
43 private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
45 public static final int CLASS = 1;
47 public static final int TYPE = 1;
50 * lengths of subfields inside multi-field in bits
52 private static final int VERSION_SF_LENGTH = 3;
55 * offsets of subfields inside multi-field in bits
57 private static final int VERSION_SF_OFFSET = 0;
59 private static final int PCEP_VERSION = 1;
61 public PCEPOpenObjectParser(TlvRegistry tlvReg, VendorInformationTlvRegistry viTlvReg) {
62 super(tlvReg, viTlvReg);
66 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
67 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
68 final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
70 final OpenBuilder builder = new OpenBuilder();
71 builder.setVersion(new ProtocolVersion((short) versionValue));
72 builder.setProcessingRule(header.isProcessingRule());
73 builder.setIgnore(header.isIgnore());
74 final short keepalive = (short) UnsignedBytes.toInt(bytes.readByte());
75 builder.setKeepalive(keepalive);
76 final short deadTimer = (short) UnsignedBytes.toInt(bytes.readByte());
78 builder.setDeadTimer((short) 0);
80 builder.setDeadTimer(deadTimer);
82 builder.setSessionId((short) UnsignedBytes.toInt(bytes.readByte()));
84 final TlvsBuilder tbuilder = new TlvsBuilder();
85 parseTlvs(tbuilder, bytes.slice());
86 builder.setTlvs(tbuilder.build());
88 final Open obj = builder.build();
89 if (versionValue != PCEP_VERSION) {
90 // TODO: Should we move this check into the negotiator
91 LOG.debug("Unsupported PCEP version {}", versionValue);
92 return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
99 public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
100 if (tlv instanceof OfList) {
101 tbuilder.setOfList((OfList) tlv);
106 public void serializeObject(final Object object, final ByteBuf buffer) {
107 Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
108 final Open open = (Open) object;
109 final ByteBuf body = Unpooled.buffer();
110 writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
111 writeUnsignedByte(open.getKeepalive(), body);
112 writeUnsignedByte(open.getDeadTimer(), body);
113 Preconditions.checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
114 writeUnsignedByte(open.getSessionId(), body);
115 serializeTlvs(open.getTlvs(), body);
116 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
119 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
123 if (tlvs.getOfList() != null) {
124 serializeTlv(tlvs.getOfList(), body);
126 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
130 protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
131 if (!tlvs.isEmpty()) {
132 builder.setVendorInformationTlv(tlvs);