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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
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.pcep.spi.VendorInformationTlvRegistry;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.ByteBufUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ProtocolVersion;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.Tlvs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.open.TlvsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
35 import org.opendaylight.yangtools.yang.common.Uint8;
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 private static final int CLASS = 1;
46 private 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, CLASS, TYPE);
65 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
66 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
67 final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
69 final short keepalive = bytes.readUnsignedByte();
70 final short deadTimer = bytes.readUnsignedByte();
71 final Uint8 sessionId = ByteBufUtils.readUint8(bytes);
72 final TlvsBuilder tbuilder = new TlvsBuilder();
73 parseTlvs(tbuilder, bytes.slice());
74 final OpenBuilder builder = new OpenBuilder()
75 .setVersion(new ProtocolVersion(Uint8.valueOf(versionValue)))
76 .setProcessingRule(header.isProcessingRule())
77 .setIgnore(header.isIgnore())
78 .setKeepalive(Uint8.valueOf(keepalive))
79 .setSessionId(sessionId)
80 .setTlvs(tbuilder.build());
82 builder.setDeadTimer(Uint8.ZERO);
84 builder.setDeadTimer(Uint8.valueOf(deadTimer));
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 checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.",
108 final Open open = (Open) object;
109 final ByteBuf body = Unpooled.buffer();
110 writeUnsignedByte(Uint8.valueOf(PCEP_VERSION << Byte.SIZE - VERSION_SF_LENGTH), body);
111 writeUnsignedByte(open.getKeepalive(), body);
112 writeUnsignedByte(open.getDeadTimer(), body);
113 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);