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