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.impl.object.EROSubobjectUtil;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKeyCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKeyCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key._case.PathKeyBuilder;
26 * Parser for {@link PathKey}
28 public class EROPathKey128SubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
30 public static final int TYPE = 65;
32 private static final int PK_F_LENGTH = 2;
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 CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
42 public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
43 if (buffer == null || buffer.length == 0) {
44 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
46 if (buffer.length != CONTENT128_LENGTH) {
47 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
48 + CONTENT128_LENGTH + ".");
50 final byte[] pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT128_LENGTH);
51 final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET));
52 final SubobjectsBuilder builder = new SubobjectsBuilder();
53 builder.setLoose(loose);
54 final PathKeyBuilder pBuilder = new PathKeyBuilder();
55 pBuilder.setPceId(new PceId(pceId));
56 pBuilder.setPathKey(new PathKey(pathKey));
57 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
58 return builder.build();
62 public byte[] serializeSubobject(final Subobjects subobject) {
63 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
64 final int pathKey = pk.getPathKey().getValue();
65 final byte[] pceId = pk.getPceId().getBinary();
66 final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
67 System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
68 System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
69 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
73 public int getType() {