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.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPObject;
12 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
13 import org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nps.concepts.rev130930.Bandwidth;
18 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject PCEPLoadBalancingObject}
20 public class PCEPLoadBalancingObjectParser implements PCEPObjectParser {
22 public static final int FLAGS_F_LENGTH = 1;
23 public static final int MAX_LSP_F_LENGTH = 1;
24 public static final int MIN_BAND_F_LENGTH = 4;
26 public static final int FLAGS_F_OFFSET = 2;
27 public static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
28 public static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
30 public static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
33 public PCEPObject parse(final byte[] bytes, final boolean processed, final boolean ignored) throws PCEPDeserializerException {
34 if (bytes == null || bytes.length == 0)
35 throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
37 if (bytes.length != SIZE)
38 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
40 return new PCEPLoadBalancingObject(bytes[MAX_LSP_F_OFFSET] & 0xFF, new Bandwidth(ByteArray.subByte(bytes, MIN_BAND_F_OFFSET,
41 MIN_BAND_F_LENGTH)), processed);
45 public byte[] put(final PCEPObject obj) {
46 if (!(obj instanceof PCEPLoadBalancingObject))
47 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass()
48 + ". Needed PCEPLoadBalancingObject.");
50 final PCEPLoadBalancingObject specObj = (PCEPLoadBalancingObject) obj;
52 final byte[] retBytes = new byte[SIZE];
54 retBytes[MAX_LSP_F_OFFSET] = ByteArray.intToBytes(specObj.getMaxLSP())[Integer.SIZE / Byte.SIZE - 1];
55 ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);