BUG-204 : Cleanup models to follow convention
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROPathKeySubobjectParser.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.EROSubobjectParser;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.explicit.route.object.ero.Subobjects;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKeyCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.PathKeyCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key._case.PathKeyBuilder;
23
24 /**
25  * Parser for {@link PathKey}
26  */
27 public class EROPathKeySubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
28
29         public static final int TYPE = 64;
30
31         public static final int TYPE128 = 65;
32
33         private static final int PK_F_LENGTH = 2;
34         private static final int PCE_ID_F_LENGTH = 4;
35
36         private static final int PCE128_ID_F_LENGTH = 16;
37
38         private static final int PK_F_OFFSET = 0;
39         private static final int PCE_ID_F_OFFSET = PK_F_OFFSET + PK_F_LENGTH;
40
41         private static final int CONTENT_LENGTH = PCE_ID_F_OFFSET + PCE_ID_F_LENGTH;
42         private static final int CONTENT128_LENGTH = PCE_ID_F_OFFSET + PCE128_ID_F_LENGTH;
43
44         @Override
45         public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
46                 if (buffer == null || buffer.length == 0) {
47                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
48                 }
49                 byte[] pceId = null;
50                 if (buffer.length == CONTENT_LENGTH) {
51                         pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT_LENGTH);
52                 } else if (buffer.length == CONTENT128_LENGTH) {
53                         pceId = Arrays.copyOfRange(buffer, PCE_ID_F_OFFSET, CONTENT128_LENGTH);
54                 } else {
55                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: >"
56                                         + CONTENT_LENGTH + ".");
57                 }
58                 final int pathKey = ByteArray.bytesToShort(Arrays.copyOfRange(buffer, PK_F_OFFSET, PCE_ID_F_OFFSET));
59                 final SubobjectsBuilder builder = new SubobjectsBuilder();
60                 builder.setLoose(loose);
61                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
62                 pBuilder.setPceId(new PceId(pceId));
63                 pBuilder.setPathKey(new PathKey(pathKey));
64                 builder.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
65                 return builder.build();
66         }
67
68         @Override
69         public byte[] serializeSubobject(final Subobjects subobject) {
70                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobjects.subobject.type.path.key._case.PathKey pk = ((PathKeyCase) subobject.getSubobjectType()).getPathKey();
71                 final int pathKey = pk.getPathKey().getValue();
72                 final byte[] pceId = pk.getPceId().getBinary();
73                 final byte[] retBytes = new byte[PK_F_LENGTH + pceId.length];
74                 System.arraycopy(ByteArray.shortToBytes((short) pathKey), 0, retBytes, PK_F_OFFSET, PK_F_LENGTH);
75                 System.arraycopy(pceId, 0, retBytes, PCE_ID_F_OFFSET, pceId.length);
76                 return retBytes;
77         }
78
79         @Override
80         public int getType() {
81                 return TYPE;
82         }
83
84         public int getType128() {
85                 return TYPE128;
86         }
87 }