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.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;
19 * Parser for IPv4 {@link org.opendaylight.protocol.pcep.object.PCEPEndPointsObject
20 * PCEPEndPointsObject}
22 public class PCEPEndPointsIPv4ObjectParser implements PCEPObjectParser {
25 * fields lengths and offsets for IPv4 in bytes
27 public static final int SRC4_F_LENGTH = 4;
28 public static final int DEST4_F_LENGTH = 4;
30 public static final int SRC4_F_OFFSET = 0;
31 public static final int DEST4_F_OFFSET = SRC4_F_OFFSET + SRC4_F_LENGTH;
34 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
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.");
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)));
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 IPv4Address))
54 throw new IllegalArgumentException("Wrong instance of NetworkAddress. Passed " + ePObj.getSourceAddress().getClass() + ". Needed IPv4Address");
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);