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 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);
35 protected ProtectionCommonParser() {
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);
44 output.writeByte(protObj.getLinkFlags().getIntValue());
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());
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());
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 + ".");
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));
78 final int lspFlags = byteBuf.readByte();
79 sub.setLspFlag(LspFlag.forValue(lspFlags)).build();
81 byteBuf.skipBytes(ByteBufWriteUtil.ONE_BYTE_LENGTH);
82 final int linkFlags = byteBuf.readByte();
83 sub.setLinkFlags(LinkFlags.forValue(linkFlags));
85 final BitArray bitArray2 = BitArray.valueOf(byteBuf.readByte());
86 sub.setInPlace(bitArray2.get(IN_PLACE));
87 sub.setRequired(bitArray2.get(REQUIRED));
89 final int segFlags = byteBuf.readByte();
90 sub.setSegFlag(LspFlag.forValue(segFlags));
91 byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
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));
100 byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
101 final int linkFlags = byteBuf.readByte();
102 sub.setLinkFlags(LinkFlags.forValue(linkFlags));
106 protected static void serializeBody(final short ctype, final ProtectionSubobject protObj,
107 final ByteBuf output) {
109 output.writeByte(ctype);
111 case PROTECTION_SUBOBJECT_TYPE_1:
112 serializeBodyType1(protObj, output);
114 case PROTECTION_SUBOBJECT_TYPE_2:
115 serializeBodyType2(protObj, output);
118 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);
123 protected static ProtectionSubobject parseCommonProtectionBody(final short ctype, final ByteBuf byteBuf)
124 throws RSVPParsingException {
126 case PROTECTION_SUBOBJECT_TYPE_1:
127 return parseCommonProtectionBodyType1(byteBuf);
128 case PROTECTION_SUBOBJECT_TYPE_2:
129 return parseCommonProtectionBodyType2(byteBuf);
131 LOG.warn("Secondary Record Route Protection Subobject cType {} not supported", ctype);