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