Bug-731: Fixed few major Sonar warnings
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / initiated00 / CInitiated00LspObjectParser.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.initiated00;
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 org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07LspObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
18 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Lsp1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Lsp1Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder;
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
30 /**
31  * Parser for {@link Lsp}
32  */
33 public class CInitiated00LspObjectParser extends Stateful07LspObjectParser {
34
35     private static final int CREATE_FLAG_OFFSET = 8;
36
37     public CInitiated00LspObjectParser(TlvRegistry tlvReg, VendorInformationTlvRegistry viTlvReg) {
38         super(tlvReg, viTlvReg);
39     }
40
41     @Override
42     public Lsp parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
43         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44         final LspBuilder builder = new LspBuilder();
45         builder.setIgnore(header.isIgnore());
46         builder.setProcessingRule(header.isProcessingRule());
47         int[] plspIdRaw = new int[] { bytes.readUnsignedByte(), bytes.readUnsignedByte(), bytes.getUnsignedByte(2), };
48         builder.setPlspId(new PlspId((long) ((plspIdRaw[0] << TWELVE_BITS_SHIFT) | (plspIdRaw[1] << FOUR_BITS_SHIFT) | (plspIdRaw[2] >> FOUR_BITS_SHIFT))));
49         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, 2));
50         builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
51         builder.setSync(flags.get(SYNC_FLAG_OFFSET));
52         builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
53         builder.setAdministrative(flags.get(ADMINISTRATIVE_FLAG_OFFSET));
54         builder.addAugmentation(Lsp1.class, new Lsp1Builder().setCreate(flags.get(CREATE_FLAG_OFFSET)).build());
55         short s = 0;
56         s |= flags.get(OPERATIONAL_OFFSET + 2) ? 1 : 0;
57         s |= (flags.get(OPERATIONAL_OFFSET + 1) ? 1 : 0) << 1;
58         s |= (flags.get(OPERATIONAL_OFFSET) ? 1 : 0) << 2;
59         builder.setOperational(OperationalStatus.forValue(s));
60         final TlvsBuilder b = new TlvsBuilder();
61         parseTlvs(b, bytes.slice());
62         builder.setTlvs(b.build());
63         return builder.build();
64     }
65
66     @Override
67     public void serializeObject(final Object object, final ByteBuf buffer) {
68         Preconditions.checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s. Needed LspObject.", object.getClass());
69         final Lsp specObj = (Lsp) object;
70         final ByteBuf body = Unpooled.buffer();
71
72         final byte[] retBytes = new byte[BODY_LENGTH];
73
74         Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
75         final int lspID = specObj.getPlspId().getValue().intValue();
76         retBytes[0] = (byte) (lspID >> TWELVE_BITS_SHIFT);
77         retBytes[1] = (byte) (lspID >> FOUR_BITS_SHIFT);
78         retBytes[2] = (byte) (lspID << FOUR_BITS_SHIFT);
79         if (specObj.isDelegate() != null && specObj.isDelegate()) {
80             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
81         }
82         if (specObj.isRemove() != null && specObj.isRemove()) {
83             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
84         }
85         if (specObj.isSync() != null && specObj.isSync()) {
86             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
87         }
88         if (specObj.isAdministrative() != null && specObj.isAdministrative()) {
89             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (ADMINISTRATIVE_FLAG_OFFSET - Byte.SIZE) - 1);
90         }
91         if (specObj.getAugmentation(Lsp1.class) != null && specObj.getAugmentation(Lsp1.class).isCreate()) {
92             retBytes[FLAGS_INDEX] |= 1 << (Byte.SIZE - (CREATE_FLAG_OFFSET - Byte.SIZE) - 1);
93         }
94         if (specObj.getOperational() != null) {
95             final int op = specObj.getOperational().getIntValue();
96             retBytes[FLAGS_INDEX] |= (op & OP_VALUE_BITS_OFFSET) << FOUR_BITS_SHIFT;
97         }
98         body.writeBytes(retBytes);
99         serializeTlvs(specObj.getTlvs(), body);
100         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
101     }
102 }