Ignore unknown subobjects while parsing RRO/ERO objects in PCEP messages
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / ero / EROLabelSubobjectParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.subobject.ero;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.rsvp.parser.spi.LabelRegistry;
19 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
20 import org.opendaylight.protocol.util.BitArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
32     private static final Logger LOG = LoggerFactory.getLogger(EROLabelSubobjectParser.class);
33
34     public static final int TYPE = 3;
35
36     private static final int FLAGS_SIZE = 8;
37
38     private static final int C_TYPE_F_LENGTH = 1;
39
40     private static final int C_TYPE_F_OFFSET = FLAGS_SIZE / Byte.SIZE;
41
42     private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
43
44     private static final int U_FLAG_OFFSET = 0;
45
46     private final LabelRegistry registry;
47
48     public EROLabelSubobjectParser(final LabelRegistry labelReg) {
49         this.registry = requireNonNull(labelReg);
50     }
51
52     @Override
53     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException {
54         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
55             "Array of bytes is mandatory. Can't be null or empty.");
56         if (buffer.readableBytes() < HEADER_LENGTH) {
57             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; "
58                 + "Expected: >" + HEADER_LENGTH + ".");
59         }
60         final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
61         final short cType = buffer.readUnsignedByte();
62
63         final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
64         if (labelType == null) {
65             LOG.warn("Ignoring ERO label subobject with unknown C-TYPE: {}", cType);
66             return null;
67         }
68         final LabelBuilder builder = new LabelBuilder();
69         builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
70         builder.setLabelType(labelType);
71         return new SubobjectContainerBuilder().setLoose(loose).setSubobjectType(
72             new LabelCaseBuilder().setLabel(builder.build()).build()).build();
73     }
74
75     @Override
76     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
77         Preconditions.checkArgument(subobject.getSubobjectType() instanceof LabelCase,
78             "Unknown subobject instance. Passed %s. Needed LabelCase.",
79             subobject.getSubobjectType().getClass());
80         final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
81         final ByteBuf body = Unpooled.buffer();
82         this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType(), body);
83         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
84     }
85 }