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