BUG 2230: RSVP RRO Subobjects
[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
9 package org.opendaylight.protocol.rsvp.parser.impl.subobject.rro;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.rsvp.parser.impl.te.ProtectionCommonParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.protection.subobject.ProtectionSubobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.object.secondary.record.route.object.subobject.container.subobject.type.BasicProtectionCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.object.secondary.record.route.object.subobject.container.subobject.type.BasicProtectionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.object.secondary.record.route.object.subobject.container.subobject.type.DynamicControlProtectionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.object.secondary.record.route.object.subobject.container.subobject.type.basic.protection._case.BasicProtectionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.secondary.record.route.object.secondary.record.route.object.subobject.container.subobject.type.dynamic.control.protection._case.DynamicControlProtectionBuilder;
26
27 /**
28  * Parser for {@link BasicProtectionCase}
29  */
30 public class SRROBasicProtectionSubobjectParser extends ProtectionCommonParser implements RROSubobjectParser, RROSubobjectSerializer {
31
32     public static final int TYPE = 37;
33     public static final Short CTYPE = 1;
34
35     @Override
36     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
37         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "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, "Unknown subobject instance. Passed %s. Needed UnnumberedCase.", subobject.getSubobjectType().getClass());
57         final ProtectionSubobject protObj = ((BasicProtectionCase) subobject.getSubobjectType()).getBasicProtection()
58             .getProtectionSubobject();
59         final ByteBuf body = Unpooled.buffer();
60         serializeBody(CTYPE, protObj, body);
61         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
62     }
63 }