BUG-47 : PCEP migration to generated DTOs.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROUnnumberedInterfaceSubobjectParser.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.ExcludeRouteSubobject;
12 import org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject;
13 import org.opendaylight.protocol.util.ByteArray;
14
15 /**
16  * Parser for {@link org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject
17  * XROUnnumberedInterfaceSubobject}
18  */
19 public class XROUnnumberedInterfaceSubobjectParser {
20         public static final int ATTRIBUTE_LENGTH = 1;
21         public static final int ROUTER_ID_NUMBER_LENGTH = 4;
22         public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
23
24         public static final int ATTRIBUTE_OFFSET = 1;// added reserved field of size 1
25         public static final int ROUTER_ID_NUMBER_OFFSET = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
26         public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
27
28         public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
29
30         public static XROUnnumberedInterfaceSubobject parse(final byte[] soContentsBytes, final boolean mandatory)
31                         throws PCEPDeserializerException {
32                 if (soContentsBytes == null || soContentsBytes.length == 0)
33                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
34                 if (soContentsBytes.length != CONTENT_LENGTH)
35                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: "
36                                         + CONTENT_LENGTH + ".");
37
38                 // return new XROUnnumberedInterfaceSubobject(new IPv4Address(
39                 // ByteArray.subByte(soContentsBytes, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)), new
40                 // UnnumberedInterfaceIdentifier(
41                 // UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(soContentsBytes, INTERFACE_ID_NUMBER_OFFSET,
42                 // INTERFACE_ID_NUMBER_LENGTH)))),
43                 // mandatory, XROSubobjectAttributeMapping.getInstance().getFromAttributeIdentifier((short)
44                 // (soContentsBytes[ATTRIBUTE_OFFSET] & 0xFF)));
45                 return null;
46         }
47
48         public static byte[] put(final ExcludeRouteSubobject objToSerialize) {
49                 if (!(objToSerialize instanceof XROUnnumberedInterfaceSubobject))
50                         throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + objToSerialize.getClass()
51                                         + ". Needed XROUnnumberedInterfaceSubobject.");
52
53                 byte[] retBytes;
54                 retBytes = new byte[CONTENT_LENGTH];
55                 final XROUnnumberedInterfaceSubobject specObj = (XROUnnumberedInterfaceSubobject) objToSerialize;
56
57                 retBytes[ATTRIBUTE_OFFSET] = (byte) XROSubobjectAttributeMapping.getInstance().getFromAttributeEnum(specObj.getAttribute());
58                 ByteArray.copyWhole(specObj.getRouterID().getAddress(), retBytes, ROUTER_ID_NUMBER_OFFSET);
59                 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceID().getInterfaceId()), Long.SIZE / Byte.SIZE
60                                 - INTERFACE_ID_NUMBER_LENGTH, retBytes, INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
61
62                 return retBytes;
63         }
64 }