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