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;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
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.basic.explicit.route.subobjects.SubobjectType;
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;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26 * Parser for {@link PathKey}.
28 public abstract class AbstractEROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
29 public static final int TYPE_32 = 64;
30 public static final int TYPE_128 = 65;
32 protected static final int PCE_ID_F_LENGTH = 4;
33 protected static final int PCE128_ID_F_LENGTH = 16;
34 protected static final int CONTENT128_LENGTH = 2 + PCE128_ID_F_LENGTH;
35 protected static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
37 protected abstract byte[] readPceId(ByteBuf buffer);
39 protected abstract void checkContentLength(int length) throws PCEPDeserializerException;
42 public final Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
43 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44 checkContentLength(buffer.readableBytes());
45 return new SubobjectBuilder()
47 .setSubobjectType(new PathKeyCaseBuilder()
48 .setPathKey(new PathKeyBuilder()
49 .setPathKey(new PathKey(ByteBufUtils.readUint16(buffer)))
50 .setPceId(new PceId(readPceId(buffer)))
57 public final void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
58 final SubobjectType type = subobject.getSubobjectType();
59 checkArgument(type instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.",
61 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
62 .subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) type).getPathKey();
64 final PathKey pathKey = pk.getPathKey();
65 checkArgument(pathKey != null, "PathKey is mandatory.");
66 final byte[] pceID = pk.getPceId().getValue();
67 checkArgument(pceID.length == PCE_ID_F_LENGTH || pceID.length == PCE128_ID_F_LENGTH,
68 "PceId 32/128 Bit required.");
69 final ByteBuf body = Unpooled.buffer();
70 ByteBufUtils.write(body, pathKey.getValue());
71 body.writeBytes(pceID);
72 EROSubobjectUtil.formatSubobject(pceID.length == PCE_ID_F_LENGTH ? TYPE_32 : TYPE_128, subobject.getLoose(),