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