8f3d65d0f1a7e094734879781cde429a320137a5
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.hc2vpp.routing.write.factory;
18
19 import io.fd.hc2vpp.common.translate.util.NamingContext;
20 import io.fd.hc2vpp.routing.write.factory.base.BasicHopRequestFactory;
21 import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
22 import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
23 import io.fd.honeycomb.translate.MappingContext;
24 import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv4.unicast.routing.rev180319.VppIpv4NextHopAugmentation;
27 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv4.unicast.routing.rev180319.VppIpv4RouteAttributesAugmentation;
28 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv4.unicast.routing.rev180319.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv4.route.VppIpv4Route;
29 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv6.unicast.routing.rev180319.VppIpv6NextHopAugmentation;
30 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv6.unicast.routing.rev180319.VppIpv6RouteAttributesAugmentation;
31 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv6.unicast.routing.rev180319.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv6.route.VppIpv6Route;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev180313.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv4.Route;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev180313.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv4.route.next.hop.NextHop1;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.next.hop.content.next.hop.options.next.hop.list.next.hop.list.NextHop;
39
40 /**
41  * Factory for creating requests to create route with multiple hops
42  */
43 public class MultipathHopRequestFactory extends BasicHopRequestFactory implements RouteRequestProducer {
44
45     private MultipathHopRequestFactory(final VppClassifierContextManager classifierContextManager,
46                                        final NamingContext interfaceContext,
47                                        final NamingContext routingProtocolContext) {
48         super(classifierContextManager, interfaceContext, routingProtocolContext);
49     }
50
51     public static MultipathHopRequestFactory forContexts(
52             @Nonnull final VppClassifierContextManager classifierContextManager,
53             @Nonnull final NamingContext interfaceContext,
54             @Nonnull final NamingContext routingProtocolContext) {
55         return new MultipathHopRequestFactory(classifierContextManager, interfaceContext, routingProtocolContext);
56     }
57
58     public IpAddDelRoute createIpv4MultipathHopRequest(final boolean add,
59                                                        @Nonnull final String parentProtocolName,
60                                                        @Nonnull final Route route,
61                                                        @Nonnull final NextHop hop,
62                                                        @Nonnull final MappingContext mappingContext) {
63
64         final VppIpv4Route routingAttributes = route.augmentation(VppIpv4RouteAttributesAugmentation.class) != null ?
65             route.augmentation(VppIpv4RouteAttributesAugmentation.class).getVppIpv4Route() : null;
66
67         final int nextHopInterfaceIndex =
68                 getInterfaceNamingContext().getIndex(hop.getOutgoingInterface(), mappingContext);
69
70         if (routingAttributes != null && classifyTablePresent(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
71                 mappingContext)) {
72             return getMultipathHopRequest(add,
73                     route.getDestinationPrefix(),
74                     nextHopInterfaceIndex,
75                     hop.augmentation(NextHop1.class).getNextHopAddress(),
76                     hop.augmentation(VppIpv4NextHopAugmentation.class).getWeight().byteValue(),
77                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
78                     DEFAULT_VNI,
79                     classifyTableIndex(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
80                             mappingContext),
81                     true);
82         } else {
83             return getMultipathHopRequest(add,
84                     route.getDestinationPrefix(),
85                     nextHopInterfaceIndex,
86                     hop.augmentation(NextHop1.class).getNextHopAddress(),
87                     hop.augmentation(VppIpv4NextHopAugmentation.class).getWeight().byteValue(),
88                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
89                     DEFAULT_VNI,
90                     0,
91                     false);
92         }
93     }
94
95     public IpAddDelRoute createIpv6MultipathHopRequest(final boolean add,
96                                                        @Nonnull final String parentProtocolName,
97                                                        @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev180313.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv6.Route route,
98                                                        @Nonnull final NextHop hop,
99                                                        @Nonnull final MappingContext mappingContext) {
100         final VppIpv6Route routingAttributes = route.augmentation(VppIpv6RouteAttributesAugmentation.class) != null ?
101             route.augmentation(VppIpv6RouteAttributesAugmentation.class).getVppIpv6Route() : null;
102
103         final int nextHopInterfaceIndex =
104                 getInterfaceNamingContext().getIndex(hop.getOutgoingInterface(), mappingContext);
105
106         if (routingAttributes != null && classifyTablePresent(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
107                 mappingContext)) {
108             return getMultipathHopRequest(add,
109                     route.getDestinationPrefix(),
110                     nextHopInterfaceIndex,
111                     hop.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev180313.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv6.route.next.hop.NextHop1.class).getNextHopAddress(),
112                     hop.augmentation(VppIpv6NextHopAugmentation.class).getWeight().byteValue(),
113                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
114                     DEFAULT_VNI,
115                     classifyTableIndex(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
116                             mappingContext),
117                     true);
118         } else {
119             return getMultipathHopRequest(add,
120                     route.getDestinationPrefix(),
121                     nextHopInterfaceIndex,
122                     hop.augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev180313.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv6.route.next.hop.NextHop1.class).getNextHopAddress(),
123                     hop.augmentation(VppIpv6NextHopAugmentation.class).getWeight().byteValue(),
124                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
125                     DEFAULT_VNI,
126                     0,
127                     false);
128         }
129     }
130
131
132     private IpAddDelRoute getMultipathHopRequest(final boolean isAdd, @Nonnull final Ipv6Prefix destinationAddress,
133                                                  final int nextHopInterfaceIndex,
134                                                  @Nonnull final Ipv6Address nextHopAddress,
135                                                  final byte nextHopWeight,
136                                                  final int primaryVrf, final int secondaryVrf,
137                                                  final int classifyTableIndex, final boolean classifyIndexSet) {
138         return flaglessAddDelRouteRequest(booleanToByte(isAdd), nextHopInterfaceIndex,
139                 ipv6AddressNoZoneToArray(nextHopAddress), nextHopWeight, (byte)1,
140                 ipv6AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), (byte)1,
141                 primaryVrf, secondaryVrf, classifyTableIndex,
142                 booleanToByte(classifyIndexSet));
143     }
144
145     private IpAddDelRoute getMultipathHopRequest(final boolean isAdd, @Nonnull final Ipv4Prefix destinationAddress,
146                                                  final int nextHopInterfaceIndex,
147                                                  @Nonnull final Ipv4Address nextHopAddress,
148                                                  final byte nextHopWeight,
149                                                  final int primaryVrf, final int secondaryVrf,
150                                                  final int classifyTableIndex, final boolean classifyIndexSet) {
151         return flaglessAddDelRouteRequest(booleanToByte(isAdd), nextHopInterfaceIndex,
152                 ipv4AddressNoZoneToArray(nextHopAddress.getValue()), nextHopWeight, (byte)0,
153                 ipv4AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), (byte)1,
154                 primaryVrf, secondaryVrf, classifyTableIndex,
155                 booleanToByte(classifyIndexSet));
156     }
157
158
159 }