BUG-2794 : refactored code to use BitArray
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / segment / routing / SrRroSubobjectParser.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.segment.routing;
10
11 import static org.opendaylight.protocol.pcep.segment.routing.SrSubobjectParserUtil.BITSET_LENGTH;
12 import static org.opendaylight.protocol.pcep.segment.routing.SrSubobjectParserUtil.MINIMAL_LENGTH;
13
14 import com.google.common.base.Function;
15 import com.google.common.base.Preconditions;
16 import io.netty.buffer.ByteBuf;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
19 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
21 import org.opendaylight.protocol.util.BitArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SrRroSubobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SrSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.add.lsp.input.arguments.rro.subobject.subobject.type.SrRroTypeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
27
28 public class SrRroSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
29
30     public static final int TYPE = 6;
31
32     private static final int S_FLAG_POSITION = 7;
33     private static final int F_FLAG_POSITION = 6;
34
35     @Override
36     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
37         Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrRroSubobject,
38                 "Unknown subobject instance. Passed %s. Needed SrRroSubobject.", subobject.getSubobjectType()
39                         .getClass());
40
41         final SrRroSubobject srRroSubobject = (SrRroSubobject) subobject.getSubobjectType();
42         final BitArray bits = new BitArray(BITSET_LENGTH);
43         if (srRroSubobject.getSid() == null) {
44             bits.set(S_FLAG_POSITION, Boolean.TRUE);
45         }
46         if (srRroSubobject.getNai() == null) {
47             bits.set(F_FLAG_POSITION, Boolean.TRUE);
48         }
49         final ByteBuf body = SrSubobjectParserUtil.serializeSrSubobject(srRroSubobject, bits);
50         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
51     }
52
53     @Override
54     public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
55         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
56                 "Array of bytes is mandatory. Can't be null or empty.");
57         if (buffer.readableBytes() <= MINIMAL_LENGTH) {
58             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
59         }
60
61         final SrSubobject srSubobject = SrSubobjectParserUtil.parseSrSubobject(buffer, new Function<BitArray, Void>() {
62             @Override
63             public Void apply(final BitArray input) {
64                 return null;
65             }
66         }, F_FLAG_POSITION, S_FLAG_POSITION);
67         final SrRroTypeBuilder srRroSubobjectBuilder = new SrRroTypeBuilder(srSubobject);
68
69         final SubobjectBuilder subobjectBuilder = new SubobjectBuilder();
70         subobjectBuilder.setSubobjectType(srRroSubobjectBuilder.build());
71         return subobjectBuilder.build();
72     }
73 }