BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROPathKey32SubobjectParser.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.PathKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKeyBuilder;
24
25 import com.google.common.base.Preconditions;
26
27 /**
28  * Parser for {@link PathKey}
29  */
30 public class EROPathKey32SubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
31
32         public static final int TYPE = 64;
33
34         private static final int PK_F_LENGTH = 2;
35         private static final int PCE_ID_F_LENGTH = 4;
36
37         private static final int PK_F_OFFSET = 0;
38         private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
39
40         private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
41
42         @Override
43         public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
44                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
45                 if (buffer.readableBytes() != CONTENT_LENGTH) {
46                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
47                                         + CONTENT_LENGTH + ".");
48                 }
49                 final int pathKey = buffer.readUnsignedShort();
50                 final byte[] pceId = ByteArray.readBytes(buffer, PCE_ID_F_LENGTH);
51                 final SubobjectBuilder builder = new SubobjectBuilder();
52                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
53                 pBuilder.setPceId(new PceId(pceId));
54                 pBuilder.setPathKey(new PathKey(pathKey));
55                 builder.setLoose(loose);
56                 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
57                 return builder.build();
58         }
59
60         @Override
61         public byte[] serializeSubobject(final Subobject subobject) {
62                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
63                 final int pathKey = pk.getPathKey().getValue();
64                 final byte[] pceId = pk.getPceId().getBinary();
65                 final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
66                 System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
67                 System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
68                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
69         }
70 }