2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.object;
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;
20 * Parser for IPv6 {@link org.opendaylight.protocol.pcep.object.PCEPEndPointsObject
21 * PCEPEndPointsObject}
23 public class PCEPEndPointsIPv6ObjectParser implements PCEPObjectParser {
25 public static final int SRC6_F_LENGTH = 16;
26 public static final int DEST6_F_LENGT = 16;
28 public static final int SRC6_F_OFFSET = 0;
29 public static final int DEST6_F_OFFSET = SRC6_F_OFFSET + SRC6_F_LENGTH;
32 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
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.");
39 throw new PCEPDocumentedException("Processed flag not set", PCEPErrors.P_FLAG_NOT_SET);
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)));
47 public byte[] put(PCEPObject obj) {
48 if (!(obj instanceof PCEPEndPointsObject))
49 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPEndPointsObject.");
51 final PCEPEndPointsObject<?> ePObj = (PCEPEndPointsObject<?>) obj;
53 if (!(ePObj.getSourceAddress() instanceof IPv6Address))
54 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + ePObj.getSourceAddress().getClass() + ". Needed IPv6Address");
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);