bbc54120eab5a300681a71c5809dd9ee9a9b7430
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / extended / communities / Layer2AttributesExtCom.java
1 /*
2  * Copyright (c) 2016 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.evpn.impl.extended.communities;
10
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.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.util.BitArray;
17 import org.opendaylight.protocol.util.ByteBufWriteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.NormalizationType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.OperationalMode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunity;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunityBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
25
26 public class Layer2AttributesExtCom extends AbstractExtendedCommunities {
27     // TODO: TBD BY IANA
28     private static final int SUBTYPE = 4;
29     private static final int FLAGS_SIZE = 16;
30     private static final int BACKUP_PE_OFFSET = 15;
31     private static final int PRIMARY_PE_OFFSET = 14;
32     private static final int CONTROL_WORD_OFFSET = 13;
33     private static final int MODE_OF_OPERATION = 11;
34     private static final int NORMALIZATION_TYPE = 9;
35     private static final int RESERVED = 2;
36     private static final int THREE_BITS_SHIFT = 3;
37     private static final int FIVE_BITS_SHIFT = 5;
38
39     @Override
40     public ExtendedCommunity parseExtendedCommunity(final ByteBuf body) throws BGPDocumentedException, BGPParsingException {
41         final Layer2AttributesExtendedCommunityBuilder builder = new Layer2AttributesExtendedCommunityBuilder();
42         final BitArray flags = BitArray.valueOf(body, FLAGS_SIZE);
43         builder.setBackupPe(flags.get(BACKUP_PE_OFFSET));
44         builder.setPrimaryPe(flags.get(PRIMARY_PE_OFFSET));
45         builder.setControlWord(flags.get(CONTROL_WORD_OFFSET));
46
47         builder.setModeOfOperation(OperationalMode.forValue(getFlagShort(flags, MODE_OF_OPERATION)));
48         builder.setOperatingPer(NormalizationType.forValue(getFlagShort(flags, NORMALIZATION_TYPE)));
49
50         builder.setL2Mtu(body.readUnsignedShort());
51         body.skipBytes(RESERVED);
52         return new Layer2AttributesExtendedCommunityCaseBuilder()
53             .setLayer2AttributesExtendedCommunity(builder.build()).build();
54     }
55
56     private static short getFlagShort(final BitArray flags, final int index) {
57         short opMode= 0;
58         opMode |= flags.get(index + 1) ? 1 : 0;
59         opMode |= (flags.get(index) ? 1 : 0) << 1;
60         return opMode;
61     }
62
63     @Override
64     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
65         Preconditions.checkArgument(extendedCommunity instanceof Layer2AttributesExtendedCommunityCase,
66             "The extended community %s is not EsImportRouteExtendedCommunityCaseCase type.", extendedCommunity);
67         final Layer2AttributesExtendedCommunity extCom = ((Layer2AttributesExtendedCommunityCase) extendedCommunity).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());
72
73         final byte[] res = flags.array();
74         byte aux = 0;
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);
80         }
81
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);
87         }
88         ByteBufWriteUtil.writeUnsignedShort((int) res[res.length - 1], body);
89         ByteBufWriteUtil.writeUnsignedShort(extCom.getL2Mtu(), body);
90         body.writeZero(RESERVED);
91     }
92
93     @Override
94     public int getSubType() {
95         return SUBTYPE;
96     }
97 }