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
9 package org.opendaylight.protocol.bgp.evpn.impl.extended.communities;
11 import com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.protocol.util.ByteBufWriteUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NormalizationType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.OperationalMode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunity;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunityBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
24 public class Layer2AttributesExtCom extends AbstractExtendedCommunities {
26 private static final int SUBTYPE = 4;
27 private static final int FLAGS_SIZE = 16;
28 private static final int BACKUP_PE_OFFSET = 15;
29 private static final int PRIMARY_PE_OFFSET = 14;
30 private static final int CONTROL_WORD_OFFSET = 13;
31 private static final int MODE_OF_OPERATION = 11;
32 private static final int NORMALIZATION_TYPE = 9;
33 private static final int RESERVED = 2;
34 private static final int THREE_BITS_SHIFT = 3;
35 private static final int FIVE_BITS_SHIFT = 5;
37 private static short getFlagShort(final BitArray flags, final int index) {
39 opMode |= flags.get(index + 1) ? 1 : 0;
40 opMode |= (flags.get(index) ? 1 : 0) << 1;
45 public ExtendedCommunity parseExtendedCommunity(final ByteBuf body) {
46 final Layer2AttributesExtendedCommunityBuilder builder = new Layer2AttributesExtendedCommunityBuilder();
47 final BitArray flags = BitArray.valueOf(body, FLAGS_SIZE);
48 builder.setBackupPe(flags.get(BACKUP_PE_OFFSET));
49 builder.setPrimaryPe(flags.get(PRIMARY_PE_OFFSET));
50 builder.setControlWord(flags.get(CONTROL_WORD_OFFSET));
52 builder.setModeOfOperation(OperationalMode.forValue(getFlagShort(flags, MODE_OF_OPERATION)));
53 builder.setOperatingPer(NormalizationType.forValue(getFlagShort(flags, NORMALIZATION_TYPE)));
55 builder.setL2Mtu(body.readUnsignedShort());
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.isPrimaryPe());
70 flags.set(BACKUP_PE_OFFSET, extCom.isBackupPe());
71 flags.set(CONTROL_WORD_OFFSET, extCom.isControlWord());
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 ByteBufWriteUtil.writeUnsignedShort((int) res[res.length - 1], body);
89 ByteBufWriteUtil.writeUnsignedShort(extCom.getL2Mtu(), body);
90 body.writeZero(RESERVED);
94 public int getSubType() {