Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPExcludeRouteObjectParser.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.parser.object;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectRegistry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.Xro;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.Xro.Flags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.XroBuilder;
22
23 /**
24  * Parser for {@link Xro}.
25  */
26 public final class PCEPExcludeRouteObjectParser extends AbstractXROWithSubobjectsParser {
27     private static final int CLASS = 17;
28     private static final int TYPE = 1;
29     private static final int FLAGS_OFFSET = 3;
30
31     public PCEPExcludeRouteObjectParser(final XROSubobjectRegistry registry) {
32         super(registry, CLASS, TYPE);
33     }
34
35     @Override
36     public Xro parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
37         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
38         bytes.skipBytes(FLAGS_OFFSET);
39         return new XroBuilder()
40                 .setIgnore(header.getIgnore())
41                 .setProcessingRule(header.getProcessingRule())
42                 .setFlags(new Flags(bytes.readBoolean()))
43                 .setSubobject(parseSubobjects(bytes.slice()))
44                 .build();
45     }
46
47     @Override
48     public void serializeObject(final Object object, final ByteBuf buffer) {
49         checkArgument(object instanceof Xro, "Wrong instance of PCEPObject. Passed %s. Needed XroObject.",
50             object.getClass());
51         final Xro obj = (Xro) object;
52         final ByteBuf body = Unpooled.buffer();
53         body.writeZero(FLAGS_OFFSET);
54         final Flags flags = obj.getFlags();
55         body.writeBoolean(flags != null && Boolean.TRUE.equals(flags.getFail()));
56         serializeSubobject(obj.getSubobject(), body);
57         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
58     }
59 }