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