Code clean up
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / TrafficActionEcHandler.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.BitArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.action.extended.community.TrafficActionExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.action.extended.community.TrafficActionExtendedCommunityBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
22
23 public class TrafficActionEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
24
25     private static final int TYPE = 128;
26
27     private static final int SUBTYPE = 7;
28
29     private static final int RESERVED = 5;
30
31     private static final int FLAGS_SIZE = 8;
32
33     private static final int SAMPLE_BIT = 6;
34
35     private static final int TERMINAL_BIT = 7;
36
37     @Override
38     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
39         Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.TrafficActionExtendedCommunity,
40                 "The extended community %s is not TrafficActionExtendedCommunityCase type.", extendedCommunity);
41         final TrafficActionExtendedCommunity trafficAction = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.TrafficActionExtendedCommunity) extendedCommunity).getTrafficActionExtendedCommunity();
42         byteAggregator.writeZero(RESERVED);
43         final BitArray flags = new BitArray(FLAGS_SIZE);
44         flags.set(SAMPLE_BIT, trafficAction.isSample());
45         flags.set(TERMINAL_BIT, trafficAction.isTerminalAction());
46         flags.toByteBuf(byteAggregator);
47     }
48
49     @Override
50     public int getType(final boolean isTransitive) {
51         //traffic-action is transitive only
52         return TYPE;
53     }
54
55     @Override
56     public int getSubType() {
57         return SUBTYPE;
58     }
59
60     @Override
61     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
62         buffer.skipBytes(RESERVED);
63         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
64         final boolean sample = flags.get(SAMPLE_BIT);
65         final boolean terminal = flags.get(TERMINAL_BIT);
66         return new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
67                 new TrafficActionExtendedCommunityBuilder()
68                     .setSample(sample)
69                     .setTerminalAction(terminal)
70                     .build()).build();
71     }
72
73 }