2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec.ipv6;
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.protocol.bgp.flowspec.AbstractFlowspecNlriParser;
15 import org.opendaylight.protocol.bgp.flowspec.handlers.NumericOneByteOperandParser;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.FlowspecBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.flowspec.FlowspecType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabel;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabelBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.next.header._case.NextHeaders;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.flowspec.destination.group.ipv6.flowspec.flowspec.type.next.header._case.NextHeadersBuilder;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
38 public final class FlowspecIpv6NlriParserHelper {
40 private static final NodeIdentifier NEXT_HEADER_NID = new NodeIdentifier(NextHeaders.QNAME);
41 private static final NodeIdentifier FLOW_LABEL_NID = new NodeIdentifier(FlowLabel.QNAME);
43 private FlowspecIpv6NlriParserHelper() {}
45 public static void extractFlowspec(final ChoiceNode fsType, final FlowspecBuilder fsBuilder) {
46 if (fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).isPresent()) {
47 fsBuilder.setFlowspecType(
48 new DestinationIpv6PrefixCaseBuilder()
49 .setDestinationPrefix(new Ipv6Prefix((String) fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).get().getValue()))
52 } else if (fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).isPresent()) {
53 fsBuilder.setFlowspecType(
54 new SourceIpv6PrefixCaseBuilder()
55 .setSourcePrefix(new Ipv6Prefix((String) fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).get().getValue()))
58 } else if (fsType.getChild(NEXT_HEADER_NID).isPresent()) {
59 fsBuilder.setFlowspecType(new NextHeaderCaseBuilder().setNextHeaders(createNextHeaders((UnkeyedListNode) fsType.getChild(NEXT_HEADER_NID).get())).build());
60 } else if (fsType.getChild(FLOW_LABEL_NID).isPresent()) {
61 fsBuilder.setFlowspecType(new FlowLabelCaseBuilder().setFlowLabel(createFlowLabels((UnkeyedListNode) fsType.getChild(FLOW_LABEL_NID).get())).build());
65 public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
66 if (value instanceof DestinationIpv6PrefixCase) {
68 buffer.append(((DestinationIpv6PrefixCase) value).getDestinationPrefix().getValue());
69 } else if (value instanceof SourceIpv6PrefixCase) {
70 buffer.append("from ");
71 buffer.append(((SourceIpv6PrefixCase) value).getSourcePrefix().getValue());
72 } else if (value instanceof NextHeaderCase) {
73 buffer.append("where next header ");
74 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((NextHeaderCase) value).getNextHeaders()));
75 } else if (value instanceof FlowLabelCase) {
76 buffer.append("where flow label ");
77 buffer.append(stringFlowLabel(((FlowLabelCase) value).getFlowLabel()));
81 private static List<NextHeaders> createNextHeaders(final UnkeyedListNode nextHeadersData) {
82 final List<NextHeaders> nextHeaders = new ArrayList<>();
84 for (final UnkeyedListEntryNode node : nextHeadersData.getValue()) {
85 final NextHeadersBuilder nextHeadersBuilder = new NextHeadersBuilder();
86 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(AbstractFlowspecNlriParser.OP_NID);
87 if (opValue.isPresent()) {
88 nextHeadersBuilder.setOp(NumericOneByteOperandParser.INSTANCE.create((Set<String>) opValue.get().getValue()));
90 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
91 if (valueNode.isPresent()) {
92 nextHeadersBuilder.setValue((Short) valueNode.get().getValue());
94 nextHeaders.add(nextHeadersBuilder.build());
100 private static List<FlowLabel> createFlowLabels(final UnkeyedListNode flowLabelsData) {
101 final List<FlowLabel> flowLabels = new ArrayList<>();
103 for (final UnkeyedListEntryNode node : flowLabelsData.getValue()) {
104 final FlowLabelBuilder flowLabelsBuilder = new FlowLabelBuilder();
105 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(AbstractFlowspecNlriParser.OP_NID);
106 if (opValue.isPresent()) {
107 flowLabelsBuilder.setOp(NumericOneByteOperandParser.INSTANCE.create((Set<String>) opValue.get().getValue()));
109 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
110 if (valueNode.isPresent()) {
111 flowLabelsBuilder.setValue((Long) valueNode.get().getValue());
113 flowLabels.add(flowLabelsBuilder.build());
119 private static String stringFlowLabel(final List<FlowLabel> list) {
120 final StringBuilder buffer = new StringBuilder();
121 boolean isFirst = true;
122 for (final FlowLabel item : list) {
123 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(item.getOp(), isFirst));
124 buffer.append(item.getValue());
130 return buffer.toString();