Bump to odlparent-3.0.2 and yangtools-2.0.0
[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 ProtectionCommonParser() {
40
41     }
42
43     protected static void serializeBodyType1(final ProtectionSubobject protObj, final ByteBuf output) {
44         final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
45         flagBitArray.set(SECONDARY, protObj.isSecondary());
46         flagBitArray.toByteBuf(output);
47         output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
48         output.writeByte(protObj.getLinkFlags().getIntValue());
49     }
50
51     protected static void serializeBodyType2(final ProtectionSubobject protObj, final ByteBuf output) {
52         final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
53         flagBitArray.set(SECONDARY, protObj.isSecondary());
54         flagBitArray.set(PROTECTING, protObj.isProtecting());
55         flagBitArray.set(NOTIFICATION, protObj.isNotification());
56         flagBitArray.set(OPERATIONAL, protObj.isOperational());
57         flagBitArray.toByteBuf(output);
58         output.writeByte(protObj.getLspFlag().getIntValue());
59         output.writeZero(BYTE_SIZE);
60         output.writeByte(protObj.getLinkFlags().getIntValue());
61         final BitArray flagInPlaceBitArray = new BitArray(FLAGS_SIZE);
62         flagInPlaceBitArray.set(IN_PLACE, protObj.isInPlace());
63         flagInPlaceBitArray.set(REQUIRED, protObj.isRequired());
64         flagInPlaceBitArray.toByteBuf(output);
65         output.writeByte(protObj.getSegFlag().getIntValue());
66         output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
67     }
68
69     protected static ProtectionSubobject parseCommonProtectionBodyType2(final ByteBuf byteBuf) throws
70         RSVPParsingException {
71         if (byteBuf.readableBytes() != CONTENT_LENGTH_C2) {
72             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + byteBuf.readableBytes() + "; "
73                 + "Expected: " + CONTENT_LENGTH_C2 + ".");
74         }
75         final ProtectionSubobjectBuilder sub = new ProtectionSubobjectBuilder();
76         final BitArray protectionFlag = BitArray.valueOf(byteBuf.readByte());
77         sub.setSecondary(protectionFlag.get(SECONDARY));
78         sub.setProtecting(protectionFlag.get(PROTECTING));
79         sub.setNotification(protectionFlag.get(NOTIFICATION));
80         sub.setOperational(protectionFlag.get(OPERATIONAL));
81
82         final int lspFlags = byteBuf.readByte();
83         sub.setLspFlag(LspFlag.forValue(lspFlags)).build();
84         //Skip Reserved
85         byteBuf.skipBytes(ByteBufWriteUtil.ONE_BYTE_LENGTH);
86         final int linkFlags = byteBuf.readByte();
87         sub.setLinkFlags(LinkFlags.forValue(linkFlags));
88
89         final BitArray bitArray2 = BitArray.valueOf(byteBuf.readByte());
90         sub.setInPlace(bitArray2.get(IN_PLACE));
91         sub.setRequired(bitArray2.get(REQUIRED));
92
93         final int segFlags = byteBuf.readByte();
94         sub.setSegFlag(LspFlag.forValue(segFlags));
95         byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
96         return sub.build();
97     }
98
99     protected static ProtectionSubobject parseCommonProtectionBodyType1(final ByteBuf byteBuf) {
100         final BitArray bitArray = BitArray.valueOf(byteBuf.readByte());
101         final ProtectionSubobjectBuilder sub = new ProtectionSubobjectBuilder();
102         sub.setSecondary(bitArray.get(SECONDARY));
103         //Skip Reserved
104         byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
105         final int linkFlags = byteBuf.readByte();
106         sub.setLinkFlags(LinkFlags.forValue(linkFlags));
107         return sub.build();
108     }
109
110     protected static void serializeBody(final Short ctype, final ProtectionSubobject protObj,
111         final ByteBuf output) {
112         output.writeZero(BYTE_SIZE);
113         writeUnsignedByte(ctype, output);
114         switch (ctype) {
115             case PROTECTION_SUBOBJECT_TYPE_1:
116                 serializeBodyType1(protObj, output);
117                 break;
118             case PROTECTION_SUBOBJECT_TYPE_2:
119                 serializeBodyType2(protObj, output);
120                 break;
121             default:
122                 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
123                 break;
124         }
125     }
126
127     protected static ProtectionSubobject parseCommonProtectionBody(final short ctype, final ByteBuf byteBuf)
128         throws RSVPParsingException {
129         ProtectionSubobject protectionSubobject = null;
130         switch (ctype) {
131             case PROTECTION_SUBOBJECT_TYPE_1:
132                 protectionSubobject = parseCommonProtectionBodyType1(byteBuf);
133                 break;
134             case PROTECTION_SUBOBJECT_TYPE_2:
135                 protectionSubobject = parseCommonProtectionBodyType2(byteBuf);
136                 break;
137             default:
138                 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
139                 break;
140         }
141         return protectionSubobject;
142     }
143 }