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
8 package org.opendaylight.protocol.pcep.impl.object;
10 import java.util.BitSet;
12 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.Tlvs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.TlvsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVector;
26 import com.google.common.primitives.UnsignedBytes;
29 * Parser for {@link NoPath}
31 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<NoPathBuilder> {
33 public static final int CLASS = 3;
35 public static final int TYPE = 1;
38 * lengths of fields in bytes
40 private static final int NI_F_LENGTH = 1;
41 private static final int FLAGS_F_LENGTH = 2;
42 private static final int RESERVED_F_LENGTH = 1;
45 * offsets of field in bytes
47 private static final int NI_F_OFFSET = 0;
48 private static final int FLAGS_F_OFFSET = NI_F_OFFSET + NI_F_LENGTH;
49 private static final int RESERVED_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
50 private static final int TLVS_OFFSET = RESERVED_F_OFFSET + RESERVED_F_LENGTH;
55 private static final int C_FLAG_OFFSET = 0;
57 public PCEPNoPathObjectParser(final TlvHandlerRegistry tlvReg) {
62 public NoPath parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
63 if (bytes == null || bytes.length == 0) {
64 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
66 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
68 final NoPathBuilder builder = new NoPathBuilder();
69 builder.setIgnore(header.isIgnore());
70 builder.setProcessingRule(header.isProcessingRule());
72 builder.setNatureOfIssue((short) UnsignedBytes.toInt(bytes[NI_F_OFFSET]));
73 builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
74 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
75 return builder.build();
79 public void addTlv(final NoPathBuilder builder, final Tlv tlv) {
80 if (tlv instanceof NoPathVector) {
81 builder.setTlvs(new TlvsBuilder().setNoPathVector((NoPathVector) tlv).build());
86 public byte[] serializeObject(final Object object) {
87 if (!(object instanceof NoPath)) {
88 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed NoPathObject.");
90 final NoPath nPObj = (NoPath) object;
92 final byte[] tlvs = serializeTlvs(nPObj.getTlvs());
93 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
95 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
97 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
98 flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
99 retBytes[NI_F_OFFSET] = UnsignedBytes.checkedCast(nPObj.getNatureOfIssue());
100 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
101 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
102 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
105 public byte[] serializeTlvs(final Tlvs tlvs) {
108 } else if (tlvs.getNoPathVector() != null) {
109 return serializeTlv(tlvs.getNoPathVector());
115 public int getObjectType() {
120 public int getObjectClass() {