99607aa9b9ce2c0d685db19889d0357b59183287
[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.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
33
34 /**
35  * Helper for parsing IPv4 Flowspec.
36  *
37  * @author Kevin Wang
38  */
39 public final class FlowspecIpv4NlriParserHelper {
40     private static final NodeIdentifier PROTOCOL_IP_NID = new NodeIdentifier(ProtocolIps.QNAME);
41
42     private FlowspecIpv4NlriParserHelper() {
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 DestinationPrefixCaseBuilder().setDestinationPrefix(
49                     new Ipv4Prefix((String) fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).get()
50                             .getValue())).build());
51         } else if (fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).isPresent()) {
52             fsBuilder.setFlowspecType(new SourcePrefixCaseBuilder().setSourcePrefix(new Ipv4Prefix((String) fsType
53                     .getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).get().getValue())).build());
54         } else if (fsType.getChild(PROTOCOL_IP_NID).isPresent()) {
55             fsBuilder.setFlowspecType(new ProtocolIpCaseBuilder()
56                     .setProtocolIps(createProtocolsIps((UnkeyedListNode) fsType.getChild(PROTOCOL_IP_NID).get()))
57                     .build());
58         }
59     }
60
61     public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
62         if (value instanceof DestinationPrefixCase) {
63             buffer.append("to ");
64             buffer.append(((DestinationPrefixCase) value).getDestinationPrefix().getValue());
65         } else if (value instanceof SourcePrefixCase) {
66             buffer.append("from ");
67             buffer.append(((SourcePrefixCase) value).getSourcePrefix().getValue());
68         } else if (value instanceof ProtocolIpCase) {
69             buffer.append("where IP protocol ");
70             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((ProtocolIpCase) value).getProtocolIps()));
71         }
72     }
73
74     private static List<ProtocolIps> createProtocolsIps(final UnkeyedListNode protocolIpsData) {
75         final List<ProtocolIps> protocolIps = new ArrayList<>();
76
77         for (final UnkeyedListEntryNode node : protocolIpsData.getValue()) {
78             final ProtocolIpsBuilder ipsBuilder = new ProtocolIpsBuilder();
79             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
80                     = node.getChild(AbstractFlowspecNlriParser.OP_NID);
81             opValue.ifPresent(dataContainerChild -> ipsBuilder.setOp(NumericOneByteOperandParser
82                     .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
83             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
84                     = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
85             valueNode.ifPresent(dataContainerChild -> ipsBuilder.setValue((Short) dataContainerChild.getValue()));
86             protocolIps.add(ipsBuilder.build());
87         }
88
89         return protocolIps;
90     }
91
92 }
93