Bug-731: Fixed few major Sonar warnings
[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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.BitSet;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
19 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
36
37 /**
38  * Parser for {@link Lsp}
39  */
40 public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
41
42     public static final int CLASS = 32;
43
44     public static final int TYPE = 1;
45
46     /*
47      * offset of TLVs offset of other fields are not defined as constants
48      * because of non-standard mapping of bits
49      */
50     protected static final int TLVS_OFFSET = 4;
51
52     /*
53      * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
54      */
55     protected static final int DELEGATE_FLAG_OFFSET = 15;
56     protected static final int SYNC_FLAG_OFFSET = 14;
57     protected static final int REMOVE_FLAG_OFFSET = 13;
58     protected static final int ADMINISTRATIVE_FLAG_OFFSET = 12;
59     protected static final int OPERATIONAL_OFFSET = 9;
60
61     protected static final int FOUR_BITS_SHIFT = 4;
62     protected static final int TWELVE_BITS_SHIFT = 12;
63     protected static final int BODY_LENGTH = 4;
64     protected static final int FLAGS_INDEX = 3;
65     protected static final int OP_VALUE_BITS_OFFSET = 7;
66
67     public Stateful07LspObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
68         super(tlvReg, viTlvReg);
69     }
70
71     @Override
72     public Lsp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
73         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
74         final LspBuilder builder = new LspBuilder();
75         builder.setIgnore(header.isIgnore());
76         builder.setProcessingRule(header.isProcessingRule());
77         int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), };
78         builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << TWELVE_BITS_SHIFT) | (plspIdRaw[1] << FOUR_BITS_SHIFT) | (plspIdRaw[2] >> FOUR_BITS_SHIFT))));
79         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, 2));
80         builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
81         builder.setSync(flags.get(SYNC_FLAG_OFFSET));
82         builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
83         builder.setAdministrative(flags.get(ADMINISTRATIVE_FLAG_OFFSET));
84         short s = 0;
85         s |= flags.get(OPERATIONAL_OFFSET + 2) ? 1 : 0;
86         s |= (flags.get(OPERATIONAL_OFFSET + 1) ? 1 : 0) << 1;
87         s |= (flags.get(OPERATIONAL_OFFSET) ? 1 : 0) << 2;
88         builder.setOperational(OperationalStatus.forValue(s));
89         final TlvsBuilder b = new TlvsBuilder();
90         parseTlvs(b, bytes.slice());
91         builder.setTlvs(b.build());
92         return builder.build();
93     }
94
95     @Override
96     public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
97         if (tlv instanceof LspErrorCode) {
98             builder.setLspErrorCode((LspErrorCode) tlv);
99         } else if (tlv instanceof LspIdentifiers) {
100             builder.setLspIdentifiers((LspIdentifiers) tlv);
101         } else if (tlv instanceof RsvpErrorSpec) {
102             builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
103         } else if (tlv instanceof SymbolicPathName) {
104             builder.setSymbolicPathName((SymbolicPathName) tlv);
105         } else if (tlv instanceof VsTlv) {
106             builder.setVsTlv((VsTlv) tlv);
107         }
108     }
109
110     @Override
111     public void serializeObject(final Object object, final ByteBuf buffer) {
112         Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s . Needed LspObject.", object.getClass());
113         final Lsp specObj = (Lsp) object;
114         final ByteBuf body = Unpooled.buffer();
115
116         final byte[] retBytes = new byte[BODY_LENGTH];
117
118         Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
119         final int lspID = specObj.getPlspId().getValue().intValue();
120         retBytes[0] = (byte) (lspID >> TWELVE_BITS_SHIFT);
121         retBytes[1] = (byte) (lspID >> FOUR_BITS_SHIFT);
122         retBytes[2] = (byte) (lspID << FOUR_BITS_SHIFT);
123         if (specObj.isDelegate() != null && specObj.isDelegate()) {
124             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
125         }
126         if (specObj.isRemove() != null && specObj.isRemove()) {
127             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
128         }
129         if (specObj.isSync() != null && specObj.isSync()) {
130             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
131         }
132         if (specObj.isAdministrative() != null && specObj.isAdministrative()) {
133             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (ADMINISTRATIVE_FLAG_OFFSET - Byte.SIZE) - 1);
134         }
135         if (specObj.getOperational() != null) {
136             final int op = specObj.getOperational().getIntValue();
137             retBytes[FLAGS_INDEX] |= (op & OP_VALUE_BITS_OFFSET) << FOUR_BITS_SHIFT;
138         }
139         body.writeBytes(retBytes);
140         serializeTlvs(specObj.getTlvs(), body);
141         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
142     }
143
144     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
145         if (tlvs == null) {
146             return;
147         }
148         if (tlvs.getLspErrorCode() != null) {
149             serializeTlv(tlvs.getLspErrorCode(), body);
150         }
151         if (tlvs.getLspIdentifiers() != null) {
152             serializeTlv(tlvs.getLspIdentifiers(), body);
153         }
154         if (tlvs.getRsvpErrorSpec() != null) {
155             serializeTlv(tlvs.getRsvpErrorSpec(), body);
156         }
157         if (tlvs.getSymbolicPathName() != null) {
158             serializeTlv(tlvs.getSymbolicPathName(), body);
159         }
160         if (tlvs.getVsTlv() != null) {
161             serializeTlv(tlvs.getVsTlv(), body);
162         }
163         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
164     }
165
166     @Override
167     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
168         if (!tlvs.isEmpty()) {
169             builder.setVendorInformationTlv(tlvs);
170         }
171     }
172 }