Rework PCEPDeserializerException and move it into SPI
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROPathKeySubobjectParser.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.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobjects;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobjects.subobject.type.PathKeyBuilder;
21
22 public class RROPathKeySubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
23
24         public static final int TYPE = 64;
25
26         public static final int TYPE128 = 65;
27
28         private static final int PK_F_LENGTH = 2;
29         private static final int PCE_ID_F_LENGTH = 4;
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 CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
37         private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
38
39         @Override
40         public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
41                 if (buffer == null || buffer.length == 0) {
42                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
43                 }
44                 byte[] pceId = null;
45                 if (buffer.length == CONTENT_LENGTH) {
46                         pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT_LENGTH);
47                 } else if (buffer.length == CONTENT128_LENGTH) {
48                         pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT128_LENGTH);
49                 } else {
50                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
51                                         + CONTENT_LENGTH + ".");
52                 }
53                 final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET));
54                 final SubobjectsBuilder builder = new SubobjectsBuilder();
55                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobjects.subobject.type.path.key.PathKeyBuilder pBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobjects.subobject.type.path.key.PathKeyBuilder();
56                 pBuilder.setPceId(new PceId(pceId));
57                 pBuilder.setPathKey(new PathKey(pathKey));
58                 builder.setSubobjectType(new PathKeyBuilder().setPathKey(pBuilder.build()).build());
59                 return builder.build();
60         }
61
62         @Override
63         public byte[] serializeSubobject(final Subobjects subobject) {
64                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobjects.subobject.type.PathKey pk = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobjects.subobject.type.PathKey) subobject.getSubobjectType();
65                 final int pathKey = pk.getPathKey().getPathKey().getValue();
66                 final byte[] pceId = pk.getPathKey().getPceId().getBinary();
67                 final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
68                 System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
69                 System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
70                 return retBytes;
71         }
72
73         @Override
74         public int getType() {
75                 return TYPE;
76         }
77
78         public int getType128() {
79                 return TYPE128;
80         }
81 }