BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROAsNumberSubobjectParser.java
1 /*
2  * Copyright (c) 2013 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 io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.util.ByteArray;
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.pcep.types.rev131005.explicit.route.object.ero.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AsNumberSubobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
24
25 import com.google.common.base.Preconditions;
26
27 /**
28  * Parser for {@link AsNumberCase}
29  */
30 public class EROAsNumberSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
31
32         public static final int TYPE = 32;
33
34         public static final int AS_NUMBER_LENGTH = 2;
35
36         public static final int AS_NUMBER_OFFSET = 0;
37
38         public static final int CONTENT_LENGTH = AS_NUMBER_LENGTH + AS_NUMBER_OFFSET;
39
40         @Override
41         public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
42                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
43                 if (buffer.readableBytes() != CONTENT_LENGTH) {
44                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
45                                         + CONTENT_LENGTH + ".");
46                 }
47                 return new SubobjectBuilder().setLoose(loose).setSubobjectType(
48                                 new AsNumberCaseBuilder().setAsNumber(
49                                                 new AsNumberBuilder().setAsNumber(new AsNumber((long) buffer.readUnsignedShort())).build()).build()).build();
50         }
51
52         @Override
53         public byte[] serializeSubobject(final Subobject subobject) {
54                 if (!(subobject.getSubobjectType() instanceof AsNumberCase)) {
55                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
56                                         + ". Needed AsNumberCase.");
57                 }
58
59                 final byte[] retBytes = new byte[CONTENT_LENGTH];
60
61                 final AsNumberSubobject s = ((AsNumberCase) subobject.getSubobjectType()).getAsNumber();
62
63                 System.arraycopy(ByteArray.longToBytes(s.getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes, AS_NUMBER_OFFSET,
64                                 AS_NUMBER_LENGTH);
65
66                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
67         }
68 }