a502ebcf5ad48446612ff720df084ed07c7b7130
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / AsNumberCaseParser.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.parser.subobject;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AsNumberSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
20 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 import org.opendaylight.yangtools.yang.common.Uint32;
22
23 final class AsNumberCaseParser {
24
25     private AsNumberCaseParser() {
26         throw new UnsupportedOperationException();
27     }
28
29     private static final int CONTENT_LENGTH = 2;
30
31     static AsNumberCase parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
32         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
33         if (buffer.readableBytes() != CONTENT_LENGTH) {
34             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
35                 + "; Expected: " + CONTENT_LENGTH + ".");
36         }
37         return new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(
38             new AsNumber(Uint32.valueOf(buffer.readUnsignedShort()))).build()).build();
39     }
40
41     static ByteBuf serializeSubobject(final AsNumberCase asCase) {
42         final AsNumberSubobject asNumber = asCase.getAsNumber();
43         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
44         checkArgument(asNumber.getAsNumber() != null, "AsNumber is mandatory.");
45         writeShort(asNumber.getAsNumber().getValue().shortValue(), body);
46         return body;
47     }
48 }