Code clean up
[bgpcep.git] / bgp / 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
40     private static final NodeIdentifier NEXT_HEADER_NID = new NodeIdentifier(NextHeaders.QNAME);
41     private static final NodeIdentifier FLOW_LABEL_NID = new NodeIdentifier(FlowLabel.QNAME);
42
43     private FlowspecIpv6NlriParserHelper() {}
44
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()))
56                     .build());
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());
60         }
61     }
62
63     public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
64         if (value instanceof DestinationIpv6PrefixCase) {
65             buffer.append("to ");
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()));
76         }
77     }
78
79     private static List<NextHeaders> createNextHeaders(final UnkeyedListNode nextHeadersData) {
80         final List<NextHeaders> nextHeaders = new ArrayList<>();
81
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());
93         }
94
95         return nextHeaders;
96     }
97
98     private static List<FlowLabel> createFlowLabels(final UnkeyedListNode flowLabelsData) {
99         final List<FlowLabel> flowLabels = new ArrayList<>();
100
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());
112         }
113
114         return flowLabels;
115     }
116
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());
123             buffer.append(' ');
124             if (isFirst) {
125                 isFirst = false;
126             }
127         }
128         return buffer.toString();
129     }
130 }
131