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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
29 * Parser for {@link PathKey}
31 // TODO: refactor to abstract class
32 public class EROPathKey32SubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
34 public static final int TYPE = 64;
36 private static final int PCE_ID_F_LENGTH = 4;
38 private static final int CONTENT_LENGTH = 2 + PCE_ID_F_LENGTH;
41 public 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 if (buffer.readableBytes() != CONTENT_LENGTH) {
44 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
45 + CONTENT_LENGTH + ".");
47 final int pathKey = buffer.readUnsignedShort();
48 final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH);
49 final SubobjectBuilder builder = new SubobjectBuilder();
50 final PathKeyBuilder pBuilder = new PathKeyBuilder();
51 pBuilder.setPceId(new PceId(pceId));
52 pBuilder.setPathKey(new PathKey(pathKey));
53 builder.setLoose(loose);
54 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
55 return builder.build();
59 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
60 Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase, "Unknown subobject instance. Passed %s. Needed PathKey.", subobject.getSubobjectType().getClass());
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) subobject.getSubobjectType()).getPathKey();
63 final ByteBuf body = Unpooled.buffer();
64 Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
65 writeUnsignedShort(pk.getPathKey().getValue(), body);
66 Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
67 body.writeBytes(pk.getPceId().getBinary());
68 EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);