c4319f76d76d0e36b68cb9045c0459cc7a26d7ec
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02LspObjectParser.java
1 /*
2  * Copyright (c) 2014 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.stateful02;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import java.util.BitSet;
16
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.TlvRegistry;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PlspId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.LspBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
33
34 /**
35  * Parser for {@link Lsp}
36  */
37 public class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38
39     public static final int CLASS = 32;
40
41     public static final int TYPE = 1;
42
43     /*
44      * first 4b are restricted
45      */
46     private static final int DELEGATE_FLAG_OFFSET = 15;
47     private static final int SYNC_FLAG_OFFSET = 14;
48     private static final int OPERATIONAL_FLAG_OFFSET = 13;
49     private static final int REMOVE_FLAG_OFFSET = 12;
50
51     public Stateful02LspObjectParser(final TlvRegistry tlvReg) {
52         super(tlvReg);
53     }
54
55     @Override
56     public Lsp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
57         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
58         final LspBuilder builder = new LspBuilder();
59         builder.setIgnore(header.isIgnore());
60         builder.setProcessingRule(header.isProcessingRule());
61         int[] plspIdRaw = { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2) };
62         builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << 12) | (plspIdRaw[1] << 4) | (plspIdRaw[2] >> 4))));
63         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, 2));
64         builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
65         builder.setSync(flags.get(SYNC_FLAG_OFFSET));
66         builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
67         builder.setOperational(flags.get(OPERATIONAL_FLAG_OFFSET));
68         final TlvsBuilder b = new TlvsBuilder();
69         parseTlvs(b, bytes.slice());
70         builder.setTlvs(b.build());
71         return builder.build();
72     }
73
74     @Override
75     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
76         if (tlv instanceof RsvpErrorSpec) {
77             builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
78         } else if (tlv instanceof SymbolicPathName) {
79             builder.setSymbolicPathName((SymbolicPathName) tlv);
80         } else if (tlv instanceof LspDbVersion) {
81             builder.setLspDbVersion((LspDbVersion) tlv);
82         }
83     }
84
85     @Override
86     public void serializeObject(final Object object, final ByteBuf buffer) {
87         Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s. Needed LspObject.", object.getClass());
88         final Lsp specObj = (Lsp) object;
89         final ByteBuf body = Unpooled.buffer();
90         final PlspId plsp = specObj.getPlspId();
91         Preconditions.checkArgument(plsp != null, "PLSP-ID not present");
92         body.writeMedium(plsp.getValue().intValue() << 4);
93
94         BitSet flags = new BitSet(2 * Byte.SIZE);
95         if (specObj.isDelegate() != null && specObj.isDelegate()) {
96             flags.set(DELEGATE_FLAG_OFFSET);
97         }
98         if (specObj.isRemove() != null && specObj.isRemove()) {
99             flags.set(REMOVE_FLAG_OFFSET);
100         }
101         if (specObj.isSync() != null && specObj.isSync()) {
102             flags.set(SYNC_FLAG_OFFSET);
103         }
104         if (specObj.isOperational() != null && specObj.isOperational()) {
105             flags.set(OPERATIONAL_FLAG_OFFSET);
106         }
107         body.writeByte(ByteArray.bitSetToBytes(flags, 2)[1]);
108         //FIXME: switch to ByteBuf
109         final byte[] tlvs = serializeTlvs(specObj.getTlvs());
110         body.writeBytes(tlvs);
111         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
112     }
113
114     public byte[] serializeTlvs(final Tlvs tlvs) {
115         if (tlvs == null) {
116             return new byte[0];
117         }
118         int finalLength = 0;
119         byte[] rsvpErrBytes = null;
120         byte[] symbBytes = null;
121         byte[] dbvBytes = null;
122         if (tlvs.getRsvpErrorSpec() != null) {
123             rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
124             finalLength += rsvpErrBytes.length;
125         }
126         if (tlvs.getSymbolicPathName() != null) {
127             symbBytes = serializeTlv(tlvs.getSymbolicPathName());
128             finalLength += symbBytes.length;
129         }
130         if (tlvs.getLspDbVersion() != null) {
131             dbvBytes = serializeTlv(tlvs.getLspDbVersion());
132             finalLength += dbvBytes.length;
133         }
134         int offset = 0;
135         final byte[] result = new byte[finalLength];
136         if (rsvpErrBytes != null) {
137             ByteArray.copyWhole(rsvpErrBytes, result, offset);
138             offset += rsvpErrBytes.length;
139         }
140         if (symbBytes != null) {
141             ByteArray.copyWhole(symbBytes, result, offset);
142             offset += symbBytes.length;
143         }
144         if (dbvBytes != null) {
145             ByteArray.copyWhole(dbvBytes, result, offset);
146             offset += dbvBytes.length;
147         }
148         return result;
149     }
150 }