Fixup ProtectionCommonParser.serializeBody()
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / rro / SRROBasicProtectionSubobjectParser.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.rro;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.rsvp.parser.impl.te.ProtectionCommonParser;
14 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
16 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.subobject.ProtectionSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.subobjects.subobject.type.BasicProtectionCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.subobjects.subobject.type.BasicProtectionCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.subobjects.subobject.type.DynamicControlProtectionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.subobjects.subobject.type.basic.protection._case.BasicProtectionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.subobjects.subobject.type.dynamic.control.protection._case.DynamicControlProtectionBuilder;
25
26 /**
27  * Parser for {@link BasicProtectionCase}.
28  */
29 public class SRROBasicProtectionSubobjectParser extends ProtectionCommonParser
30         implements RROSubobjectParser, RROSubobjectSerializer {
31     public static final int TYPE = 37;
32     public static final short CTYPE = 1;
33
34     @Override
35     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
36         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
37             "Array of bytes is mandatory. Can't be null or empty.");
38         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
39         //skip reserved
40         buffer.readByte();
41         final short cType = buffer.readUnsignedByte();
42         final ProtectionSubobject prot = parseCommonProtectionBody(cType, buffer);
43         if (cType == CTYPE) {
44             builder.setSubobjectType(new BasicProtectionCaseBuilder().setBasicProtection(new BasicProtectionBuilder()
45                 .setProtectionSubobject(prot).build()).build());
46         } else if (cType == SRRODynamicProtectionSubobjectParser.CTYPE) {
47             builder.setSubobjectType(new DynamicControlProtectionCaseBuilder().setDynamicControlProtection(new
48                 DynamicControlProtectionBuilder().setProtectionSubobject(prot).build()).build());
49         }
50         return builder.build();
51     }
52
53
54     @Override
55     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
56         Preconditions.checkArgument(subobject.getSubobjectType() instanceof BasicProtectionCase,
57             "Unknown subobject instance. Passed %s. Needed UnnumberedCase.",
58             subobject.getSubobjectType().getClass());
59         final ProtectionSubobject protObj = ((BasicProtectionCase) subobject.getSubobjectType()).getBasicProtection()
60             .getProtectionSubobject();
61         final ByteBuf body = Unpooled.buffer();
62         serializeBody(CTYPE, protObj, body);
63         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
64     }
65 }