Code clean up
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / RedirectAsTwoOctetEcHandler.java
1 /*
2  * Copyright (c) 2015 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.flowspec.extended.communities;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
16 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.protocol.util.ByteBufWriteUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.extended.community.RedirectExtendedCommunity;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.extended.community.RedirectExtendedCommunityBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ShortAsNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
24
25 public class RedirectAsTwoOctetEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
26
27     private static final int TYPE = 128;
28
29     private static final int SUBTYPE = 8;
30
31     private static final int TRAFFIC_RATE_SIZE = 4;
32
33     @Override
34     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
35         Preconditions.checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.RedirectExtendedCommunity,
36                 "The extended community %s is not RedirectExtendedCommunityCase type.", extendedCommunity);
37         final RedirectExtendedCommunity redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.RedirectExtendedCommunity) extendedCommunity).getRedirectExtendedCommunity();
38         ByteBufWriteUtil.writeUnsignedShort(redirect.getGlobalAdministrator().getValue().intValue(), byteAggregator);
39         byteAggregator.writeBytes(redirect.getLocalAdministrator());
40     }
41
42     @Override
43     public int getType(final boolean isTransitive) {
44         //redirect is transitive only
45         return TYPE;
46     }
47
48     @Override
49     public int getSubType() {
50         return SUBTYPE;
51     }
52
53     @Override
54     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
55         final ShortAsNumber as1 = new ShortAsNumber((long) buffer.readUnsignedShort());
56         final byte[] byteValue = ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE);
57         return new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(
58                 new RedirectExtendedCommunityBuilder()
59                     .setGlobalAdministrator(as1)
60                     .setLocalAdministrator(byteValue)
61                     .build()).build();
62     }
63 }