Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPUnreachedIPv6DestinationObjectParser.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.IPv6;
11 import org.opendaylight.protocol.concepts.IPv6Address;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.PCEPObject;
15 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
16 import org.opendaylight.protocol.pcep.impl.Util;
17 import org.opendaylight.protocol.pcep.object.PCEPUnreachedDestinationObject;
18
19 public class PCEPUnreachedIPv6DestinationObjectParser implements PCEPObjectParser {
20
21     /*
22      * fields lengths and offsets for IPv6 in bytes
23      */
24     public static final int DEST6_F_LENGTH = 16;
25
26     public static final int DEST6_F_OFFSET = 0;
27
28     @Override
29     public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
30         if (bytes == null)
31             throw new IllegalArgumentException("Array of bytes is mandatory");
32
33         return new PCEPUnreachedDestinationObject<IPv6Address>(Util.parseAddresses(bytes, DEST6_F_OFFSET, IPv6.FAMILY, DEST6_F_LENGTH), processed, ignored);
34     }
35
36     @Override
37     public byte[] put(PCEPObject obj) {
38         if (!(obj instanceof PCEPUnreachedDestinationObject))
39             throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPUnreachedDestinationObject.");
40
41         final PCEPUnreachedDestinationObject<?> uDObj = (PCEPUnreachedDestinationObject<?>) obj;
42
43         if (uDObj.getUnreachedDestinations().isEmpty())
44             return new byte[0];
45
46         if (!(uDObj.getUnreachedDestinations().get(0) instanceof IPv6Address))
47             throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + uDObj.getUnreachedDestinations().get(0).getClass()
48                     + ". Needed IPv6Address");
49
50         final byte[] retBytes = new byte[DEST6_F_LENGTH * uDObj.getUnreachedDestinations().size()];
51         Util.putAddresses(retBytes, DEST6_F_OFFSET, uDObj.getUnreachedDestinations(), DEST6_F_LENGTH);
52         return retBytes;
53     }
54 }