8a11adc8f8ea9f827a117627b9c337f9df739e35
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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 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;
38
39 public class TableLookupRequestFactory extends BasicHopRequestFactory implements RouteRequestProducer {
40
41     // must be all zeros
42     private static final byte[] DEAGRAGATION_ADDRESS = new byte[4];
43
44     public TableLookupRequestFactory(@Nonnull final VppClassifierContextManager classifierContextManager,
45                                      @Nonnull final NamingContext interfaceContext,
46                                      @Nonnull final NamingContext routingProtocolContext) {
47         super(classifierContextManager, interfaceContext, routingProtocolContext);
48     }
49
50     public IpAddDelRoute createV4TableLookupRouteRequest(final boolean add,
51                                                          @Nonnull final String parentProtocolName,
52                                                          @Nonnull final Route route,
53                                                          @Nonnull final MappingContext mappingContext) {
54
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)
64                 .map(Long::byteValue)
65                 .orElseThrow(() -> new IllegalArgumentException("Table lookup option not specified correctly"));
66
67         VppIpv4Route vppIpv4Route =
68             route.getAugmentation(VppIpv4RouteAttributesAugmentation.class) != null ? route.getAugmentation(
69                 VppIpv4RouteAttributesAugmentation.class).getVppIpv4Route() : null;
70
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);
77
78         return flaglessAddDelRouteRequest(booleanToByte(add), ~0, DEAGRAGATION_ADDRESS, (byte) 0, (byte) 0, destinationAddress,
79                 destinationPrefix, (byte) 0, primaryTableId, secondaryTableId, classifyTableIndex, classifyTableSet);
80     }
81
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) {
86
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)
96                 .map(Long::byteValue)
97                 .orElseThrow(() -> new IllegalArgumentException("Table lookup option not specified correctly"));
98
99         VppIpv6Route vppIpv6Route = route.getAugmentation(VppIpv6RouteAttributesAugmentation.class) != null
100             ? route.getAugmentation(VppIpv6RouteAttributesAugmentation.class).getVppIpv6Route() : null;
101
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);
108
109         return flaglessAddDelRouteRequest(booleanToByte(add), ~0, DEAGRAGATION_ADDRESS, (byte) 0, (byte) 1, destinationAddress,
110                 destinationPrefix, (byte) 0, primaryTableId, secondaryTableId, classifyTableIndex, classifyTableSet);
111     }
112 }