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.subobject;
10 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
11 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
22 import com.google.common.primitives.UnsignedInts;
25 * Parser for {@link UnnumberedCase}
27 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29 public static final int TYPE = 4;
31 private static final int ROUTER_ID_NUMBER_LENGTH = 4;
32 private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
34 private static final int ROUTER_ID_NUMBER_OFFSET = 2;
35 private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
37 private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
40 public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
41 if (buffer == null || buffer.length == 0) {
42 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
44 if (buffer.length != CONTENT_LENGTH) {
45 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
46 + CONTENT_LENGTH + ".");
48 final SubobjectsBuilder builder = new SubobjectsBuilder();
49 builder.setLoose(loose);
50 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
51 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
52 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
53 INTERFACE_ID_NUMBER_LENGTH))));
54 builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
55 return builder.build();
59 public byte[] serializeSubobject(final Subobjects subobject) {
60 if (!(subobject.getSubobjectType() instanceof UnnumberedCase)) {
61 throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
62 + ". Needed UnnumberedCase.");
65 retBytes = new byte[CONTENT_LENGTH];
66 final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
67 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
68 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
69 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
70 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
74 public int getType() {