Code Clean Up
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / segment / routing / SrRroSubobjectParser.java
1 /*
2  * Copyright (c) 2014 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.pcep.segment.routing;
10
11 import com.google.common.base.Preconditions;
12
13 import io.netty.buffer.ByteBuf;
14
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.SrSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.add.lsp.input.arguments.rro.subobject.subobject.type.SrRroTypeBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
23
24 public class SrRroSubobjectParser extends AbstractSrSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
25
26     private static final int LEGACY_TYPE = 6;
27     private static final int PROPOSED_TYPE = 36;
28
29     private final int type;
30
31     SrRroSubobjectParser(final boolean isIanaAssignedType) {
32         this.type = (isIanaAssignedType) ? PROPOSED_TYPE : LEGACY_TYPE;
33     }
34     @Override
35     public void serializeSubobject(Subobject subobject, ByteBuf buffer) {
36         Preconditions.checkArgument(subobject.getSubobjectType() instanceof SrSubobject,
37                 "Unknown subobject instance. Passed %s. Needed SrSubobject.", subobject.getSubobjectType()
38                         .getClass());
39         final SrSubobject srSubobject = (SrSubobject) subobject.getSubobjectType();
40         final ByteBuf body = serializeSubobject(srSubobject);
41         RROSubobjectUtil.formatSubobject(this.type, body, buffer);
42     }
43
44     @Override
45     public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
46         final SrRroTypeBuilder srRroSubobjectBuilder = new SrRroTypeBuilder(parseSrSubobject(buffer));
47         final SubobjectBuilder subobjectBuilder = new SubobjectBuilder();
48         subobjectBuilder.setSubobjectType(srRroSubobjectBuilder.build());
49         return subobjectBuilder.build();
50     }
51
52     public int getCodePoint() {
53         return this.type;
54     }
55 }