2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.segment.routing;
10 import static org.opendaylight.protocol.pcep.segment.routing.SrSubobjectParserUtil.BITSET_LENGTH;
12 import com.google.common.base.Function;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SrEroSubobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SrSubobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.add.lsp.input.arguments.ero.subobject.subobject.type.SrEroTypeBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
26 public class SrEroSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
28 public static final int TYPE = 5;
30 private static final int M_FLAG_POSITION = 7;
31 private static final int C_FLAG_POSITION = 6;
32 private static final int S_FLAG_POSITION = 5;
33 private static final int F_FLAG_POSITION = 4;
34 private static final int MPLS_LABEL_OFFSET = 12;
37 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
38 Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrEroSubobject,
39 "Unknown subobject instance. Passed %s. Needed SrEroSubobject.", subobject.getSubobjectType()
42 final SrEroSubobject srEroSubobject = (SrEroSubobject) subobject.getSubobjectType();
43 final SrEroTypeBuilder builder = new SrEroTypeBuilder(srEroSubobject);
44 if (srEroSubobject.isMFlag() != null && srEroSubobject.isMFlag() && srEroSubobject.getSid() != null) {
45 builder.setSid(srEroSubobject.getSid() << MPLS_LABEL_OFFSET);
47 final BitArray bits = new BitArray(BITSET_LENGTH);
48 bits.set(M_FLAG_POSITION, srEroSubobject.isMFlag());
49 bits.set(C_FLAG_POSITION, srEroSubobject.isCFlags());
50 if (srEroSubobject.getSid() == null) {
51 bits.set(S_FLAG_POSITION, Boolean.TRUE);
53 if (srEroSubobject.getNai() == null) {
54 bits.set(F_FLAG_POSITION, Boolean.TRUE);
56 final ByteBuf body = SrSubobjectParserUtil.serializeSrSubobject(builder.build(), bits);
57 EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
61 public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
62 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
63 "Array of bytes is mandatory. Can't be null or empty.");
64 if (buffer.readableBytes() <= SrSubobjectParserUtil.MINIMAL_LENGTH) {
65 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
67 final BitArray flags = new BitArray(BITSET_LENGTH);
68 final SrSubobject srSubobject = SrSubobjectParserUtil.parseSrSubobject(buffer, new Function<BitArray, Void>() {
70 public Void apply(final BitArray input) {
71 flags.set(C_FLAG_POSITION, input.get(C_FLAG_POSITION));
72 flags.set(M_FLAG_POSITION, input.get(M_FLAG_POSITION));
75 }, F_FLAG_POSITION, S_FLAG_POSITION);
76 final SrEroTypeBuilder srEroSubobjectBuilder = new SrEroTypeBuilder(srSubobject);
77 srEroSubobjectBuilder.setCFlags(flags.get(C_FLAG_POSITION));
78 srEroSubobjectBuilder.setMFlag(flags.get(M_FLAG_POSITION));
79 if (srEroSubobjectBuilder.isMFlag() != null && srEroSubobjectBuilder.isMFlag() && srEroSubobjectBuilder.getSid() != null) {
80 srEroSubobjectBuilder.setSid(srEroSubobjectBuilder.getSid() >> MPLS_LABEL_OFFSET);
82 final SubobjectBuilder subobjectBuilder = new SubobjectBuilder();
83 subobjectBuilder.setLoose(loose);
84 subobjectBuilder.setSubobjectType(srEroSubobjectBuilder.build());
85 return subobjectBuilder.build();