Code clean up
[bgpcep.git] / bgp / 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  * @author Kevin Wang
36  */
37 public final class FlowspecIpv4NlriParserHelper {
38
39     private static final NodeIdentifier PROTOCOL_IP_NID = new NodeIdentifier(ProtocolIps.QNAME);
40
41     private FlowspecIpv4NlriParserHelper() {}
42
43     public static void extractFlowspec(final ChoiceNode fsType, final FlowspecBuilder fsBuilder) {
44         if (fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).isPresent()) {
45             fsBuilder.setFlowspecType(new DestinationPrefixCaseBuilder().setDestinationPrefix(
46                     new Ipv4Prefix((String) fsType.getChild(AbstractFlowspecNlriParser.DEST_PREFIX_NID).get()
47                             .getValue())).build());
48         } else if (fsType.getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).isPresent()) {
49             fsBuilder.setFlowspecType(new SourcePrefixCaseBuilder().setSourcePrefix(new Ipv4Prefix((String) fsType
50                     .getChild(AbstractFlowspecNlriParser.SOURCE_PREFIX_NID).get().getValue())).build());
51         } else if (fsType.getChild(PROTOCOL_IP_NID).isPresent()) {
52             fsBuilder.setFlowspecType(new ProtocolIpCaseBuilder()
53                     .setProtocolIps(createProtocolsIps((UnkeyedListNode) fsType.getChild(PROTOCOL_IP_NID).get()))
54                     .build());
55         }
56     }
57
58     public static void buildFlowspecString(final FlowspecType value, final StringBuilder buffer) {
59         if (value instanceof DestinationPrefixCase) {
60             buffer.append("to ");
61             buffer.append(((DestinationPrefixCase) value).getDestinationPrefix().getValue());
62         } else if (value instanceof SourcePrefixCase) {
63             buffer.append("from ");
64             buffer.append(((SourcePrefixCase) value).getSourcePrefix().getValue());
65         } else if (value instanceof ProtocolIpCase) {
66             buffer.append("where IP protocol ");
67             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((ProtocolIpCase) value).getProtocolIps()));
68         }
69     }
70
71     private static List<ProtocolIps> createProtocolsIps(final UnkeyedListNode protocolIpsData) {
72         final List<ProtocolIps> protocolIps = new ArrayList<>();
73
74         for (final UnkeyedListEntryNode node : protocolIpsData.getValue()) {
75             final ProtocolIpsBuilder ipsBuilder = new ProtocolIpsBuilder();
76             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue
77                     = node.getChild(AbstractFlowspecNlriParser.OP_NID);
78             opValue.ifPresent(dataContainerChild -> ipsBuilder.setOp(NumericOneByteOperandParser
79                     .INSTANCE.create((Set<String>) dataContainerChild.getValue())));
80             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode
81                     = node.getChild(AbstractFlowspecNlriParser.VALUE_NID);
82             valueNode.ifPresent(dataContainerChild -> ipsBuilder.setValue((Short) dataContainerChild.getValue()));
83             protocolIps.add(ipsBuilder.build());
84         }
85
86         return protocolIps;
87     }
88
89 }
90