Reduce ByteBuf.writeZero() usage
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / te / ProtectionCommonParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.te;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
12 import org.opendaylight.protocol.util.BitArray;
13 import org.opendaylight.protocol.util.ByteBufWriteUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LinkFlags;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspFlag;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.subobject.ProtectionSubobject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.subobject.ProtectionSubobjectBuilder;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class ProtectionCommonParser {
22
23     protected static final short PROTECTION_SUBOBJECT_TYPE_1 = 1;
24     protected static final short PROTECTION_SUBOBJECT_TYPE_2 = 2;
25     protected static final int CONTENT_LENGTH_C2 = 8;
26     private static final int SECONDARY = 0;
27     private static final int PROTECTING = 1;
28     private static final int NOTIFICATION = 2;
29     private static final int OPERATIONAL = 3;
30     private static final int IN_PLACE = 0;
31     private static final int REQUIRED = 1;
32     private static final int FLAGS_SIZE = 8;
33     private static final Logger LOG = LoggerFactory.getLogger(ProtectionCommonParser.class);
34
35     protected ProtectionCommonParser() {
36
37     }
38
39     protected static void serializeBodyType1(final ProtectionSubobject protObj, final ByteBuf output) {
40         final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
41         flagBitArray.set(SECONDARY, protObj.isSecondary());
42         flagBitArray.toByteBuf(output);
43         output.writeShort(0);
44         output.writeByte(protObj.getLinkFlags().getIntValue());
45     }
46
47     protected static void serializeBodyType2(final ProtectionSubobject protObj, final ByteBuf output) {
48         final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
49         flagBitArray.set(SECONDARY, protObj.isSecondary());
50         flagBitArray.set(PROTECTING, protObj.isProtecting());
51         flagBitArray.set(NOTIFICATION, protObj.isNotification());
52         flagBitArray.set(OPERATIONAL, protObj.isOperational());
53         flagBitArray.toByteBuf(output);
54         output.writeByte(protObj.getLspFlag().getIntValue());
55         output.writeByte(0);
56         output.writeByte(protObj.getLinkFlags().getIntValue());
57         final BitArray flagInPlaceBitArray = new BitArray(FLAGS_SIZE);
58         flagInPlaceBitArray.set(IN_PLACE, protObj.isInPlace());
59         flagInPlaceBitArray.set(REQUIRED, protObj.isRequired());
60         flagInPlaceBitArray.toByteBuf(output);
61         output.writeByte(protObj.getSegFlag().getIntValue());
62         output.writeShort(0);
63     }
64
65     protected static ProtectionSubobject parseCommonProtectionBodyType2(final ByteBuf byteBuf) throws
66         RSVPParsingException {
67         if (byteBuf.readableBytes() != CONTENT_LENGTH_C2) {
68             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + byteBuf.readableBytes() + "; "
69                 + "Expected: " + CONTENT_LENGTH_C2 + ".");
70         }
71         final ProtectionSubobjectBuilder sub = new ProtectionSubobjectBuilder();
72         final BitArray protectionFlag = BitArray.valueOf(byteBuf.readByte());
73         sub.setSecondary(protectionFlag.get(SECONDARY));
74         sub.setProtecting(protectionFlag.get(PROTECTING));
75         sub.setNotification(protectionFlag.get(NOTIFICATION));
76         sub.setOperational(protectionFlag.get(OPERATIONAL));
77
78         final int lspFlags = byteBuf.readByte();
79         sub.setLspFlag(LspFlag.forValue(lspFlags)).build();
80         //Skip Reserved
81         byteBuf.skipBytes(ByteBufWriteUtil.ONE_BYTE_LENGTH);
82         final int linkFlags = byteBuf.readByte();
83         sub.setLinkFlags(LinkFlags.forValue(linkFlags));
84
85         final BitArray bitArray2 = BitArray.valueOf(byteBuf.readByte());
86         sub.setInPlace(bitArray2.get(IN_PLACE));
87         sub.setRequired(bitArray2.get(REQUIRED));
88
89         final int segFlags = byteBuf.readByte();
90         sub.setSegFlag(LspFlag.forValue(segFlags));
91         byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
92         return sub.build();
93     }
94
95     protected static ProtectionSubobject parseCommonProtectionBodyType1(final ByteBuf byteBuf) {
96         final BitArray bitArray = BitArray.valueOf(byteBuf.readByte());
97         final ProtectionSubobjectBuilder sub = new ProtectionSubobjectBuilder();
98         sub.setSecondary(bitArray.get(SECONDARY));
99         //Skip Reserved
100         byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
101         final int linkFlags = byteBuf.readByte();
102         sub.setLinkFlags(LinkFlags.forValue(linkFlags));
103         return sub.build();
104     }
105
106     protected static void serializeBody(final short ctype, final ProtectionSubobject protObj,
107         final ByteBuf output) {
108         output.writeByte(0);
109         output.writeByte(ctype);
110         switch (ctype) {
111             case PROTECTION_SUBOBJECT_TYPE_1:
112                 serializeBodyType1(protObj, output);
113                 break;
114             case PROTECTION_SUBOBJECT_TYPE_2:
115                 serializeBodyType2(protObj, output);
116                 break;
117             default:
118                 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
119                 break;
120         }
121     }
122
123     protected static ProtectionSubobject parseCommonProtectionBody(final short ctype, final ByteBuf byteBuf)
124             throws RSVPParsingException {
125         switch (ctype) {
126             case PROTECTION_SUBOBJECT_TYPE_1:
127                 return parseCommonProtectionBodyType1(byteBuf);
128             case PROTECTION_SUBOBJECT_TYPE_2:
129                 return parseCommonProtectionBodyType2(byteBuf);
130             default:
131                 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
132                 return null;
133         }
134     }
135 }