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 java.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
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.rev180329.flowspec.destination.FlowspecBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.FlowLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.NextHeaderCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.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.rev180329.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.rev180329.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.rev180329.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(new DestinationIpv6PrefixCaseBuilder()
48 .setDestinationPrefix(new Ipv6Prefix((String) fsType
49 .getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).get().getValue())).build());
50 } else if (fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).isPresent()) {
51 fsBuilder.setFlowspecType(new SourceIpv6PrefixCaseBuilder().setSourcePrefix(new Ipv6Prefix((String) fsType
52 .getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).get().getValue())).build());
53 } else if (fsType.getChild(NEXT_HEADER_NID).isPresent()) {
54 fsBuilder.setFlowspecType(new NextHeaderCaseBuilder()
55 .setNextHeaders(createNextHeaders((UnkeyedListNode) fsType.getChild(NEXT_HEADER_NID).get()))
57 } else if (fsType.getChild(FLOW_LABEL_NID).isPresent()) {
58 fsBuilder.setFlowspecType(new FlowLabelCaseBuilder()
59 .setFlowLabel(createFlowLabels((UnkeyedListNode) fsType.getChild(FLOW_LABEL_NID).get())).build());
63 public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
64 if (value instanceof DestinationIpv6PrefixCase) {
66 buffer.append(((DestinationIpv6PrefixCase) value).getDestinationPrefix().getValue());
67 } else if (value instanceof SourceIpv6PrefixCase) {
68 buffer.append("from ");
69 buffer.append(((SourceIpv6PrefixCase) value).getSourcePrefix().getValue());
70 } else if (value instanceof NextHeaderCase) {
71 buffer.append("where next header ");
72 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((NextHeaderCase) value).getNextHeaders()));
73 } else if (value instanceof FlowLabelCase) {
74 buffer.append("where flow label ");
75 buffer.append(stringFlowLabel(((FlowLabelCase) value).getFlowLabel()));
79 private static List<NextHeaders> createNextHeaders(final UnkeyedListNode nextHeadersData) {
80 final List<NextHeaders> nextHeaders = new ArrayList<>();
82 for (final UnkeyedListEntryNode node : nextHeadersData.getValue()) {
83 final NextHeadersBuilder nextHeadersBuilder = new NextHeadersBuilder();
84 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
85 = node.getChild(AbstractFlowspecNlriParser.OP_NID);
86 opValue.ifPresent(dataContainerChild -> nextHeadersBuilder.setOp(NumericOneByteOperandParser
87 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
88 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
89 = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
90 valueNode.ifPresent(dataContainerChild
91 -> nextHeadersBuilder.setValue((Short) dataContainerChild.getValue()));
92 nextHeaders.add(nextHeadersBuilder.build());
98 private static List<FlowLabel> createFlowLabels(final UnkeyedListNode flowLabelsData) {
99 final List<FlowLabel> flowLabels = new ArrayList<>();
101 for (final UnkeyedListEntryNode node : flowLabelsData.getValue()) {
102 final FlowLabelBuilder flowLabelsBuilder = new FlowLabelBuilder();
103 final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
104 = node.getChild(AbstractFlowspecNlriParser.OP_NID);
105 opValue.ifPresent(dataContainerChild -> flowLabelsBuilder.setOp(NumericOneByteOperandParser
106 .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
107 final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
108 = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
109 valueNode.ifPresent(dataContainerChild
110 -> flowLabelsBuilder.setValue((Long) dataContainerChild.getValue()));
111 flowLabels.add(flowLabelsBuilder.build());
117 private static String stringFlowLabel(final List<FlowLabel> list) {
118 final StringBuilder buffer = new StringBuilder();
119 boolean isFirst = true;
120 for (final FlowLabel item : list) {
121 buffer.append(NumericOneByteOperandParser.INSTANCE.toString(item.getOp(), isFirst));
122 buffer.append(item.getValue());
128 return buffer.toString();