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