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