2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.rsvp.parser.impl.te;
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;
21 public class ProtectionCommonParser {
23 protected static final int BYTE_SIZE = 1;
24 protected static final short PROTECTION_SUBOBJECT_TYPE_1 = 1;
25 protected static final short PROTECTION_SUBOBJECT_TYPE_2 = 2;
26 protected static final int CONTENT_LENGTH_C2 = 8;
27 private static final int SECONDARY = 0;
28 private static final int PROTECTING = 1;
29 private static final int NOTIFICATION = 2;
30 private static final int OPERATIONAL = 3;
31 private static final int IN_PLACE = 0;
32 private static final int REQUIRED = 1;
33 private static final int FLAGS_SIZE = 8;
34 private static final Logger LOG = LoggerFactory.getLogger(ProtectionCommonParser.class);
36 protected ProtectionCommonParser() {
40 protected static void serializeBodyType1(final ProtectionSubobject protObj, final ByteBuf output) {
41 final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
42 flagBitArray.set(SECONDARY, protObj.isSecondary());
43 flagBitArray.toByteBuf(output);
44 output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
45 output.writeByte(protObj.getLinkFlags().getIntValue());
48 protected static void serializeBodyType2(final ProtectionSubobject protObj, final ByteBuf output) {
49 final BitArray flagBitArray = new BitArray(FLAGS_SIZE);
50 flagBitArray.set(SECONDARY, protObj.isSecondary());
51 flagBitArray.set(PROTECTING, protObj.isProtecting());
52 flagBitArray.set(NOTIFICATION, protObj.isNotification());
53 flagBitArray.set(OPERATIONAL, protObj.isOperational());
54 flagBitArray.toByteBuf(output);
55 output.writeByte(protObj.getLspFlag().getIntValue());
56 output.writeZero(BYTE_SIZE);
57 output.writeByte(protObj.getLinkFlags().getIntValue());
58 final BitArray flagInPlaceBitArray = new BitArray(FLAGS_SIZE);
59 flagInPlaceBitArray.set(IN_PLACE, protObj.isInPlace());
60 flagInPlaceBitArray.set(REQUIRED, protObj.isRequired());
61 flagInPlaceBitArray.toByteBuf(output);
62 output.writeByte(protObj.getSegFlag().getIntValue());
63 output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
66 protected static ProtectionSubobject parseCommonProtectionBodyType2(final ByteBuf byteBuf) throws
67 RSVPParsingException {
68 if (byteBuf.readableBytes() != CONTENT_LENGTH_C2) {
69 throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + byteBuf.readableBytes() + "; "
70 + "Expected: " + CONTENT_LENGTH_C2 + ".");
72 final ProtectionSubobjectBuilder sub = new ProtectionSubobjectBuilder();
73 final BitArray protectionFlag = BitArray.valueOf(byteBuf.readByte());
74 sub.setSecondary(protectionFlag.get(SECONDARY));
75 sub.setProtecting(protectionFlag.get(PROTECTING));
76 sub.setNotification(protectionFlag.get(NOTIFICATION));
77 sub.setOperational(protectionFlag.get(OPERATIONAL));
79 final int lspFlags = byteBuf.readByte();
80 sub.setLspFlag(LspFlag.forValue(lspFlags)).build();
82 byteBuf.skipBytes(ByteBufWriteUtil.ONE_BYTE_LENGTH);
83 final int linkFlags = byteBuf.readByte();
84 sub.setLinkFlags(LinkFlags.forValue(linkFlags));
86 final BitArray bitArray2 = BitArray.valueOf(byteBuf.readByte());
87 sub.setInPlace(bitArray2.get(IN_PLACE));
88 sub.setRequired(bitArray2.get(REQUIRED));
90 final int segFlags = byteBuf.readByte();
91 sub.setSegFlag(LspFlag.forValue(segFlags));
92 byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
96 protected static ProtectionSubobject parseCommonProtectionBodyType1(final ByteBuf byteBuf) {
97 final BitArray bitArray = BitArray.valueOf(byteBuf.readByte());
98 final ProtectionSubobjectBuilder sub = new ProtectionSubobjectBuilder();
99 sub.setSecondary(bitArray.get(SECONDARY));
101 byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
102 final int linkFlags = byteBuf.readByte();
103 sub.setLinkFlags(LinkFlags.forValue(linkFlags));
107 protected static void serializeBody(final short ctype, final ProtectionSubobject protObj,
108 final ByteBuf output) {
109 output.writeZero(BYTE_SIZE);
110 output.writeByte(ctype);
112 case PROTECTION_SUBOBJECT_TYPE_1:
113 serializeBodyType1(protObj, output);
115 case PROTECTION_SUBOBJECT_TYPE_2:
116 serializeBodyType2(protObj, output);
119 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
124 protected static ProtectionSubobject parseCommonProtectionBody(final short ctype, final ByteBuf byteBuf)
125 throws RSVPParsingException {
127 case PROTECTION_SUBOBJECT_TYPE_1:
128 return parseCommonProtectionBodyType1(byteBuf);
129 case PROTECTION_SUBOBJECT_TYPE_2:
130 return parseCommonProtectionBodyType2(byteBuf);
132 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);