Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / EROUnnumberedInterfaceSubobjectParser.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.subobject;
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.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.UnnumberedSubobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
24 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
25
26 /**
27  * Parser for {@link UnnumberedCase}.
28  */
29 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
30     public static final int TYPE = 4;
31
32     private static final int RESERVED = 2;
33     private static final int CONTENT_LENGTH = 10;
34
35     @Override
36     public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
37         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
38         if (buffer.readableBytes() != CONTENT_LENGTH) {
39             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
40                 + "; Expected: " + CONTENT_LENGTH + ".");
41         }
42         buffer.skipBytes(RESERVED);
43         return new SubobjectBuilder()
44                 .setLoose(loose)
45                 .setSubobjectType(new UnnumberedCaseBuilder()
46                     .setUnnumbered(new UnnumberedBuilder()
47                         .setRouterId(ByteBufUtils.readUint32(buffer))
48                         .setInterfaceId(ByteBufUtils.readUint32(buffer))
49                         .build())
50                     .build())
51                 .build();
52     }
53
54     @Override
55     public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
56         checkArgument(subobject.getSubobjectType() instanceof UnnumberedCase,
57             "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
58         final UnnumberedSubobject specObj = ((UnnumberedCase) subobject.getSubobjectType()).getUnnumbered();
59         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
60         body.writeZero(RESERVED);
61         ByteBufUtils.writeMandatory(body, specObj.getRouterId(), "RouterId");
62         ByteBufUtils.writeMandatory(body, specObj.getInterfaceId(), "InterfaceId");
63         EROSubobjectUtil.formatSubobject(TYPE, subobject.getLoose(), body, buffer);
64     }
65 }