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.rsvp.parser.impl.subobject.rro;
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.rsvp.parser.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
27 public class RROPathKey32SubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
29 public static final int TYPE = 64;
31 private static final int PK_F_LENGTH = 2;
32 private static final int PCE_ID_F_LENGTH = 4;
34 private static final int PK_F_OFFSET = 0;
35 private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
37 private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
40 public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
41 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
42 "Array of bytes is mandatory. Can't be null or empty.");
43 if (buffer.readableBytes() != CONTENT_LENGTH) {
44 throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
45 + "; " + "Expected: >" + CONTENT_LENGTH + ".");
47 final int pathKey = buffer.readUnsignedShort();
48 final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH);
49 final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
50 final PathKeyBuilder pBuilder = new PathKeyBuilder();
51 pBuilder.setPceId(new PceId(pceId));
52 pBuilder.setPathKey(new PathKey(pathKey));
53 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
54 return builder.build();
58 public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
59 Preconditions.checkArgument(subobject.getSubobjectType() instanceof PathKeyCase,
60 "Unknown subobject instance.Passed %s. Needed PathKey.",
61 subobject.getSubobjectType().getClass());
62 final PathKeyCase pkcase = (PathKeyCase) subobject.getSubobjectType();
63 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects
64 .subobject.type.path.key._case.PathKey pk = pkcase.getPathKey();
65 final ByteBuf body = Unpooled.buffer();
66 Preconditions.checkArgument(pk.getPceId() != null, "PceId is mandatory.");
68 final byte[] pceId = pk.getPceId().getValue();
69 if (pceId.length == RROPathKey128SubobjectParser.PCE128_ID_F_LENGTH) {
70 RROPathKey128SubobjectParser.serializeSubobject(subobject, buffer);
72 Preconditions.checkArgument(pk.getPathKey() != null, "PathKey is mandatory.");
73 Preconditions.checkArgument(pceId.length == PCE_ID_F_LENGTH, "PathKey 32Bit is mandatory.");
74 writeUnsignedShort(pk.getPathKey().getValue(), body);
75 body.writeBytes(pceId);
76 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);