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 org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder;
19 import com.google.common.primitives.UnsignedBytes;
22 * Parser for {@link Xro}
24 public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobjectsParser {
26 public static final int CLASS = 7;
28 public static final int TYPE = 1;
30 private static final int FLAGS_OFFSET = 3;
32 public PCEPExcludeRouteObjectParser(final XROSubobjectHandlerRegistry registry) {
37 public Xro parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
38 if (bytes == null || bytes.length == 0) {
39 throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
41 final XroBuilder builder = new XroBuilder();
42 builder.setIgnore(header.isIgnore());
43 builder.setProcessingRule(header.isProcessingRule());
44 builder.setFlags(new Flags(UnsignedBytes.toInt(bytes[FLAGS_OFFSET]) != 0));
45 builder.setSubobjects(parseSubobjects(ByteArray.cutBytes(bytes, FLAGS_OFFSET + 1)));
46 return builder.build();
50 public byte[] serializeObject(final Object object) {
51 if (!(object instanceof Xro)) {
52 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ExcludeRouteObject.");
54 final Xro obj = (Xro) object;
55 assert !(obj.getSubobjects().isEmpty()) : "Empty Excluded Route Object.";
56 final byte[] bytes = serializeSubobject(obj.getSubobjects());
57 final byte[] result = new byte[FLAGS_OFFSET + 1 + bytes.length];
58 if (obj.getFlags().isFail()) {
59 result[FLAGS_OFFSET] = 1;
61 ByteArray.copyWhole(bytes, result, FLAGS_OFFSET + 1);
62 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), result);
66 public int getObjectType() {
71 public int getObjectClass() {