Removed duplicated code from stateful02. Extended existing parsers.
[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 java.util.BitSet;
11
12 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.PlspId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.LspBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.TlvsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
28
29 /**
30  * Parser for {@link Lsp}
31  */
32 public final class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
33
34         public static final int CLASS = 32;
35
36         public static final int TYPE = 1;
37
38         /*
39          * offset of TLVs offset of other fields are not defined as constants
40          * because of non-standard mapping of bits
41          */
42         private static final int TLVS_OFFSET = 4;
43
44         /*
45          * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
46          */
47         private static final int DELEGATE_FLAG_OFFSET = 15;
48         private static final int SYNC_FLAG_OFFSET = 14;
49         private static final int REMOVE_FLAG_OFFSET = 12;
50         private static final int OPERATIONAL_FLAG_OFFSET = 13;
51
52         public Stateful02LspObjectParser(final TlvHandlerRegistry tlvReg) {
53                 super(tlvReg);
54         }
55
56         @Override
57         public Lsp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
58                 if (bytes == null || bytes.length == 0) {
59                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
60                 }
61                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
62
63                 final LspBuilder builder = new LspBuilder();
64                 builder.setIgnore(header.isIgnore());
65                 builder.setProcessingRule(header.isProcessingRule());
66
67                 builder.setPlspId(new PlspId((ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4));
68                 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
69                 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
70                 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
71                 builder.setOperational(flags.get(OPERATIONAL_FLAG_OFFSET));
72                 final TlvsBuilder b = new TlvsBuilder();
73                 parseTlvs(b, ByteArray.cutBytes(bytes, TLVS_OFFSET));
74                 builder.setTlvs(b.build());
75                 return builder.build();
76         }
77
78         @Override
79         public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
80                 if (tlv instanceof RsvpErrorSpec) {
81                         builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
82                 } else if (tlv instanceof SymbolicPathName) {
83                         builder.setSymbolicPathName((SymbolicPathName) tlv);
84                 } else if (tlv instanceof LspDbVersion) {
85                         builder.setLspDbVersion((LspDbVersion) tlv);
86                 }
87         }
88
89         @Override
90         public byte[] serializeObject(final Object object) {
91                 if (!(object instanceof Lsp)) {
92                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
93                 }
94                 final Lsp specObj = (Lsp) object;
95
96                 final byte[] tlvs = serializeTlvs(specObj.getTlvs());
97                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
98
99                 final int lspID = specObj.getPlspId().getValue().intValue();
100                 retBytes[0] = (byte) (lspID >> 12);
101                 retBytes[1] = (byte) (lspID >> 4);
102                 retBytes[2] = (byte) (lspID << 4);
103                 if (specObj.isDelegate() != null && specObj.isDelegate()) {
104                         retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
105                 }
106                 if (specObj.isRemove() != null && specObj.isRemove()) {
107                         retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
108                 }
109                 if (specObj.isSync() != null && specObj.isSync()) {
110                         retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
111                 }
112                 if (specObj.isOperational() != null && specObj.isOperational()) {
113                         retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
114                 }
115                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
116                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
117         }
118
119         public byte[] serializeTlvs(final Tlvs tlvs) {
120                 if (tlvs == null) {
121                         return new byte[0];
122                 }
123                 int finalLength = 0;
124                 byte[] rsvpErrBytes = null;
125                 byte[] symbBytes = null;
126                 byte[] dbvBytes = null;
127                 if (tlvs.getRsvpErrorSpec() != null) {
128                         rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
129                         finalLength += rsvpErrBytes.length;
130                 }
131                 if (tlvs.getSymbolicPathName() != null) {
132                         symbBytes = serializeTlv(tlvs.getSymbolicPathName());
133                         finalLength += symbBytes.length;
134                 }
135                 if (tlvs.getLspDbVersion() != null) {
136                         dbvBytes = serializeTlv(tlvs.getLspDbVersion());
137                         finalLength += dbvBytes.length;
138                 }
139                 int offset = 0;
140                 final byte[] result = new byte[finalLength];
141                 if (rsvpErrBytes != null) {
142                         ByteArray.copyWhole(rsvpErrBytes, result, offset);
143                         offset += rsvpErrBytes.length;
144                 }
145                 if (symbBytes != null) {
146                         ByteArray.copyWhole(symbBytes, result, offset);
147                         offset += symbBytes.length;
148                 }
149                 if (dbvBytes != null) {
150                         ByteArray.copyWhole(dbvBytes, result, offset);
151                         offset += dbvBytes.length;
152                 }
153                 return result;
154         }
155
156         @Override
157         public int getObjectType() {
158                 return TYPE;
159         }
160
161         @Override
162         public int getObjectClass() {
163                 return CLASS;
164         }
165 }