Merge "Synchronized with snapshot releases again"
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07LspObjectParser.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 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.ietf.stateful.rev131222.OperationalStatus;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
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
31 import com.google.common.base.Preconditions;
32
33 /**
34  * Parser for {@link Lsp}
35  */
36 public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37
38         public static final int CLASS = 32;
39
40         public static final int TYPE = 1;
41
42         /*
43          * offset of TLVs offset of other fields are not defined as constants
44          * because of non-standard mapping of bits
45          */
46         protected static final int TLVS_OFFSET = 4;
47
48         /*
49          * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
50          */
51         protected static final int DELEGATE_FLAG_OFFSET = 15;
52         protected static final int SYNC_FLAG_OFFSET = 14;
53         protected static final int REMOVE_FLAG_OFFSET = 13;
54         protected static final int ADMINISTRATIVE_FLAG_OFFSET = 12;
55         protected static final int OPERATIONAL_OFFSET = 9;
56
57         public Stateful07LspObjectParser(final TlvHandlerRegistry tlvReg) {
58                 super(tlvReg);
59         }
60
61         @Override
62         public Lsp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
63                 if (bytes == null || bytes.length == 0) {
64                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
65                 }
66                 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
67
68                 final LspBuilder builder = new LspBuilder();
69                 builder.setIgnore(header.isIgnore());
70                 builder.setProcessingRule(header.isProcessingRule());
71
72                 builder.setPlspId(new PlspId((ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4));
73                 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
74                 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
75                 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
76                 builder.setAdministrative(flags.get(ADMINISTRATIVE_FLAG_OFFSET));
77                 short s = 0;
78                 s |= flags.get(OPERATIONAL_OFFSET + 2) ? 1 : 0;
79                 s |= (flags.get(OPERATIONAL_OFFSET + 1) ? 1 : 0) << 1;
80                 s |= (flags.get(OPERATIONAL_OFFSET) ? 1 : 0) << 2;
81                 builder.setOperational(OperationalStatus.forValue(s));
82                 final TlvsBuilder b = new TlvsBuilder();
83                 parseTlvs(b, ByteArray.cutBytes(bytes, TLVS_OFFSET));
84                 builder.setTlvs(b.build());
85                 return builder.build();
86         }
87
88         @Override
89         public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
90                 if (tlv instanceof LspErrorCode) {
91                         builder.setLspErrorCode((LspErrorCode) tlv);
92                 } else if (tlv instanceof LspIdentifiers) {
93                         builder.setLspIdentifiers((LspIdentifiers) tlv);
94                 } else if (tlv instanceof RsvpErrorSpec) {
95                         builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
96                 } else if (tlv instanceof SymbolicPathName) {
97                         builder.setSymbolicPathName((SymbolicPathName) tlv);
98                 }
99         }
100
101         @Override
102         public byte[] serializeObject(final Object object) {
103                 if (!(object instanceof Lsp)) {
104                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
105                 }
106                 final Lsp specObj = (Lsp) object;
107
108                 final byte[] tlvs = serializeTlvs(specObj.getTlvs());
109                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
110
111                 Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
112                 final int lspID = specObj.getPlspId().getValue().intValue();
113                 retBytes[0] = (byte) (lspID >> 12);
114                 retBytes[1] = (byte) (lspID >> 4);
115                 retBytes[2] = (byte) (lspID << 4);
116                 if (specObj.isDelegate() != null && specObj.isDelegate()) {
117                         retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
118                 }
119                 if (specObj.isRemove() != null && specObj.isRemove()) {
120                         retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
121                 }
122                 if (specObj.isSync() != null && specObj.isSync()) {
123                         retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
124                 }
125                 if (specObj.isAdministrative() != null && specObj.isAdministrative()) {
126                         retBytes[3] |= 1 << (Byte.SIZE - (ADMINISTRATIVE_FLAG_OFFSET - Byte.SIZE) - 1);
127                 }
128                 if (specObj.getOperational() != null) {
129                         final int op = specObj.getOperational().getIntValue();
130                         retBytes[3] |= (op & 7) << 4;
131                 }
132                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
133                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
134         }
135
136         public byte[] serializeTlvs(final Tlvs tlvs) {
137                 if (tlvs == null) {
138                         return new byte[0];
139                 }
140                 int finalLength = 0;
141                 byte[] lspErrBytes = null;
142                 byte[] lspIdBytes = null;
143                 byte[] rsvpErrBytes = null;
144                 byte[] symbBytes = null;
145                 if (tlvs.getLspErrorCode() != null) {
146                         lspErrBytes = serializeTlv(tlvs.getLspErrorCode());
147                         finalLength += lspErrBytes.length;
148                 }
149                 if (tlvs.getLspIdentifiers() != null) {
150                         lspIdBytes = serializeTlv(tlvs.getLspIdentifiers());
151                         finalLength += lspIdBytes.length;
152                 }
153                 if (tlvs.getRsvpErrorSpec() != null) {
154                         rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
155                         finalLength += rsvpErrBytes.length;
156                 }
157                 if (tlvs.getSymbolicPathName() != null) {
158                         symbBytes = serializeTlv(tlvs.getSymbolicPathName());
159                         finalLength += symbBytes.length;
160                 }
161                 int offset = 0;
162                 final byte[] result = new byte[finalLength];
163                 if (lspErrBytes != null) {
164                         ByteArray.copyWhole(lspErrBytes, result, offset);
165                         offset += lspErrBytes.length;
166                 }
167                 if (lspIdBytes != null) {
168                         ByteArray.copyWhole(lspIdBytes, result, offset);
169                         offset += lspIdBytes.length;
170                 }
171                 if (rsvpErrBytes != null) {
172                         ByteArray.copyWhole(rsvpErrBytes, result, offset);
173                         offset += rsvpErrBytes.length;
174                 }
175                 if (symbBytes != null) {
176                         ByteArray.copyWhole(symbBytes, result, offset);
177                         offset += symbBytes.length;
178                 }
179                 return result;
180         }
181
182         @Override
183         public int getObjectType() {
184                 return TYPE;
185         }
186
187         @Override
188         public int getObjectClass() {
189                 return CLASS;
190         }
191 }