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.rsvp.parser.impl.subobject;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AsNumberSubobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
23 public final class AsNumberCaseParser {
24 private static final int CONTENT_LENGTH = 2;
26 private AsNumberCaseParser() {
27 throw new UnsupportedOperationException();
30 public static AsNumberCase parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
31 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
32 if (buffer.readableBytes() != CONTENT_LENGTH) {
33 throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
34 + CONTENT_LENGTH + ".");
36 return new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build();
40 public static ByteBuf serializeSubobject(final AsNumberCase asCase) {
41 final AsNumberSubobject asNumber = asCase.getAsNumber();
42 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
43 Preconditions.checkArgument(asNumber.getAsNumber() != null, "AsNumber is mandatory.");
44 writeShort(asNumber.getAsNumber().getValue().shortValue(), body);