aae0b61e97140f11ad5de81264395f7858847452
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROUnnumberedInterfaceSubobjectParser.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 org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
11 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
21
22 import com.google.common.primitives.UnsignedInts;
23
24 /**
25  * Parser for {@link UnnumberedCase}
26  */
27 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
28
29         public static final int TYPE = 4;
30
31         private static final int ROUTER_ID_NUMBER_LENGTH = 4;
32         private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
33
34         private static final int ROUTER_ID_NUMBER_OFFSET = 2;
35         private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
36
37         private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
38
39         @Override
40         public Subobject parseSubobject(final byte[] buffer, final boolean loose) 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                 if (buffer.length != CONTENT_LENGTH) {
45                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
46                                         + CONTENT_LENGTH + ".");
47                 }
48                 final SubobjectBuilder builder = new SubobjectBuilder();
49                 builder.setLoose(loose);
50                 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
51                 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
52                 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
53                                 INTERFACE_ID_NUMBER_LENGTH))));
54                 builder.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(ubuilder.build()).build());
55                 return builder.build();
56         }
57
58         @Override
59         public byte[] serializeSubobject(final Subobject subobject) {
60                 if (!(subobject.getSubobjectType() instanceof UnnumberedCase)) {
61                         throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
62                                         + ". Needed UnnumberedCase.");
63                 }
64                 byte[] retBytes;
65                 retBytes = new byte[CONTENT_LENGTH];
66                 final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
67                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
68                 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
69                                 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
70                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
71         }
72 }