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 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.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Parser for {@link Open}
39 public class PCEPOpenObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
40 private static final Logger LOG = LoggerFactory.getLogger(PCEPOpenObjectParser.class);
42 public static final int CLASS = 1;
44 public static final int TYPE = 1;
47 * lengths of subfields inside multi-field in bits
49 private static final int VERSION_SF_LENGTH = 3;
52 * offsets of subfields inside multi-field in bits
54 private static final int VERSION_SF_OFFSET = 0;
56 private static final int PCEP_VERSION = 1;
58 public PCEPOpenObjectParser(final TlvRegistry tlvReg) {
63 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
64 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
65 final int versionValue = ByteArray.copyBitsRange(bytes.readByte(), VERSION_SF_OFFSET, VERSION_SF_LENGTH);
67 final OpenBuilder builder = new OpenBuilder();
68 builder.setVersion(new ProtocolVersion((short) versionValue));
69 builder.setProcessingRule(header.isProcessingRule());
70 builder.setIgnore(header.isIgnore());
71 final short keepalive = (short) UnsignedBytes.toInt(bytes.readByte());
72 builder.setKeepalive(keepalive);
73 final short deadTimer = (short) UnsignedBytes.toInt(bytes.readByte());
75 builder.setDeadTimer((short) 0);
77 builder.setDeadTimer(deadTimer);
79 builder.setSessionId((short) UnsignedBytes.toInt(bytes.readByte()));
81 final TlvsBuilder tbuilder = new TlvsBuilder();
82 parseTlvs(tbuilder, bytes.slice());
83 builder.setTlvs(tbuilder.build());
85 final Open obj = builder.build();
86 if (versionValue != PCEP_VERSION) {
87 // TODO: Should we move this check into the negotiator
88 LOG.debug("Unsupported PCEP version {}", versionValue);
89 return new UnknownObject(PCEPErrors.PCEP_VERSION_NOT_SUPPORTED, obj);
96 public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
97 if (tlv instanceof OfList) {
98 tbuilder.setOfList((OfList) tlv);
103 public void serializeObject(final Object object, final ByteBuf buffer) {
104 Preconditions.checkArgument(object instanceof Open, "Wrong instance of PCEPObject. Passed %s. Needed OpenObject.", object.getClass());
105 final Open open = (Open) object;
106 final ByteBuf body = Unpooled.buffer();
107 writeUnsignedByte((short) (PCEP_VERSION << (Byte.SIZE - VERSION_SF_LENGTH)), body);
108 writeUnsignedByte(open.getKeepalive(), body);
109 writeUnsignedByte(open.getDeadTimer(), body);
110 Preconditions.checkArgument(open.getSessionId() != null, "SessionId is mandatory.");
111 writeUnsignedByte(open.getSessionId(), body);
112 serializeTlvs(open.getTlvs(), body);
113 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
116 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
120 if (tlvs.getOfList() != null) {
121 serializeTlv(tlvs.getOfList(), body);