Add new revision for pcep types model
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
11
12 import com.google.common.base.Preconditions;
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
33 /**
34  * Parser for {@link Srp}
35  */
36 public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37
38     private static final int CLASS = 33;
39     private static final int TYPE = 1;
40
41     protected static final int FLAGS_SIZE = 32;
42
43     protected static final int SRP_ID_SIZE = 4;
44
45     protected static final int MIN_SIZE = FLAGS_SIZE / Byte.SIZE + SRP_ID_SIZE;
46
47     protected Stateful07SrpObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
48         super(tlvReg, viTlvReg, CLASS, TYPE);
49     }
50
51     @Override
52     public Srp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
53         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
54         if (bytes.readableBytes() < MIN_SIZE) {
55             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: >="
56                     + MIN_SIZE + ".");
57         }
58         final SrpBuilder builder = new SrpBuilder();
59         builder.setIgnore(header.isIgnore());
60         builder.setProcessingRule(header.isProcessingRule());
61         parseFlags(builder, bytes);
62         builder.setOperationId(new SrpIdNumber(bytes.readUnsignedInt()));
63         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
64         parseTlvs(tlvsBuilder, bytes.slice());
65         builder.setTlvs(tlvsBuilder.build());
66         return builder.build();
67     }
68
69     protected void parseFlags(final SrpBuilder builder, final ByteBuf bytes) {
70         bytes.skipBytes(FLAGS_SIZE / Byte.SIZE);
71     }
72
73     @Override
74     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
75         if (tlv instanceof SymbolicPathName) {
76             builder.setSymbolicPathName((SymbolicPathName) tlv);
77         }
78         if (tlv instanceof PathSetupType) {
79             builder.setPathSetupType((PathSetupType) tlv);
80         }
81     }
82
83     @Override
84     public void serializeObject(final Object object, final ByteBuf buffer) {
85         Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.", object.getClass());
86         final Srp srp = (Srp) object;
87         final ByteBuf body = Unpooled.buffer();
88         serializeFlags(srp, body);
89         final SrpIdNumber srpId = srp.getOperationId();
90         Preconditions.checkArgument(srpId != null, "SrpId is mandatory.");
91         writeUnsignedInt(srpId.getValue(), body);
92         serializeTlvs(srp.getTlvs(), body);
93         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
94     }
95
96     protected void serializeFlags(final Srp srp, final ByteBuf body) {
97         body.writeZero(FLAGS_SIZE / Byte.SIZE);
98     }
99
100     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
101         if (tlvs == null) {
102             return;
103         }
104         if (tlvs.getSymbolicPathName() != null) {
105             serializeTlv(tlvs.getSymbolicPathName(), body);
106         }
107         if (tlvs.getPathSetupType() != null) {
108             serializeTlv(tlvs.getPathSetupType(), body);
109         }
110     }
111
112     @Override
113     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
114         if (!tlvs.isEmpty()) {
115             builder.setVendorInformationTlv(tlvs);
116         }
117     }
118 }