Bug 5503 - remove package cyclic dependency in BGP-FS
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / SimpleFlowspecIpv6NlriParser.java
1 /*
2  * Copyright (c) 2015 Cisco 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;
9
10 import com.google.common.base.Optional;
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
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.rev100924.Ipv6Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.Flowspec;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.FlowspecBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.flowspec.FlowspecType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.DestinationIpv6PrefixCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.FlowLabelCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.FlowLabelCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.NextHeaderCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.NextHeaderCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabel;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.flow.label._case.FlowLabelBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.next.header._case.NextHeaders;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.destination.ipv6.flowspec.flowspec.type.next.header._case.NextHeadersBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecIpv6Case;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
44
45 public final class SimpleFlowspecIpv6NlriParser extends AbstractFlowspecNlriParser {
46
47     static final NodeIdentifier NEXT_HEADER_NID = new NodeIdentifier(NextHeaders.QNAME);
48     static final NodeIdentifier FLOW_LABEL_NID = new NodeIdentifier(FlowLabel.QNAME);
49
50     public SimpleFlowspecIpv6NlriParser(SimpleFlowspecTypeRegistry flowspecTypeRegistry) {
51         this.flowspecTypeRegistry = flowspecTypeRegistry;
52     }
53
54     @Override
55     DestinationType createWithdrawnDestinationType(final List<Flowspec> dst) {
56         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationFlowspecIpv6CaseBuilder().setDestinationFlowspec(
57             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.flowspec.ipv6._case.DestinationFlowspecBuilder().setFlowspec(
58                 dst).build()).build();
59     }
60
61     @Override
62     DestinationType createAdvertizedRoutesDestinationType(final List<Flowspec> dst) {
63         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecIpv6CaseBuilder()
64             .setDestinationFlowspec(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.flowspec.ipv6._case.DestinationFlowspecBuilder()
65                 .setFlowspec(dst).build()).build();
66     }
67
68     @Override
69     protected void serializeMpReachNlri(final Attributes1 pathAttributes, final ByteBuf byteAggregator) {
70         if (pathAttributes == null) {
71             return;
72         }
73         final AdvertizedRoutes routes = (pathAttributes.getMpReachNlri()).getAdvertizedRoutes();
74         if (routes != null && routes.getDestinationType() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecIpv6Case) {
75             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecIpv6Case flowspecCase = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationFlowspecIpv6Case) routes.getDestinationType();
76             serializeNlri(flowspecCase.getDestinationFlowspec().getFlowspec(), byteAggregator);
77         }
78     }
79
80     @Override
81     public void extractSpecificFlowspec(final ChoiceNode fsType, final FlowspecBuilder fsBuilder) {
82         if (fsType.getChild(DEST_PREFIX_NID).isPresent()) {
83             fsBuilder.setFlowspecType(new DestinationIpv6PrefixCaseBuilder()
84                 .setDestinationPrefix(new Ipv6Prefix((String) fsType.getChild(DEST_PREFIX_NID).get().getValue()))
85                 .build());
86         } else if (fsType.getChild(SOURCE_PREFIX_NID).isPresent()) {
87             fsBuilder.setFlowspecType(new SourceIpv6PrefixCaseBuilder()
88                 .setSourcePrefix(new Ipv6Prefix((String) fsType.getChild(SOURCE_PREFIX_NID).get().getValue()))
89                 .build());
90         } else if (fsType.getChild(NEXT_HEADER_NID).isPresent()) {
91             fsBuilder.setFlowspecType(new NextHeaderCaseBuilder().setNextHeaders(createNextHeaders((UnkeyedListNode) fsType.getChild(NEXT_HEADER_NID).get())).build());
92         } else if (fsType.getChild(FLOW_LABEL_NID).isPresent()) {
93             fsBuilder.setFlowspecType(new FlowLabelCaseBuilder().setFlowLabel(createFlowLabels((UnkeyedListNode) fsType.getChild(FLOW_LABEL_NID).get())).build());
94         }
95     }
96
97     private List<NextHeaders> createNextHeaders(final UnkeyedListNode nextHeadersData) {
98         final List<NextHeaders> nextHeaders = new ArrayList<>();
99
100         for (final UnkeyedListEntryNode node : nextHeadersData.getValue()) {
101             final NextHeadersBuilder nextHeadersBuilder = new NextHeadersBuilder();
102             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
103             if (opValue.isPresent()) {
104                 nextHeadersBuilder.setOp(NumericOneByteOperandParser.INSTANCE.create((Set<String>) opValue.get().getValue()));
105             }
106             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
107             if (valueNode.isPresent()) {
108                 nextHeadersBuilder.setValue((Short) valueNode.get().getValue());
109             }
110             nextHeaders.add(nextHeadersBuilder.build());
111         }
112
113         return nextHeaders;
114     }
115
116     private List<FlowLabel> createFlowLabels(final UnkeyedListNode flowLabelsData) {
117         final List<FlowLabel> flowLabels = new ArrayList<>();
118
119         for (final UnkeyedListEntryNode node : flowLabelsData.getValue()) {
120             final FlowLabelBuilder flowLabelsBuilder = new FlowLabelBuilder();
121             final Optional<DataContainerChild<? extends PathArgument, ?>> opValue = node.getChild(OP_NID);
122             if (opValue.isPresent()) {
123                 flowLabelsBuilder.setOp(NumericOneByteOperandParser.INSTANCE.create((Set<String>) opValue.get().getValue()));
124             }
125             final Optional<DataContainerChild<? extends PathArgument, ?>> valueNode = node.getChild(VALUE_NID);
126             if (valueNode.isPresent()) {
127                 flowLabelsBuilder.setValue((Long) valueNode.get().getValue());
128             }
129             flowLabels.add(flowLabelsBuilder.build());
130         }
131
132         return flowLabels;
133     }
134
135     @Override
136     protected void stringSpecificFSNlriType(final FlowspecType value, final StringBuilder buffer) {
137         if (value instanceof DestinationIpv6PrefixCase) {
138             buffer.append("to ");
139             buffer.append(((DestinationIpv6PrefixCase) value).getDestinationPrefix().getValue());
140         } else if (value instanceof SourceIpv6PrefixCase) {
141             buffer.append("from ");
142             buffer.append(((SourceIpv6PrefixCase) value).getSourcePrefix().getValue());
143         } else if (value instanceof NextHeaderCase) {
144             buffer.append("where next header ");
145             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(((NextHeaderCase) value).getNextHeaders()));
146         } else if (value instanceof FlowLabelCase) {
147             buffer.append("where flow label ");
148             buffer.append(stringFlowLabel(((FlowLabelCase) value).getFlowLabel()));
149         }
150     }
151
152     private static String stringFlowLabel(final List<FlowLabel> list) {
153         final StringBuilder buffer = new StringBuilder();
154         boolean isFirst = true;
155         for (final FlowLabel item : list) {
156             buffer.append(NumericOneByteOperandParser.INSTANCE.toString(item.getOp(), isFirst));
157             buffer.append(item.getValue());
158             buffer.append(' ');
159             if (isFirst) {
160                 isFirst = false;
161             }
162         }
163         return buffer.toString();
164     }
165
166     @Override
167     protected void serializeMpUnreachNlri(final Attributes2 pathAttributes, final ByteBuf byteAggregator) {
168         if (pathAttributes == null) {
169             return;
170         }
171         final MpUnreachNlri mpUnreachNlri = pathAttributes.getMpUnreachNlri();
172         if (mpUnreachNlri.getWithdrawnRoutes() != null && mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationFlowspecIpv6Case) {
173             final DestinationFlowspecIpv6Case flowspecCase = (DestinationFlowspecIpv6Case) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
174             serializeNlri(flowspecCase.getDestinationFlowspec().getFlowspec(), byteAggregator);
175         }
176     }
177 }