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.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject;
12 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
17 * Parser for {@link org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject EROAsNumberSubobject}
20 public class EROAsNumberSubobjectParser {
21 public static final int AS_NUMBER_LENGTH = 2;
23 public static final int AS_NUMBER_OFFSET = 0;
25 public static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
27 public static EROAsNumberSubobject parse(final byte[] soContentsBytes, final boolean loose) throws PCEPDeserializerException {
28 if (soContentsBytes == null || soContentsBytes.length == 0)
29 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
30 if (soContentsBytes.length != CONTENT_LENGTH)
31 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: "
32 + CONTENT_LENGTH + ".");
34 return new EROAsNumberSubobject(new AsNumber((long) (ByteArray.bytesToShort(soContentsBytes) & 0xFFFF)), loose);
37 public static byte[] put(final ExplicitRouteSubobject objToSerialize) {
38 if (!(objToSerialize instanceof EROAsNumberSubobject))
39 throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + objToSerialize.getClass()
40 + ". Needed EROAsNumberSubobject.");
42 final byte[] retBytes = new byte[CONTENT_LENGTH];
44 System.arraycopy(ByteArray.longToBytes(((EROAsNumberSubobject) objToSerialize).getASNumber().getValue()), Long.SIZE / Byte.SIZE
45 - AS_NUMBER_LENGTH, retBytes, AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);