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
8 package org.opendaylight.protocol.pcep.spi;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
24 * Parser for {@link PathKey}.
26 public abstract class AbstractEROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
28 public static final int TYPE_32 = 64;
29 public static final int TYPE_128 = 65;
30 protected static final int PCE_ID_F_LENGTH = 4;
31 protected static final int PCE128_ID_F_LENGTH = 16;
32 protected static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
33 protected static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
35 protected abstract byte[] readPceId(ByteBuf buffer);
37 protected abstract void checkContentLength(int length) throws PCEPDeserializerException;
40 public final Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
41 checkArgument(buffer != null && buffer.isReadable(),
42 "Array of bytes is mandatory. Can't be null or empty.");
43 checkContentLength(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 checkArgument(subobject.getSubobjectType() instanceof PathKeyCase,
58 "Unknown subobject instance. Passed %s. Needed PathKey.", 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 =
61 ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
62 checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
63 final byte[] pceID = pk.getPceId().getValue();
64 checkArgument(pceID.length == PCE_ID_F_LENGTH || pceID.length == PCE128_ID_F_LENGTH,
65 "PceId 32/128 Bit required.");
66 final ByteBuf body = Unpooled.buffer();
67 writeUnsignedShort(pk.getPathKey().getValue(), body);
68 body.writeBytes(pceID);
69 EROSubobjectUtil.formatSubobject(pceID.length == PCE_ID_F_LENGTH ? TYPE_32 : TYPE_128, subobject.isLoose(),