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