Fix LeafNode conversions
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / ipv4 / FlowspecIpv4NlriParserHelper.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.ipv4;
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.Ipv4Prefix;
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.ipv4.flowspec.flowspec.type.DestinationPrefixCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.DestinationPrefixCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.ProtocolIpCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.ProtocolIpCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.SourcePrefixCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.SourcePrefixCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIps;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv4.flowspec.flowspec.type.protocol.ip._case.ProtocolIpsBuilder;
27 import org.opendaylight.yangtools.yang.common.Uint8;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
34
35 /**
36  * Helper for parsing IPv4 Flowspec.
37  *
38  * @author Kevin Wang
39  */
40 public final class FlowspecIpv4NlriParserHelper {
41     private static final NodeIdentifier PROTOCOL_IP_NID = new NodeIdentifier(ProtocolIps.QNAME);
42
43     private FlowspecIpv4NlriParserHelper() {
44
45     }
46
47     public static void extractFlowspec(final ChoiceNode fsType, final FlowspecBuilder fsBuilder) {
48         if (fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).isPresent()) {
49             fsBuilder.setFlowspecType(new DestinationPrefixCaseBuilder().setDestinationPrefix(
50                     new Ipv4Prefix((String) fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).get()
51                             .getValue())).build());
52         } else if (fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).isPresent()) {
53             fsBuilder.setFlowspecType(new SourcePrefixCaseBuilder().setSourcePrefix(new Ipv4Prefix((String) fsType
54                     .getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).get().getValue())).build());
55         } else if (fsType.getChild(PROTOCOL_IP_NID).isPresent()) {
56             fsBuilder.setFlowspecType(new ProtocolIpCaseBuilder()
57                     .setProtocolIps(createProtocolsIps((UnkeyedListNode) fsType.getChild(PROTOCOL_IP_NID).get()))
58                     .build());
59         }
60     }
61
62     public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
63         if (value instanceof DestinationPrefixCase) {
64             buffer.append("to ");
65             buffer.append(((DestinationPrefixCase) value).getDestinationPrefix().getValue());
66         } else if (value instanceof SourcePrefixCase) {
67             buffer.append("from ");
68             buffer.append(((SourcePrefixCase) value).getSourcePrefix().getValue());
69         } else if (value instanceof ProtocolIpCase) {
70             buffer.append("where IP protocol ");
71             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((ProtocolIpCase) value).getProtocolIps()));
72         }
73     }
74
75     private static List<ProtocolIps> createProtocolsIps(final UnkeyedListNode protocolIpsData) {
76         final List<ProtocolIps> protocolIps = new ArrayList<>();
77
78         for (final UnkeyedListEntryNode node : protocolIpsData.getValue()) {
79             final ProtocolIpsBuilder ipsBuilder = new ProtocolIpsBuilder();
80             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
81                     = node.getChild(AbstractFlowspecNlriParser.OP_NID);
82             opValue.ifPresent(dataContainerChild -> ipsBuilder.setOp(NumericOneByteOperandParser
83                     .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
84             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
85                     = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
86             valueNode.ifPresent(dataContainerChild -> ipsBuilder.setValue((Uint8) dataContainerChild.getValue()));
87             protocolIps.add(ipsBuilder.build());
88         }
89
90         return protocolIps;
91     }
92
93 }
94