Bug-2225: Move Path Setup Type TLV to base PCEP
[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.rev131222.SrpIdNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.Tlvs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
32
33 /**
34  * Parser for {@link Srp}
35  */
36 public class Stateful07SrpObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37
38     public static final int CLASS = 33;
39
40     public static final int TYPE = 1;
41
42     protected static final int FLAGS_SIZE = 4;
43
44     protected static final int SRP_ID_SIZE = 4;
45
46     protected static final int TLVS_OFFSET = FLAGS_SIZE + SRP_ID_SIZE;
47
48     protected static final int MIN_SIZE = FLAGS_SIZE + SRP_ID_SIZE;
49
50     protected Stateful07SrpObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
51         super(tlvReg, viTlvReg);
52     }
53
54     @Override
55     public Srp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
56         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
57         if (bytes.readableBytes() < MIN_SIZE) {
58             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: >="
59                     + MIN_SIZE + ".");
60         }
61         final SrpBuilder builder = new SrpBuilder();
62         builder.setIgnore(header.isIgnore());
63         builder.setProcessingRule(header.isProcessingRule());
64         bytes.readerIndex(bytes.readerIndex() + FLAGS_SIZE);
65         builder.setOperationId(new SrpIdNumber(bytes.readUnsignedInt()));
66         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
67         parseTlvs(tlvsBuilder, bytes.slice());
68         builder.setTlvs(tlvsBuilder.build());
69         return builder.build();
70     }
71
72     @Override
73     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
74         if (tlv instanceof SymbolicPathName) {
75             builder.setSymbolicPathName((SymbolicPathName) tlv);
76         }
77         if (tlv instanceof PathSetupType) {
78             builder.setPathSetupType((PathSetupType) tlv);
79         }
80     }
81
82     @Override
83     public void serializeObject(final Object object, final ByteBuf buffer) {
84         Preconditions.checkArgument(object instanceof Srp, "Wrong instance of PCEPObject. Passed %s . Needed SrpObject.", object.getClass());
85         final Srp srp = (Srp) object;
86         final ByteBuf body = Unpooled.buffer();
87         body.writerIndex(body.writerIndex() + FLAGS_SIZE);
88         final SrpIdNumber srpId = srp.getOperationId();
89         Preconditions.checkArgument(srpId != null, "SrpId is mandatory.");
90         writeUnsignedInt(srpId.getValue(), body);
91         serializeTlvs(srp.getTlvs(), body);
92         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
93     }
94
95     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
96         if (tlvs == null) {
97             return;
98         }
99         if (tlvs.getSymbolicPathName() != null) {
100             serializeTlv(tlvs.getSymbolicPathName(), body);
101         }
102         if (tlvs.getPathSetupType() != null) {
103             serializeTlv(tlvs.getPathSetupType(), body);
104         }
105     }
106
107     @Override
108     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
109         if (!tlvs.isEmpty()) {
110             builder.setVendorInformationTlv(tlvs);
111         }
112     }
113 }