BUG-64 : initial rewrite, EROSubobjectRegistry.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROPathKey128SubobjectParser.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 java.util.Arrays;
11
12 import org.opendaylight.protocol.pcep.impl.object.RROSubobjectUtil;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
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.reported.route.object.rro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.path.key._case.PathKeyBuilder;
24
25 public class RROPathKey128SubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
26
27         public static final int TYPE = 65;
28
29         private static final int PK_F_LENGTH = 2;
30
31         private static final int PCE128_ID_F_LENGTH = 16;
32
33         private static final int PK_F_OFFSET = 0;
34         private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
35
36         private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
37
38         @Override
39         public Subobject parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
40                 if (buffer == null || buffer.length == 0) {
41                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
42                 }
43                 if (buffer.length != CONTENT128_LENGTH) {
44                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
45                                         + CONTENT128_LENGTH + ".");
46                 }
47                 final byte[] pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT128_LENGTH);
48                 final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET));
49                 final SubobjectBuilder builder = new SubobjectBuilder();
50                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
51                 pBuilder.setPceId(new PceId(pceId));
52                 pBuilder.setPathKey(new PathKey(pathKey));
53                 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
54                 return builder.build();
55         }
56
57         @Override
58         public byte[] serializeSubobject(final Subobject subobject) {
59                 final PathKeyCase pk = (PathKeyCase) subobject.getSubobjectType();
60                 final int pathKey = pk.getPathKey().getPathKey().getValue();
61                 final byte[] pceId = pk.getPathKey().getPceId().getBinary();
62                 final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
63                 System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
64                 System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
65                 return RROSubobjectUtil.formatSubobject(TYPE, retBytes);
66         }
67
68         @Override
69         public int getType() {
70                 return TYPE;
71         }
72 }