Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPEndPointsIPv6ObjectParser.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.IPv6Address;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
13 import org.opendaylight.protocol.pcep.PCEPErrors;
14 import org.opendaylight.protocol.pcep.PCEPObject;
15 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
16 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
17 import org.opendaylight.protocol.util.ByteArray;
18
19 /**
20  * Parser for IPv6 {@link org.opendaylight.protocol.pcep.object.PCEPEndPointsObject
21  * PCEPEndPointsObject}
22  */
23 public class PCEPEndPointsIPv6ObjectParser implements PCEPObjectParser {
24
25         public static final int SRC6_F_LENGTH = 16;
26         public static final int DEST6_F_LENGT = 16;
27
28         public static final int SRC6_F_OFFSET = 0;
29         public static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
30
31         @Override
32         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
33                 if (bytes == null)
34                         throw new IllegalArgumentException("Array of bytes is mandatory");
35                 if (bytes.length != SRC6_F_LENGTH + DEST6_F_LENGT)
36                         throw new PCEPDeserializerException("Wrong length of array of bytes.");
37
38                 if (!processed)
39                         throw new PCEPDocumentedException("Processed flag not set", PCEPErrors.P_FLAG_NOT_SET);
40
41                 return new PCEPEndPointsObject<IPv6Address>(
42                                 new IPv6Address(ByteArray.subByte(bytes, SRC6_F_OFFSET, SRC6_F_LENGTH)),
43                                 new IPv6Address(ByteArray.subByte(bytes, DEST6_F_OFFSET, DEST6_F_LENGT)));
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 IPv6Address))
54                         throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + ePObj.getSourceAddress().getClass() + ". Needed IPv6Address");
55
56                 final byte[] retBytes = new byte[SRC6_F_LENGTH + DEST6_F_LENGT];
57                 ByteArray.copyWhole(((IPv6Address) ePObj.getSourceAddress()).getAddress(), retBytes, SRC6_F_OFFSET);
58                 ByteArray.copyWhole(((IPv6Address) ePObj.getDestinationAddress()).getAddress(), retBytes, DEST6_F_OFFSET);
59
60                 return retBytes;
61         }
62
63 }