2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.hc2vpp.routing.write.factory;
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 java.util.Optional;
26 import javax.annotation.Nonnull;
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.Ipv6Prefix;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.next.hop.content.NextHopOptions;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.next.hop.content.next.hop.options.TableLookupCase;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.ipv4.unicast.routing.rev180319.VppIpv4RouteAttributesAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.ipv4.unicast.routing.rev180319.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv4.route.VppIpv4Route;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.ipv6.unicast.routing.rev180319.VppIpv6RouteAttributesAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.ipv6.unicast.routing.rev180319.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv6.route.VppIpv6Route;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.routing.rev180319.VppRouteAttributes;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.routing.types.rev180406.VniReference;
39 public class TableLookupRequestFactory extends BasicHopRequestFactory implements RouteRequestProducer {
42 private static final byte[] DEAGRAGATION_ADDRESS = new byte[4];
44 public TableLookupRequestFactory(@Nonnull final VppClassifierContextManager classifierContextManager,
45 @Nonnull final NamingContext interfaceContext,
46 @Nonnull final NamingContext routingProtocolContext) {
47 super(classifierContextManager, interfaceContext, routingProtocolContext);
50 public IpAddDelRoute createV4TableLookupRouteRequest(final boolean add,
51 @Nonnull final String parentProtocolName,
52 @Nonnull final Route route,
53 @Nonnull final MappingContext mappingContext) {
55 final Ipv4Prefix prefix = route.getDestinationPrefix();
56 final byte[] destinationAddress = ipv4AddressPrefixToArray(prefix);
57 final byte destinationPrefix = extractPrefix(prefix);
58 final int primaryTableId = getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext);
59 final byte secondaryTableId = Optional.ofNullable(route.getNextHop().getNextHopOptions())
60 .filter(nextHopOptions -> nextHopOptions instanceof TableLookupCase)
61 .map(TableLookupCase.class::cast)
62 .map(TableLookupCase::getSecondaryVrf)
63 .map(VniReference::getValue)
65 .orElseThrow(() -> new IllegalArgumentException("Table lookup option not specified correctly"));
67 VppIpv4Route vppIpv4Route =
68 route.getAugmentation(VppIpv4RouteAttributesAugmentation.class) != null ? route.getAugmentation(
69 VppIpv4RouteAttributesAugmentation.class).getVppIpv4Route() : null;
71 final Optional<String> optClassifyTable = Optional.ofNullable(vppIpv4Route)
72 .map(VppRouteAttributes::getClassifyTable);
73 final byte classifyTableSet = booleanToByte(optClassifyTable.isPresent());
74 final byte classifyTableIndex = optClassifyTable.map(tableName -> classifyTableIndex(tableName, getVppClassifierContextManager(), mappingContext))
75 .map(Integer::byteValue)
76 .orElse(DEFAULT_CLASSIFY_TABLE_INDEX);
78 return flaglessAddDelRouteRequest(booleanToByte(add), ~0, DEAGRAGATION_ADDRESS, (byte) 0, (byte) 0, destinationAddress,
79 destinationPrefix, (byte) 0, primaryTableId, secondaryTableId, classifyTableIndex, classifyTableSet);
82 public IpAddDelRoute createV6TableLookupRouteRequest(final boolean add,
83 @Nonnull final String parentProtocolName,
84 @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,
85 @Nonnull final MappingContext mappingContext) {
87 final Ipv6Prefix prefix = route.getDestinationPrefix();
88 final byte[] destinationAddress = ipv6AddressPrefixToArray(prefix);
89 final byte destinationPrefix = extractPrefix(prefix);
90 final int primaryTableId = getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext);
91 final byte secondaryTableId = Optional.ofNullable(route.getNextHop().getNextHopOptions())
92 .filter(nextHopOptions -> nextHopOptions instanceof TableLookupCase)
93 .map(TableLookupCase.class::cast)
94 .map(TableLookupCase::getSecondaryVrf)
95 .map(VniReference::getValue)
97 .orElseThrow(() -> new IllegalArgumentException("Table lookup option not specified correctly"));
99 VppIpv6Route vppIpv6Route = route.getAugmentation(VppIpv6RouteAttributesAugmentation.class) != null
100 ? route.getAugmentation(VppIpv6RouteAttributesAugmentation.class).getVppIpv6Route() : null;
102 final Optional<String> optClassifyTable = Optional.ofNullable(vppIpv6Route)
103 .map(VppRouteAttributes::getClassifyTable);
104 final byte classifyTableSet = booleanToByte(optClassifyTable.isPresent());
105 final byte classifyTableIndex = optClassifyTable.map(tableName -> classifyTableIndex(tableName, getVppClassifierContextManager(), mappingContext))
106 .map(Integer::byteValue)
107 .orElse(DEFAULT_CLASSIFY_TABLE_INDEX);
109 return flaglessAddDelRouteRequest(booleanToByte(add), ~0, DEAGRAGATION_ADDRESS, (byte) 0, (byte) 1, destinationAddress,
110 destinationPrefix, (byte) 0, primaryTableId, secondaryTableId, classifyTableIndex, classifyTableSet);