2 * Copyright (c) 2015 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
9 package org.opendaylight.protocol.pcep.spi;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
25 * Parser for {@link PathKey}
27 public abstract class AbstractEROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29 public static final int TYPE_32 = 64;
30 public static final int TYPE_128 = 65;
31 protected static final int PCE_ID_F_LENGTH = 4;
32 protected static final int PCE128_ID_F_LENGTH = 16;
33 protected static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
34 protected static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
36 protected abstract byte[] readPceId(final ByteBuf buffer);
38 protected abstract void checkContentLenght(final int i) throws PCEPDeserializerException;
41 public final Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
42 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
43 checkContentLenght(buffer.readableBytes());
44 final int pathKey = buffer.readUnsignedShort();
45 final byte[] pceId = readPceId(buffer);
46 final PathKeyBuilder pBuilder = new PathKeyBuilder();
47 pBuilder.setPceId(new PceId(pceId));
48 pBuilder.setPathKey(new PathKey(pathKey));
49 final SubobjectBuilder builder = new SubobjectBuilder();
50 builder.setLoose(loose);
51 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
52 return builder.build();
56 public final void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
57 Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.",
58 subobject.getSubobjectType().getClass());
59 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
60 .subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
61 Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
62 Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
63 final byte[] pceID = pk.getPceId().getBinary();
64 Preconditions.checkArgument(pceID.length == PCE_ID_F_LENGTH || pceID.length == PCE128_ID_F_LENGTH, "PceId 32/128 Bit required.");
65 final ByteBuf body = Unpooled.buffer();
66 writeUnsignedShort(pk.getPathKey().getValue(), body);
67 body.writeBytes(pceID);
68 EROSubobjectUtil.formatSubobject(pceID.length == PCE_ID_F_LENGTH ? TYPE_32 : TYPE_128, subobject.isLoose(), body, buffer);