Code clean up
[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.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;
23
24 public class Layer2AttributesExtCom extends AbstractExtendedCommunities {
25     // TODO: TBD BY IANA
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;
36
37     private static short getFlagShort(final BitArray flags, final int index) {
38         short opMode = 0;
39         opMode |= flags.get(index + 1) ? 1 : 0;
40         opMode |= (flags.get(index) ? 1 : 0) << 1;
41         return opMode;
42     }
43
44     @Override
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));
51
52         builder.setModeOfOperation(OperationalMode.forValue(getFlagShort(flags, MODE_OF_OPERATION)));
53         builder.setOperatingPer(NormalizationType.forValue(getFlagShort(flags, NORMALIZATION_TYPE)));
54
55         builder.setL2Mtu(body.readUnsignedShort());
56         body.skipBytes(RESERVED);
57         return new Layer2AttributesExtendedCommunityCaseBuilder()
58                 .setLayer2AttributesExtendedCommunity(builder.build()).build();
59     }
60
61     @Override
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.",
65                 extendedCommunity);
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());
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 }