BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROPathKeySubobjectParser.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.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
18
19 public class EROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
20
21         public static final int TYPE = 64;
22
23         public static final int TYPE128 = 65;
24
25         public static final int PK_F_LENGTH = 2;
26         public static final int PCE_ID_F_LENGTH = 4;
27
28         public static final int PCE128_ID_F_LENGTH = 16;
29
30         public static final int PK_F_OFFSET = 0;
31         public static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
32
33         public static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
34
35         @Override
36         public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
37                 if (buffer == null || buffer.length == 0)
38                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
39                 if (buffer.length != CONTENT_LENGTH)
40                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
41                                         + CONTENT_LENGTH + ".");
42
43                 final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET)) & 0xFFFF;
44
45                 final byte[] pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT_LENGTH);
46
47                 final SubobjectsBuilder builder = new SubobjectsBuilder();
48                 builder.setLoose(loose);
49                 // builder.setSubobjectType(value);
50                 return builder.build();
51         }
52
53         @Override
54         public byte[] serializeSubobject(final Subobjects subobject) {
55                 final byte[] retBytes = new byte[CONTENT_LENGTH];
56
57                 // System.arraycopy(ByteArray.shortToBytes((short) objToSerialize.getPathKey()), 0, retBytes, PK_F_OFFSET,
58                 // PK_F_LENGTH);
59                 //
60                 // if (objToSerialize.getPceId().length != PCE_ID_F_LENGTH)
61                 // throw new IllegalArgumentException("Wrong length of pce id. Passed: " + objToSerialize.getPceId().length +
62                 // ". Expected: ="
63                 // + PCE_ID_F_LENGTH);
64                 // System.arraycopy(objToSerialize.getPceId(), 0, retBytes, PCE_ID_F_OFFSET, PCE_ID_F_LENGTH);
65
66                 return retBytes;
67         }
68
69         @Override
70         public int getType() {
71                 return TYPE;
72         }
73
74         public int getType128() {
75                 return TYPE128;
76         }
77 }