Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPP2MPEndPointsIPv4ObjectParser.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.object;
9
10 import org.opendaylight.protocol.util.ByteArray;
11 import org.opendaylight.protocol.concepts.IPv4;
12 import org.opendaylight.protocol.concepts.IPv4Address;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
15 import org.opendaylight.protocol.pcep.PCEPObject;
16 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
17 import org.opendaylight.protocol.pcep.impl.Util;
18 import org.opendaylight.protocol.pcep.object.PCEPP2MPEndPointsObject;
19 import com.google.common.primitives.UnsignedInts;
20
21 /**
22  * Parser for IPv4 {@link org.opendaylight.protocol.pcep.object.PCEPP2MPEndPointsObject
23  * PCEPP2MPEndPointsObject}
24  */
25 public class PCEPP2MPEndPointsIPv4ObjectParser implements PCEPObjectParser {
26
27     /*
28      * fields lengths and offsets for IPv4 in bytes
29      */
30     public static final int LEAF_TYPE_F_LENGTH = 4;
31     public static final int SRC4_F_LENGTH = 4;
32     public static final int DEST4_F_LENGTH = 4;
33
34     public static final int LEAF_TYPE_F_OFFSET = 0;
35     public static final int SRC4_F_OFFSET = LEAF_TYPE_F_OFFSET + LEAF_TYPE_F_LENGTH;
36     public static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
37
38     @Override
39     public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
40         if (bytes == null)
41             throw new IllegalArgumentException("Array of bytes is mandatory");
42         if (bytes.length < LEAF_TYPE_F_LENGTH + SRC4_F_LENGTH + DEST4_F_LENGTH)
43             throw new PCEPDeserializerException("Wrong length of array of bytes.");
44
45         final long leafType = UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(bytes, LEAF_TYPE_F_OFFSET, LEAF_TYPE_F_LENGTH)));
46
47         return new PCEPP2MPEndPointsObject<IPv4Address>(leafType, new IPv4Address(
48                 ByteArray.subByte(bytes, SRC4_F_OFFSET, SRC4_F_LENGTH)), Util.parseAddresses(bytes, DEST4_F_OFFSET, IPv4.FAMILY,
49                 DEST4_F_LENGTH), processed, ignored);
50     }
51
52     @Override
53     public byte[] put(PCEPObject obj) {
54         if (!(obj instanceof PCEPP2MPEndPointsObject))
55             throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPP2MPEndPointsObject.");
56
57         final PCEPP2MPEndPointsObject<?> ePObj = (PCEPP2MPEndPointsObject<?>) obj;
58
59         if (!(ePObj.getSourceAddress() instanceof IPv4Address))
60             throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + ePObj.getSourceAddress().getClass() + ". Needed IPv4Address");
61
62         final byte[] retBytes = new byte[LEAF_TYPE_F_LENGTH + SRC4_F_LENGTH + DEST4_F_LENGTH * ePObj.getDestinationAddresses().size()];
63         ByteArray.copyWhole(ByteArray.intToBytes((int) ePObj.getLeafType()), retBytes, LEAF_TYPE_F_OFFSET);
64         ByteArray.copyWhole(((IPv4Address) ePObj.getSourceAddress()).getAddress(), retBytes, SRC4_F_OFFSET);
65         Util.putAddresses(retBytes, DEST4_F_OFFSET, ePObj.getDestinationAddresses(), DEST4_F_LENGTH);
66         return retBytes;
67     }
68 }