bb47abc6315dba7648012ad168c4fc97f4cf42e4
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / ipv6 / FlowspecIpv6NlriParserHelper.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.ipv6;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
13 import java.util.Set;
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;
37
38 public final class FlowspecIpv6NlriParserHelper {
39     private static final NodeIdentifier NEXT_HEADER_NID = new NodeIdentifier(NextHeaders.QNAME);
40     private static final NodeIdentifier FLOW_LABEL_NID = new NodeIdentifier(FlowLabel.QNAME);
41
42     private FlowspecIpv6NlriParserHelper() {
43
44     }
45
46     public static void extractFlowspec(final ChoiceNode fsType, final FlowspecBuilder fsBuilder) {
47         if (fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).isPresent()) {
48             fsBuilder.setFlowspecType(new DestinationIpv6PrefixCaseBuilder()
49                     .setDestinationPrefix(new Ipv6Prefix((String) fsType
50                             .getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).get().getValue())).build());
51         } else if (fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).isPresent()) {
52             fsBuilder.setFlowspecType(new SourceIpv6PrefixCaseBuilder().setSourcePrefix(new Ipv6Prefix((String) fsType
53                     .getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).get().getValue())).build());
54         } else if (fsType.getChild(NEXT_HEADER_NID).isPresent()) {
55             fsBuilder.setFlowspecType(new NextHeaderCaseBuilder()
56                     .setNextHeaders(createNextHeaders((UnkeyedListNode) fsType.getChild(NEXT_HEADER_NID).get()))
57                     .build());
58         } else if (fsType.getChild(FLOW_LABEL_NID).isPresent()) {
59             fsBuilder.setFlowspecType(new FlowLabelCaseBuilder()
60                     .setFlowLabel(createFlowLabels((UnkeyedListNode) fsType.getChild(FLOW_LABEL_NID).get())).build());
61         }
62     }
63
64     public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
65         if (value instanceof DestinationIpv6PrefixCase) {
66             buffer.append("to ");
67             buffer.append(((DestinationIpv6PrefixCase) value).getDestinationPrefix().getValue());
68         } else if (value instanceof SourceIpv6PrefixCase) {
69             buffer.append("from ");
70             buffer.append(((SourceIpv6PrefixCase) value).getSourcePrefix().getValue());
71         } else if (value instanceof NextHeaderCase) {
72             buffer.append("where next header ");
73             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((NextHeaderCase) value).getNextHeaders()));
74         } else if (value instanceof FlowLabelCase) {
75             buffer.append("where flow label ");
76             buffer.append(stringFlowLabel(((FlowLabelCase) value).getFlowLabel()));
77         }
78     }
79
80     private static List<NextHeaders> createNextHeaders(final UnkeyedListNode nextHeadersData) {
81         final List<NextHeaders> nextHeaders = new ArrayList<>();
82
83         for (final UnkeyedListEntryNode node : nextHeadersData.getValue()) {
84             final NextHeadersBuilder nextHeadersBuilder = new NextHeadersBuilder();
85             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
86                     = node.getChild(AbstractFlowspecNlriParser.OP_NID);
87             opValue.ifPresent(dataContainerChild -> nextHeadersBuilder.setOp(NumericOneByteOperandParser
88                     .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
89             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
90                     = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
91             valueNode.ifPresent(
92                 dataContainerChild -> nextHeadersBuilder.setValue((Short) dataContainerChild.getValue()));
93             nextHeaders.add(nextHeadersBuilder.build());
94         }
95
96         return nextHeaders;
97     }
98
99     private static List<FlowLabel> createFlowLabels(final UnkeyedListNode flowLabelsData) {
100         final List<FlowLabel> flowLabels = new ArrayList<>();
101
102         for (final UnkeyedListEntryNode node : flowLabelsData.getValue()) {
103             final FlowLabelBuilder flowLabelsBuilder = new FlowLabelBuilder();
104             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
105                     = node.getChild(AbstractFlowspecNlriParser.OP_NID);
106             opValue.ifPresent(dataContainerChild -> flowLabelsBuilder.setOp(NumericOneByteOperandParser
107                     .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
108             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
109                     = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
110             valueNode.ifPresent(
111                 dataContainerChild -> flowLabelsBuilder.setValue((Long) dataContainerChild.getValue()));
112             flowLabels.add(flowLabelsBuilder.build());
113         }
114
115         return flowLabels;
116     }
117
118     private static String stringFlowLabel(final List<FlowLabel> list) {
119         final StringBuilder buffer = new StringBuilder();
120         boolean isFirst = true;
121         for (final FlowLabel item : list) {
122             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(item.getOp(), isFirst));
123             buffer.append(item.getValue());
124             buffer.append(' ');
125             if (isFirst) {
126                 isFirst = false;
127             }
128         }
129         return buffer.toString();
130     }
131 }
132