remove unnecesary boxing / unboxing
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / TrafficMarkingEcHandler.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.bgp.flowspec.extended.communities;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
16 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
17 import org.opendaylight.protocol.util.ByteBufWriteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.Dscp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunity;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.traffic.marking.extended.community.TrafficMarkingExtendedCommunityBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
23
24 public class TrafficMarkingEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
25
26     private static final int TYPE = 128;
27
28     private static final int SUBTYPE = 9;
29
30     private static final int RESERVED = 5;
31
32     @Override
33     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
34         Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficMarkingExtendedCommunity,
35                 "The extended community %s is not TrafficMarkingExtendedCommunity type.", extendedCommunity);
36         final TrafficMarkingExtendedCommunity trafficMarking = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.TrafficMarkingExtendedCommunity) extendedCommunity).getTrafficMarkingExtendedCommunity();
37         byteAggregator.writeZero(RESERVED);
38         ByteBufWriteUtil.writeUnsignedByte(trafficMarking.getGlobalAdministrator().getValue(), byteAggregator);
39     }
40
41     @Override
42     public int getType(final boolean isTransitive) {
43         //traffic-rate is transitive only
44         return TYPE;
45     }
46
47     @Override
48     public int getSubType() {
49         return SUBTYPE;
50     }
51
52     @Override
53     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
54         buffer.skipBytes(RESERVED);
55         final Dscp dscp = new Dscp(buffer.readUnsignedByte());
56         return new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(
57                 new TrafficMarkingExtendedCommunityBuilder()
58                     .setGlobalAdministrator(dscp).build()).build();
59     }
60
61 }