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