Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSDscpHandler.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 package org.opendaylight.protocol.bgp.flowspec;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
19 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
20 import org.opendaylight.protocol.bgp.flowspec.handlers.Util;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.Dscp;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.NumericOperand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.DscpCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.DscpCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.dscp._case.Dscps;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.dscp._case.DscpsBuilder;
28
29 public final class FSDscpHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
30     static final int DSCP_VALUE = 11;
31
32     private static void serializeDscps(final List<Dscps> dscps, final ByteBuf nlriByteBuf) {
33         for (final Iterator<Dscps> it = dscps.iterator(); it.hasNext(); ) {
34             final Dscps dscp = it.next();
35             NumericOneByteOperandParser.INSTANCE.serialize(dscp.getOp(), 1, !it.hasNext(), nlriByteBuf);
36             Util.writeShortest(dscp.getValue().getValue(), nlriByteBuf);
37         }
38     }
39
40     private static List<Dscps> parseDscps(final ByteBuf nlri) {
41         final List<Dscps> dscps = new ArrayList<>();
42         boolean end = false;
43         // we can do this as all fields will be rewritten in the cycle
44         final DscpsBuilder builder = new DscpsBuilder();
45         while (!end) {
46             final byte b = nlri.readByte();
47             // RFC does not specify operator
48             final NumericOperand op = NumericOneByteOperandParser.INSTANCE.parse(b);
49             builder.setOp(op);
50             builder.setValue(new Dscp(nlri.readUnsignedByte()));
51             end = op.isEndOfList();
52             dscps.add(builder.build());
53         }
54         return dscps;
55     }
56
57     @Override
58     public void serializeType(final FlowspecType fsType, final ByteBuf output) {
59         Preconditions.checkArgument(fsType instanceof DscpCase, "DscpCase class is mandatory!");
60         output.writeByte(DSCP_VALUE);
61         serializeDscps(((DscpCase) fsType).getDscps(), output);
62     }
63
64     @Override
65     public FlowspecType parseType(final ByteBuf buffer) {
66         requireNonNull(buffer, "input buffer is null, missing data to parse.");
67         return new DscpCaseBuilder().setDscps(parseDscps(buffer)).build();
68     }
69 }