f21ec395a8536650bc8509b0015231bae54a72e5
[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
9 package org.opendaylight.protocol.rsvp.parser.impl.te;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
15 import org.opendaylight.protocol.util.BitArray;
16 import org.opendaylight.protocol.util.ByteBufWriteUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LinkFlags;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspFlag;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.subobject.ProtectionSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.subobject.ProtectionSubobjectBuilder;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class ProtectionCommonParser {
25
26     protected static final int BYTE_SIZE = 1;
27     protected static final short PROTECTION_SUBOBJECT_TYPE_1 = 1;
28     protected static final short PROTECTION_SUBOBJECT_TYPE_2 = 2;
29     protected static final int CONTENT_LENGTH_C2 = 8;
30     private static final int SECONDARY = 0;
31     private static final int PROTECTING = 1;
32     private static final int NOTIFICATION = 2;
33     private static final int OPERATIONAL = 3;
34     private static final int IN_PLACE = 0;
35     private static final int REQUIRED = 1;
36     private static final int FLAGS_SIZE = 8;
37     private static final Logger LOG = LoggerFactory.getLogger(ProtectionCommonParser.class);
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.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
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.writeZero(BYTE_SIZE);
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.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
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.writeZero(BYTE_SIZE);
109         writeUnsignedByte(ctype, output);
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         ProtectionSubobject protectionSubobject = null;
126         switch (ctype) {
127             case PROTECTION_SUBOBJECT_TYPE_1:
128                 protectionSubobject = parseCommonProtectionBodyType1(byteBuf);
129                 break;
130             case PROTECTION_SUBOBJECT_TYPE_2:
131                 protectionSubobject = parseCommonProtectionBodyType2(byteBuf);
132                 break;
133             default:
134                 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
135                 break;
136         }
137         return protectionSubobject;
138     }
139 }