2 * Copyright (c) 2013 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
9 package org.opendaylight.protocol.pcep.impl.object;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedBytes;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.BitSet;
19 import java.util.List;
20 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
21 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
24 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.RpBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.Tlvs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
39 * Parser for {@link Rp}
41 public class PCEPRequestParameterObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
43 public static final int CLASS = 2;
45 public static final int TYPE = 1;
48 * lengths of fields in bytes
50 private static final int FLAGS_PRI_MF_LENGTH = 4;
53 * lengths of subfields inside multi-field in bits
55 private static final int FLAGS_SF_LENGTH = 29;
58 * offsets of subfields inside multi-field in bits
61 private static final int FLAGS_SF_OFFSET = 0;
62 private static final int PRI_SF_OFFSET = FLAGS_SF_OFFSET + FLAGS_SF_LENGTH;
65 * flags offsets inside flags sub-field in bits
68 private static final int O_FLAG_OFFSET = 26;
69 private static final int B_FLAG_OFFSET = 27;
70 private static final int R_FLAG_OFFSET = 28;
73 * GCO extension flags offsets inside flags sub-field in bits
75 private static final int M_FLAG_OFFSET = 21;
76 private static final int D_FLAG_OFFSET = 22;
79 * Path-key bit (RFC5520)
81 private static final int P_FLAG_OFFSET = 23;
83 * OF extension flags offsets inside flags sub.field in bits
85 private static final int S_FLAG_OFFSET = 24;
89 private static final int F_FLAG_OFFSET = 18;
91 private static final int N_FLAG_OFFSET = 19;
93 private static final int E_FLAG_OFFSET = 20;
95 public PCEPRequestParameterObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
96 super(tlvReg, viTlvReg);
100 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
101 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
102 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(bytes, FLAGS_PRI_MF_LENGTH));
104 final RpBuilder builder = new RpBuilder();
105 builder.setIgnore(header.isIgnore());
106 builder.setProcessingRule(header.isProcessingRule());
109 priority |= flags.get(PRI_SF_OFFSET + 2) ? 1 : 0;
110 priority |= (flags.get(PRI_SF_OFFSET + 1) ? 1 : 0) << 1;
111 priority |= (flags.get(PRI_SF_OFFSET) ? 1 : 0) << 2;
112 builder.setPriority(priority);
113 builder.setFragmentation(flags.get(F_FLAG_OFFSET));
114 builder.setP2mp(flags.get(N_FLAG_OFFSET));
115 builder.setEroCompression(flags.get(E_FLAG_OFFSET));
116 builder.setMakeBeforeBreak(flags.get(M_FLAG_OFFSET));
117 builder.setOrder(flags.get(D_FLAG_OFFSET));
118 builder.setPathKey(flags.get(P_FLAG_OFFSET));
119 builder.setSupplyOf(flags.get(S_FLAG_OFFSET));
120 builder.setLoose(flags.get(O_FLAG_OFFSET));
121 builder.setBiDirectional(flags.get(B_FLAG_OFFSET));
122 builder.setReoptimization(flags.get(R_FLAG_OFFSET));
124 builder.setRequestId(new RequestId(bytes.readUnsignedInt()));
125 final TlvsBuilder tlvsBuilder = new TlvsBuilder();
126 parseTlvs(tlvsBuilder, bytes.slice());
127 builder.setTlvs(tlvsBuilder.build());
128 return builder.build();
132 public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
133 if (tlv instanceof Order) {
134 builder.setOrder((Order) tlv);
136 if (tlv instanceof PathSetupType) {
137 builder.setPathSetupType((PathSetupType) tlv);
142 public void serializeObject(final Object object, final ByteBuf buffer) {
143 Preconditions.checkArgument(object instanceof Rp, "Wrong instance of PCEPObject. Passed %s. Needed RPObject.", object.getClass());
144 final ByteBuf body = Unpooled.buffer();
145 final Rp rpObj = (Rp) object;
146 final BitSet flags = new BitSet(FLAGS_PRI_MF_LENGTH * Byte.SIZE);
147 if (rpObj.isReoptimization() != null) {
148 flags.set(R_FLAG_OFFSET, rpObj.isReoptimization());
150 if (rpObj.isBiDirectional() != null) {
151 flags.set(B_FLAG_OFFSET, rpObj.isBiDirectional());
153 if (rpObj.isLoose() != null) {
154 flags.set(O_FLAG_OFFSET, rpObj.isLoose());
156 if (rpObj.isMakeBeforeBreak() != null) {
157 flags.set(M_FLAG_OFFSET, rpObj.isMakeBeforeBreak());
159 if (rpObj.isOrder() != null) {
160 flags.set(D_FLAG_OFFSET, rpObj.isOrder());
162 if (rpObj.isPathKey() != null) {
163 flags.set(P_FLAG_OFFSET, rpObj.isPathKey());
165 if (rpObj.isSupplyOf() != null) {
166 flags.set(S_FLAG_OFFSET, rpObj.isSupplyOf());
168 if (rpObj.isFragmentation() != null) {
169 flags.set(F_FLAG_OFFSET, rpObj.isFragmentation());
171 if (rpObj.isP2mp() != null) {
172 flags.set(N_FLAG_OFFSET, rpObj.isP2mp());
174 if (rpObj.isEroCompression() != null) {
175 flags.set(E_FLAG_OFFSET, rpObj.isEroCompression());
177 if (rpObj.getPriority() != null) {
178 final byte[] p = { 0, 0, 0, UnsignedBytes.checkedCast(rpObj.getPriority().shortValue())};
179 flags.or(ByteArray.bytesToBitSet(p));
181 writeBitSet(flags, FLAGS_PRI_MF_LENGTH, body);
182 Preconditions.checkArgument(rpObj.getRequestId() != null, "RequestId is mandatory");
183 writeUnsignedInt(rpObj.getRequestId().getValue(), body);
184 serializeTlvs(rpObj.getTlvs(), body);
185 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
188 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
192 if (tlvs.getOrder() != null) {
193 serializeTlv(tlvs.getOrder(), body);
195 if (tlvs.getPathSetupType() != null) {
196 serializeTlv(tlvs.getPathSetupType(), body);
198 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
202 protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
203 if (!tlvs.isEmpty()) {
204 builder.setVendorInformationTlv(tlvs);