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.subobject;
10 import java.util.Arrays;
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.PathKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKeyBuilder;
23 * Parser for {@link PathKey}
25 public class EROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
27 public static final int TYPE = 64;
29 public static final int TYPE128 = 65;
31 private static final int PK_F_LENGTH = 2;
32 private static final int PCE_ID_F_LENGTH = 4;
34 private static final int PCE128_ID_F_LENGTH = 16;
36 private static final int PK_F_OFFSET = 0;
37 private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
39 private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
40 private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
43 public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
44 if (buffer == null || buffer.length == 0) {
45 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
48 if (buffer.length == CONTENT_LENGTH) {
49 pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT_LENGTH);
50 } else if (buffer.length == CONTENT128_LENGTH) {
51 pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT128_LENGTH);
53 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
54 + CONTENT_LENGTH + ".");
56 final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET));
57 final SubobjectsBuilder builder = new SubobjectsBuilder();
58 builder.setLoose(loose);
59 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key.PathKeyBuilder pBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key.PathKeyBuilder();
60 pBuilder.setPceId(new PceId(pceId));
61 pBuilder.setPathKey(new PathKey(pathKey));
62 builder.setSubobjectType(new PathKeyBuilder().setPathKey(pBuilder.build()).build());
63 return builder.build();
67 public byte[] serializeSubobject(final Subobjects subobject) {
68 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKey pk = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKey) subobject.getSubobjectType();
69 final int pathKey = pk.getPathKey().getPathKey().getValue();
70 final byte[] pceId = pk.getPathKey().getPceId().getBinary();
71 final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
72 System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
73 System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
78 public int getType() {
82 public int getType128() {