2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec;
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.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.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.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.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.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.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.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.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.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.attributes.ExtendedCommunities;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.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;
43 public class FSExtendedCommunitiesAttributeParser extends ExtendedCommunitiesAttributeParser {
45 private static final Logger LOG = LoggerFactory.getLogger(FSExtendedCommunitiesAttributeParser.class);
47 private static final short FS_TYPE = 128;
49 private static final short TRAFFIC_RATE_SUBTYPE = 6;
51 private static final short TRAFFIC_ACTION_SUBTYPE = 7;
53 private static final short REDIRECT_SUBTYPE = 8;
55 private static final short TRAFFIC_MARKING_SUBTYPE = 9;
57 private static final int TRAFFIC_RATE_SIZE = 4;
59 private static final int RESERVED = 5;
61 private static final int FLAGS_SIZE = 8;
63 private static final int SAMPLE_BIT = 6;
65 private static final int TERMINAL_BIT = 7;
67 public FSExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
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();
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();
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();
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();
99 throw new BGPDocumentedException("Could not parse Flowspec Extended Community type: " + comm.getCommSubType(), BGPError.OPT_ATTR_ERROR);
103 LOG.debug("Extended community is not from Flowspec, fallback to original communities.");
104 return super.parseExtendedCommunity(refCache, comm, buffer);
106 return comm.setExtendedCommunity(c).build();
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());
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);
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());
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);
135 super.serializeExtendedCommunity(exCommunities, buffer);