BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROAttributesSubobjectParser.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.subobject;
9
10 import java.util.Arrays;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.subobject.RROAttributesSubobject;
14
15 public class RROAttributesSubobjectParser {
16
17     public static final int RES_F_LENGTH = 2;
18
19     public static final int RES_F_OFFSET = 0;
20     public static final int ATTRS_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
21
22     public static RROAttributesSubobject parse(byte[] soContentsBytes) throws PCEPDeserializerException {
23         if (soContentsBytes == null || soContentsBytes.length == 0)
24             throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
25         if (soContentsBytes.length <= RES_F_LENGTH)
26             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: >" + RES_F_LENGTH + ".");
27
28         final byte[] attributes = Arrays.copyOfRange(soContentsBytes, ATTRS_F_OFFSET, soContentsBytes.length);
29
30         return new RROAttributesSubobject(attributes);
31     }
32
33     public static byte[] put(RROAttributesSubobject objToSerialize) {
34         final byte[] retBytes = new byte[RES_F_LENGTH + objToSerialize.getAttributes().length];
35
36         final byte[] attrs = objToSerialize.getAttributes();
37
38         System.arraycopy(attrs, 0, retBytes, ATTRS_F_OFFSET, attrs.length);
39
40         return retBytes;
41     }
42 }