Initial code drop
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPExistingPathBandwidthObjectParser.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.Bandwidth;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.PCEPObject;
13 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
14 import org.opendaylight.protocol.pcep.object.PCEPExistingPathBandwidthObject;
15 import org.opendaylight.protocol.util.ByteArray;
16
17 /**
18  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPExistingPathBandwidthObject
19  * PCEPExistingPathBandwidthObject}
20  */
21 public class PCEPExistingPathBandwidthObjectParser implements PCEPObjectParser {
22
23         private static final int BANDWIDTH_F_LENGTH = 4;
24
25         @Override
26         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
27                 if (bytes == null)
28                         throw new IllegalArgumentException("Array of bytes is mandatory");
29                 if (bytes.length != BANDWIDTH_F_LENGTH)
30                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + BANDWIDTH_F_LENGTH + ".");
31
32                 return new PCEPExistingPathBandwidthObject(new Bandwidth(ByteArray.bytesToFloat(bytes)), processed, ignored);
33         }
34
35         @Override
36         public byte[] put(PCEPObject obj) {
37                 if (!(obj instanceof PCEPExistingPathBandwidthObject))
38                         throw new IllegalArgumentException("Unknown PCEPObject instance.");
39
40                 return ByteArray.floatToBytes((float) ((PCEPExistingPathBandwidthObject) obj).getBandwidth().getBytesPerSecond());
41         }
42
43 }