Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROAsNumberSubobjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.subobject;
9
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;
15
16 /**
17  * Parser for {@link org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject EROAsNumberSubobject}
18  */
19
20 public class EROAsNumberSubobjectParser {
21         public static final int AS_NUMBER_LENGTH = 2;
22
23         public static final int AS_NUMBER_OFFSET = 0;
24
25         public static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
26
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 + ".");
33
34                 return new EROAsNumberSubobject(new AsNumber((long) (ByteArray.bytesToShort(soContentsBytes) & 0xFFFF)), loose);
35         }
36
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.");
41
42                 final byte[] retBytes = new byte[CONTENT_LENGTH];
43
44                 System.arraycopy(ByteArray.longToBytes(((EROAsNumberSubobject) objToSerialize).getASNumber().getValue()), Long.SIZE / Byte.SIZE
45                                 - AS_NUMBER_LENGTH, retBytes, AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);
46
47                 return retBytes;
48         }
49 }