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