36faaaa212833210884a72b28cac7e3bd3452bc5
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07SrpObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.ietf.stateful07;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12
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.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.SrpIdNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.Srp;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.SrpBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.srp.Tlvs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.srp.object.srp.TlvsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.symbolic.path.name.tlv.SymbolicPathName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.setup.type.tlv.PathSetupType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
32 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
33
34 /**
35  * Parser for {@link Srp}.
36  */
37 public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38     private static final int CLASS = 33;
39     private static final int TYPE = 1;
40
41     protected static final int FLAGS_SIZE = 32;
42     protected static final int SRP_ID_SIZE = 4;
43     protected static final int MIN_SIZE = FLAGS_SIZE / Byte.SIZE + SRP_ID_SIZE;
44
45     protected Stateful07SrpObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
46         super(tlvReg, viTlvReg, CLASS, TYPE);
47     }
48
49     @Override
50     public Srp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52         if (bytes.readableBytes() < MIN_SIZE) {
53             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
54                 + "; Expected: >=" + MIN_SIZE + ".");
55         }
56         final SrpBuilder builder = new SrpBuilder()
57                 .setIgnore(header.isIgnore())
58                 .setProcessingRule(header.isProcessingRule());
59         parseFlags(builder, bytes);
60         builder.setOperationId(new SrpIdNumber(ByteBufUtils.readUint32(bytes)));
61         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
62         parseTlvs(tlvsBuilder, bytes.slice());
63         return builder.setTlvs(tlvsBuilder.build()).build();
64     }
65
66     protected void parseFlags(final SrpBuilder builder, final ByteBuf bytes) {
67         bytes.skipBytes(FLAGS_SIZE / Byte.SIZE);
68     }
69
70     @Override
71     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
72         if (tlv instanceof SymbolicPathName) {
73             builder.setSymbolicPathName((SymbolicPathName) tlv);
74         }
75         if (tlv instanceof PathSetupType) {
76             builder.setPathSetupType((PathSetupType) tlv);
77         }
78     }
79
80     @Override
81     public void serializeObject(final Object object, final ByteBuf buffer) {
82         checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.",
83             object.getClass());
84         final Srp srp = (Srp) object;
85         final ByteBuf body = Unpooled.buffer();
86         serializeFlags(srp, body);
87         final SrpIdNumber srpId = srp.getOperationId();
88         checkArgument(srpId != null, "SrpId is mandatory.");
89         writeUnsignedInt(srpId.getValue(), body);
90         serializeTlvs(srp.getTlvs(), body);
91         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
92     }
93
94     protected void serializeFlags(final Srp srp, final ByteBuf body) {
95         body.writeZero(FLAGS_SIZE / Byte.SIZE);
96     }
97
98     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
99         if (tlvs == null) {
100             return;
101         }
102         if (tlvs.getSymbolicPathName() != null) {
103             serializeTlv(tlvs.getSymbolicPathName(), body);
104         }
105         if (tlvs.getPathSetupType() != null) {
106             serializeTlv(tlvs.getPathSetupType(), body);
107         }
108     }
109
110     @Override
111     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
112         if (!tlvs.isEmpty()) {
113             builder.setVendorInformationTlv(tlvs);
114         }
115     }
116 }