97a5998ba7932f7589fe1727bc3fc59a4fd832b0
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.subobject;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
11
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.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
22 final class AsNumberCaseParser {
23
24     private AsNumberCaseParser() {
25         throw new UnsupportedOperationException();
26     }
27
28     private static final int CONTENT_LENGTH = 2;
29
30     static AsNumberCase parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
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 PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
34                     + CONTENT_LENGTH + ".");
35         }
36         return new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build();
37     }
38
39
40     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);
45         return body;
46     }
47 }