BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROUnnumberedInterfaceSubobjectParser.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.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
23
24 import com.google.common.base.Preconditions;
25
26 /**
27  * Parser for {@link UnnumberedCase}
28  */
29 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
30
31         public static final int TYPE = 4;
32
33         private static final int ROUTER_ID_NUMBER_LENGTH = 4;
34         private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
35
36         private static final int ROUTER_ID_NUMBER_OFFSET = 2;
37         private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
38
39         private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
40
41         @Override
42         public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
43                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44                 if (buffer.readableBytes() != CONTENT_LENGTH) {
45                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
46                                         + CONTENT_LENGTH + ".");
47                 }
48                 final SubobjectBuilder builder = new SubobjectBuilder();
49                 builder.setLoose(loose);
50                 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
51                 buffer.readerIndex(buffer.readerIndex() + ROUTER_ID_NUMBER_OFFSET);
52                 ubuilder.setRouterId(buffer.readUnsignedInt());
53                 ubuilder.setInterfaceId(buffer.readUnsignedInt());
54                 builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
55                 return builder.build();
56         }
57
58         @Override
59         public byte[] serializeSubobject(final Subobject subobject) {
60                 if (!(subobject.getSubobjectType() instanceof UnnumberedCase)) {
61                         throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
62                                         + ". Needed UnnumberedCase.");
63                 }
64                 byte[] retBytes;
65                 retBytes = new byte[CONTENT_LENGTH];
66                 final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
67                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
68                 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
69                                 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
70                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
71         }
72 }