BUG-2794 : refactored code to use BitArray
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSExtendedCommunitiesAttributeParser.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.bgp.flowspec;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
12 import org.opendaylight.protocol.bgp.parser.BGPError;
13 import org.opendaylight.protocol.bgp.parser.impl.message.update.ExtendedCommunitiesAttributeParser;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.protocol.util.ByteBufWriteUtil;
17 import org.opendaylight.protocol.util.ReferenceCache;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.Dscp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.redirect.extended.community._case.RedirectExtendedCommunity;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.redirect.extended.community._case.RedirectExtendedCommunityBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.action.extended.community._case.TrafficActionExtendedCommunity;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.action.extended.community._case.TrafficActionExtendedCommunityBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.marking.extended.community._case.TrafficMarkingExtendedCommunity;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.marking.extended.community._case.TrafficMarkingExtendedCommunityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.rate.extended.community._case.TrafficRateExtendedCommunity;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.rate.extended.community._case.TrafficRateExtendedCommunityBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunitiesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class FSExtendedCommunitiesAttributeParser extends ExtendedCommunitiesAttributeParser {
44
45     private static final Logger LOG = LoggerFactory.getLogger(FSExtendedCommunitiesAttributeParser.class);
46
47     private static final short FS_TYPE = 128;
48
49     private static final short TRAFFIC_RATE_SUBTYPE = 6;
50
51     private static final short TRAFFIC_ACTION_SUBTYPE = 7;
52
53     private static final short REDIRECT_SUBTYPE = 8;
54
55     private static final short TRAFFIC_MARKING_SUBTYPE = 9;
56
57     private static final int TRAFFIC_RATE_SIZE = 4;
58
59     private static final int RESERVED = 5;
60
61     private static final int FLAGS_SIZE = 8;
62
63     private static final int SAMPLE_BIT = 6;
64
65     private static final int TERMINAL_BIT = 7;
66
67     public FSExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
68         super(refCache);
69     }
70
71     @Override
72     public ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) throws BGPDocumentedException {
73         ExtendedCommunity c = null;
74         if (comm.getCommType().equals(FS_TYPE)) {
75             switch (comm.getCommSubType()) {
76             case TRAFFIC_RATE_SUBTYPE:
77                 final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
78                 final Bandwidth value = new Bandwidth(ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE));
79                 c = new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(new TrafficRateExtendedCommunityBuilder().setInformativeAs(as).setLocalAdministrator(value).build()).build();
80                 break;
81             case TRAFFIC_ACTION_SUBTYPE:
82                 buffer.skipBytes(RESERVED);
83                 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
84                 final boolean sample = flags.get(SAMPLE_BIT);
85                 final boolean terminal = flags.get(TERMINAL_BIT);
86                 c = new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(new TrafficActionExtendedCommunityBuilder().setSample(sample).setTerminalAction(terminal).build()).build();
87                 break;
88             case REDIRECT_SUBTYPE:
89                 final ShortAsNumber as1 = new ShortAsNumber((long) buffer.readUnsignedShort());
90                 final byte[] byteValue = ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE);
91                 c = new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(new RedirectExtendedCommunityBuilder().setGlobalAdministrator(as1).setLocalAdministrator(byteValue).build()).build();
92                 break;
93             case TRAFFIC_MARKING_SUBTYPE:
94                 buffer.skipBytes(RESERVED);
95                 final Dscp dscp = new Dscp(buffer.readUnsignedByte());
96                 c = new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(new TrafficMarkingExtendedCommunityBuilder().setGlobalAdministrator(dscp).build()).build();
97                 break;
98             default:
99                 throw new BGPDocumentedException("Could not parse Flowspec Extended Community type: " + comm.getCommSubType(), BGPError.OPT_ATTR_ERROR);
100             }
101         }
102         if (c == null) {
103             LOG.debug("Extended community is not from Flowspec, fallback to original communities.");
104             return super.parseExtendedCommunity(refCache, comm, buffer);
105         }
106         return comm.setExtendedCommunity(c).build();
107     }
108
109     @Override
110     public void serializeExtendedCommunity(final ExtendedCommunities exCommunities, final ByteBuf buffer) {
111         final ExtendedCommunity ex = exCommunities.getExtendedCommunity();
112         if (ex instanceof TrafficRateExtendedCommunityCase) {
113             final TrafficRateExtendedCommunity trafficRate = ((TrafficRateExtendedCommunityCase) ex).getTrafficRateExtendedCommunity();
114             ByteBufWriteUtil.writeShort(trafficRate.getInformativeAs().getValue().shortValue(), buffer);
115             buffer.writeBytes(trafficRate.getLocalAdministrator().getValue());
116         }
117         else if (ex instanceof TrafficActionExtendedCommunityCase) {
118             final TrafficActionExtendedCommunity trafficAction = ((TrafficActionExtendedCommunityCase) ex).getTrafficActionExtendedCommunity();
119             buffer.writeZero(RESERVED);
120             final BitArray flags = new BitArray(FLAGS_SIZE);
121             flags.set(SAMPLE_BIT, trafficAction.isSample());
122             flags.set(TERMINAL_BIT, trafficAction.isTerminalAction());
123             flags.toByteBuf(buffer);
124         }
125         else if (ex instanceof RedirectExtendedCommunityCase) {
126             final RedirectExtendedCommunity redirect = ((RedirectExtendedCommunityCase) ex).getRedirectExtendedCommunity();
127             ByteBufWriteUtil.writeUnsignedShort(redirect.getGlobalAdministrator().getValue().intValue(), buffer);
128             buffer.writeBytes(redirect.getLocalAdministrator());
129         }
130         else if (ex instanceof TrafficMarkingExtendedCommunityCase) {
131             final TrafficMarkingExtendedCommunity trafficMarking = ((TrafficMarkingExtendedCommunityCase) ex).getTrafficMarkingExtendedCommunity();
132             buffer.writeZero(RESERVED);
133             ByteBufWriteUtil.writeUnsignedByte(trafficMarking.getGlobalAdministrator().getValue().shortValue(), buffer);
134         } else {
135             super.serializeExtendedCommunity(exCommunities, buffer);
136         }
137     }
138 }