599d450d278e6bc134edaaa6650b68dfc8e82f84
[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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev170917.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv4.route.VppIpv4Route;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev170917.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv4.route.next.hop.options.next.hop.list.next.hop.list.NextHop;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev170917.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv6.route.VppIpv6Route;
32
33 import javax.annotation.Nonnull;
34
35
36 /**
37  * Factory for creating requests to create route with multiple hops
38  */
39 public class MultipathHopRequestFactory extends BasicHopRequestFactory implements RouteRequestProducer {
40
41     private MultipathHopRequestFactory(final VppClassifierContextManager classifierContextManager,
42                                        final NamingContext interfaceContext,
43                                        final NamingContext routingProtocolContext) {
44         super(classifierContextManager, interfaceContext, routingProtocolContext);
45     }
46
47     public static MultipathHopRequestFactory forContexts(
48             @Nonnull final VppClassifierContextManager classifierContextManager,
49             @Nonnull final NamingContext interfaceContext,
50             @Nonnull final NamingContext routingProtocolContext) {
51         return new MultipathHopRequestFactory(classifierContextManager, interfaceContext, routingProtocolContext);
52     }
53
54     public IpAddDelRoute createIpv4MultipathHopRequest(final boolean add,
55                                                        @Nonnull final String parentProtocolName,
56                                                        @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev170917.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv4.Route route,
57                                                        @Nonnull final NextHop hop,
58                                                        @Nonnull final MappingContext mappingContext) {
59         final VppIpv4Route routingAttributes = route.getVppIpv4Route();
60
61         final int nextHopInterfaceIndex =
62                 getInterfaceNamingContext().getIndex(hop.getOutgoingInterface(), mappingContext);
63
64         if (routingAttributes != null && classifyTablePresent(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
65                 mappingContext)) {
66             return getMultipathHopRequest(add,
67                     route.getDestinationPrefix(),
68                     nextHopInterfaceIndex,
69                     hop.getAddress(),
70                     toByte(hop.getWeight()),
71                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
72                     DEFAULT_VNI,
73                     classifyTableIndex(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
74                             mappingContext),
75                     true);
76         } else {
77             return getMultipathHopRequest(add,
78                     route.getDestinationPrefix(),
79                     nextHopInterfaceIndex,
80                     hop.getAddress(),
81                     toByte(hop.getWeight()),
82                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
83                     DEFAULT_VNI,
84                     0,
85                     false);
86         }
87     }
88
89     public IpAddDelRoute createIpv6MultipathHopRequest(final boolean add,
90                                                        @Nonnull final String parentProtocolName,
91                                                        @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev170917.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv6.Route route,
92                                                        @Nonnull final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev170917.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv6.route.next.hop.options.next.hop.list.next.hop.list.NextHop hop,
93                                                        @Nonnull final MappingContext mappingContext) {
94         final VppIpv6Route routingAttributes = route.getVppIpv6Route();
95
96         final int nextHopInterfaceIndex =
97                 getInterfaceNamingContext().getIndex(hop.getOutgoingInterface(), mappingContext);
98
99         if (routingAttributes != null && classifyTablePresent(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
100                 mappingContext)) {
101             return getMultipathHopRequest(add,
102                     route.getDestinationPrefix(),
103                     nextHopInterfaceIndex,
104                     hop.getAddress(),
105                     toByte(hop.getWeight()),
106                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
107                     DEFAULT_VNI,
108                     classifyTableIndex(routingAttributes.getClassifyTable(), getVppClassifierContextManager(),
109                             mappingContext),
110                     true);
111         } else {
112             return getMultipathHopRequest(add,
113                     route.getDestinationPrefix(),
114                     nextHopInterfaceIndex,
115                     hop.getAddress(),
116                     toByte(hop.getWeight()),
117                     getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext),
118                     DEFAULT_VNI,
119                     0,
120                     false);
121         }
122     }
123
124
125     private IpAddDelRoute getMultipathHopRequest(final boolean isAdd, @Nonnull final Ipv6Prefix destinationAddress,
126                                                  final int nextHopInterfaceIndex,
127                                                  @Nonnull final Ipv6Address nextHopAddress,
128                                                  final byte nextHopWeight,
129                                                  final int primaryVrf, final int secondaryVrf,
130                                                  final int classifyTableIndex, final boolean classifyIndexSet) {
131         return flaglessAddDelRouteRequest(booleanToByte(isAdd), nextHopInterfaceIndex,
132                 ipv6AddressNoZoneToArray(nextHopAddress), nextHopWeight, toByte(1),
133                 ipv6AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), toByte(1),
134                 primaryVrf, secondaryVrf, classifyTableIndex,
135                 booleanToByte(classifyIndexSet));
136     }
137
138     private IpAddDelRoute getMultipathHopRequest(final boolean isAdd, @Nonnull final Ipv4Prefix destinationAddress,
139                                                  final int nextHopInterfaceIndex,
140                                                  @Nonnull final Ipv4Address nextHopAddress,
141                                                  final byte nextHopWeight,
142                                                  final int primaryVrf, final int secondaryVrf,
143                                                  final int classifyTableIndex, final boolean classifyIndexSet) {
144         return flaglessAddDelRouteRequest(booleanToByte(isAdd), nextHopInterfaceIndex,
145                 ipv4AddressNoZoneToArray(nextHopAddress.getValue()), nextHopWeight, toByte(0),
146                 ipv4AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), toByte(1),
147                 primaryVrf, secondaryVrf, classifyTableIndex,
148                 booleanToByte(classifyIndexSet));
149     }
150
151
152 }