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