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.ietf.stateful;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
19 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.SrpIdNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.srp.object.Srp;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.srp.object.SrpBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.srp.object.srp.Tlvs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.srp.object.srp.TlvsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.symbolic.path.name.tlv.SymbolicPathName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
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.path.setup.type.tlv.PathSetupType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
31 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
34 * Parser for {@link Srp}.
36 public class StatefulSrpObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37 private static final int CLASS = 33;
38 private static final int TYPE = 1;
40 protected static final int FLAGS_SIZE = 32;
41 protected static final int SRP_ID_SIZE = 4;
42 protected static final int MIN_SIZE = FLAGS_SIZE / Byte.SIZE + SRP_ID_SIZE;
44 protected StatefulSrpObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
45 super(tlvReg, viTlvReg, CLASS, TYPE);
49 public Srp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
50 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
51 if (bytes.readableBytes() < MIN_SIZE) {
52 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
53 + "; Expected: >=" + MIN_SIZE + ".");
55 final SrpBuilder builder = new SrpBuilder()
56 .setIgnore(header.getIgnore())
57 .setProcessingRule(header.getProcessingRule());
58 parseFlags(builder, bytes);
59 builder.setOperationId(new SrpIdNumber(ByteBufUtils.readUint32(bytes)));
60 final TlvsBuilder tlvsBuilder = new TlvsBuilder();
61 parseTlvs(tlvsBuilder, bytes.slice());
62 return builder.setTlvs(tlvsBuilder.build()).build();
65 protected void parseFlags(final SrpBuilder builder, final ByteBuf bytes) {
66 bytes.skipBytes(FLAGS_SIZE / Byte.SIZE);
70 public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
71 if (tlv instanceof SymbolicPathName symbolic) {
72 builder.setSymbolicPathName(symbolic);
74 if (tlv instanceof PathSetupType setup) {
75 builder.setPathSetupType(setup);
80 public void serializeObject(final Object object, final ByteBuf buffer) {
81 checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.",
83 final Srp srp = (Srp) object;
84 final ByteBuf body = Unpooled.buffer();
85 serializeFlags(srp, body);
86 final SrpIdNumber srpId = srp.getOperationId();
87 checkArgument(srpId != null, "SrpId is mandatory.");
88 ByteBufUtils.write(body, srpId.getValue());
89 serializeTlvs(srp.getTlvs(), body);
90 ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
93 protected void serializeFlags(final Srp srp, final ByteBuf body) {
94 body.writeZero(FLAGS_SIZE / Byte.SIZE);
97 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
99 if (tlvs.getSymbolicPathName() != null) {
100 serializeTlv(tlvs.getSymbolicPathName(), body);
102 if (tlvs.getPathSetupType() != null) {
103 serializeTlv(tlvs.getPathSetupType(), body);
109 protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
110 if (!tlvs.isEmpty()) {
111 builder.setVendorInformationTlv(tlvs);