BUG-612 : switch XRO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROSRLGSubobjectParser.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 io.netty.buffer.ByteBuf;
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.exclude.route.object.xro.Subobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgSubobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.primitives.UnsignedBytes;
28
29 /**
30  * Parser for {@link SrlgCase}
31  */
32 public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
33
34         public static final int TYPE = 34;
35
36         private static final int SRLG_ID_NUMBER_LENGTH = 4;
37         private static final int ATTRIBUTE_LENGTH = 1;
38
39         private static final int SRLG_ID_NUMBER_OFFSET = 0;
40         private static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH;
41
42         private static final int CONTENT_LENGTH = SRLG_ID_NUMBER_LENGTH + ATTRIBUTE_LENGTH;
43
44         @Override
45         public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
46                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
47                 if (buffer.readableBytes() != CONTENT_LENGTH) {
48                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
49                                         + CONTENT_LENGTH + ".");
50                 }
51
52                 final SubobjectBuilder builder = new SubobjectBuilder();
53                 builder.setMandatory(mandatory);
54                 builder.setAttribute(Attribute.Srlg);
55                 builder.setSubobjectType(new SrlgCaseBuilder().setSrlg(
56                                 new SrlgBuilder().setSrlgId(
57                                                 new SrlgId(buffer.readUnsignedInt())).build()).build());
58                 return builder.build();
59         }
60
61         @Override
62         public byte[] serializeSubobject(final Subobject subobject) {
63                 if (!(subobject.getSubobjectType() instanceof SrlgCase)) {
64                         throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
65                                         + ". Needed SrlgCase.");
66                 }
67
68                 byte[] retBytes;
69                 retBytes = new byte[CONTENT_LENGTH];
70                 final SrlgSubobject specObj = ((SrlgCase) subobject.getSubobjectType()).getSrlg();
71
72                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue(), SRLG_ID_NUMBER_LENGTH), retBytes, SRLG_ID_NUMBER_OFFSET);
73                 retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
74
75                 return XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), retBytes);
76         }
77 }