2 * Copyright (c) 2016 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.evpn.impl.extended.communities;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.util.BitArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.NormalizationType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.OperationalMode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunityBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.ExtendedCommunity;
21 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
23 public class Layer2AttributesExtCom extends AbstractExtendedCommunities {
25 private static final int SUBTYPE = 4;
26 private static final int FLAGS_SIZE = 16;
27 private static final int BACKUP_PE_OFFSET = 15;
28 private static final int PRIMARY_PE_OFFSET = 14;
29 private static final int CONTROL_WORD_OFFSET = 13;
30 private static final int MODE_OF_OPERATION = 11;
31 private static final int NORMALIZATION_TYPE = 9;
32 private static final int RESERVED = 2;
33 private static final int THREE_BITS_SHIFT = 3;
34 private static final int FIVE_BITS_SHIFT = 5;
36 private static short getFlagShort(final BitArray flags, final int index) {
38 opMode |= flags.get(index + 1) ? 1 : 0;
39 opMode |= (flags.get(index) ? 1 : 0) << 1;
44 public ExtendedCommunity parseExtendedCommunity(final ByteBuf body) {
45 final BitArray flags = BitArray.valueOf(body, FLAGS_SIZE);
47 final Layer2AttributesExtendedCommunityBuilder builder = new Layer2AttributesExtendedCommunityBuilder()
48 .setBackupPe(flags.get(BACKUP_PE_OFFSET))
49 .setPrimaryPe(flags.get(PRIMARY_PE_OFFSET))
50 .setControlWord(flags.get(CONTROL_WORD_OFFSET))
52 .setModeOfOperation(OperationalMode.forValue(getFlagShort(flags, MODE_OF_OPERATION)))
53 .setOperatingPer(NormalizationType.forValue(getFlagShort(flags, NORMALIZATION_TYPE)))
55 .setL2Mtu(ByteBufUtils.readUint16(body));
56 body.skipBytes(RESERVED);
57 return new Layer2AttributesExtendedCommunityCaseBuilder()
58 .setLayer2AttributesExtendedCommunity(builder.build()).build();
62 public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
63 Preconditions.checkArgument(extendedCommunity instanceof Layer2AttributesExtendedCommunityCase,
64 "The extended community %s is not EsImportRouteExtendedCommunityCaseCase type.",
66 final Layer2AttributesExtendedCommunity extCom = ((Layer2AttributesExtendedCommunityCase) extendedCommunity)
67 .getLayer2AttributesExtendedCommunity();
68 final BitArray flags = new BitArray(FLAGS_SIZE);
69 flags.set(PRIMARY_PE_OFFSET, extCom.getPrimaryPe());
70 flags.set(BACKUP_PE_OFFSET, extCom.getBackupPe());
71 flags.set(CONTROL_WORD_OFFSET, extCom.getControlWord());
73 final byte[] res = flags.array();
75 final OperationalMode modeOfOperation = extCom.getModeOfOperation();
76 if (modeOfOperation != null) {
77 aux = UnsignedBytes.checkedCast(modeOfOperation.getIntValue());
78 aux = (byte) (aux << THREE_BITS_SHIFT);
79 res[res.length - 1] = (byte) (res[res.length - 1] | aux);
82 final NormalizationType normalizationType = extCom.getOperatingPer();
83 if (normalizationType != null) {
84 aux = UnsignedBytes.checkedCast(normalizationType.getIntValue());
85 aux = (byte) (aux << FIVE_BITS_SHIFT);
86 res[res.length - 1] = (byte) (res[res.length - 1] | aux);
88 body.writeShort(res[res.length - 1]);
89 ByteBufUtils.writeOrZero(body, extCom.getL2Mtu());
90 body.writeZero(RESERVED);
94 public int getSubType() {