aafa0403d179cdb53fbcdd49f787c8d1cc7ed145
[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.fib.management.FibManagementIIds;
21 import io.fd.hc2vpp.routing.write.factory.base.BasicHopRequestFactory;
22 import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
23 import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
26 import java.util.Optional;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv4.unicast.routing.rev180319.VppIpv4RouteAttributesAugmentation;
29 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv4.unicast.routing.rev180319.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv4.route.VppIpv4Route;
30 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv6.unicast.routing.rev180319.VppIpv6RouteAttributesAugmentation;
31 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.ipv6.unicast.routing.rev180319.routing.control.plane.protocols.control.plane.protocol._static.routes.ipv6.route.VppIpv6Route;
32 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.routing.rev180319.VppRouteAttributes;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
35 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;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.next.hop.content.next.hop.options.TableLookupCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv4;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv6;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.VniReference;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.Table;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableKey;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43
44 public class TableLookupRequestFactory extends BasicHopRequestFactory implements RouteRequestProducer {
45
46     // must be all zeros
47     private static final byte[] DEAGRAGATION_ADDRESS = new byte[4];
48
49     public TableLookupRequestFactory(@Nonnull final VppClassifierContextManager classifierContextManager,
50                                      @Nonnull final NamingContext interfaceContext,
51                                      @Nonnull final NamingContext routingProtocolContext) {
52         super(classifierContextManager, interfaceContext, routingProtocolContext);
53     }
54
55     public IpAddDelRoute createV4TableLookupRouteRequest(final boolean add,
56                                                          @Nonnull final String parentProtocolName,
57                                                          @Nonnull final Route route,
58                                                          @Nonnull final WriteContext writeContext) {
59
60         final Ipv4Prefix prefix = route.getDestinationPrefix();
61         final byte[] destinationAddress = ipv4AddressPrefixToArray(prefix);
62         final byte destinationPrefix = extractPrefix(prefix);
63         final int primaryTableId = getRoutingProtocolContext().getIndex(parentProtocolName,
64                 writeContext.getMappingContext());
65         final Long secondaryTableId = Optional.ofNullable(route.getNextHop().getNextHopOptions())
66                 .filter(nextHopOptions -> nextHopOptions instanceof TableLookupCase)
67                 .map(TableLookupCase.class::cast)
68                 .map(TableLookupCase::getSecondaryVrf)
69                 .map(VniReference::getValue)
70                 .orElseThrow(() -> new IllegalArgumentException("Table lookup option not specified correctly"));
71
72         TableKey key = new TableKey(Ipv4.class, new VniReference(secondaryTableId));
73         KeyedInstanceIdentifier<Table, TableKey> fibIid = FibManagementIIds.FM_FIB_TABLES.child(Table.class, key);
74         if (!writeContext.readAfter(fibIid).isPresent()) {
75             throw new IllegalArgumentException(
76                     String.format("Lookup table: %s not found for route: %s", secondaryTableId, route));
77         }
78
79         VppIpv4Route vppIpv4Route =
80             route.augmentation(VppIpv4RouteAttributesAugmentation.class) != null ? route.augmentation(
81                 VppIpv4RouteAttributesAugmentation.class).getVppIpv4Route() : null;
82
83         final Optional<String> optClassifyTable = Optional.ofNullable(vppIpv4Route)
84                 .map(VppRouteAttributes::getClassifyTable);
85         final byte classifyTableSet = booleanToByte(optClassifyTable.isPresent());
86         final byte classifyTableIndex = optClassifyTable
87                 .map(tableName -> classifyTableIndex(tableName, getVppClassifierContextManager(),
88                         writeContext.getMappingContext()))
89                 .map(Integer::byteValue)
90                 .orElse(DEFAULT_CLASSIFY_TABLE_INDEX);
91
92         return flaglessAddDelRouteRequest(booleanToByte(add), ~0, DEAGRAGATION_ADDRESS, (byte) 0, (byte) 0,
93                 destinationAddress, destinationPrefix, (byte) 0, primaryTableId, secondaryTableId.intValue(),
94                 classifyTableIndex, classifyTableSet);
95     }
96
97     public IpAddDelRoute createV6TableLookupRouteRequest(final boolean add,
98                                                          @Nonnull final String parentProtocolName,
99                                                          @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,
100                                                          @Nonnull final WriteContext writeContext) {
101
102         final Ipv6Prefix prefix = route.getDestinationPrefix();
103         final byte[] destinationAddress = ipv6AddressPrefixToArray(prefix);
104         final byte destinationPrefix = extractPrefix(prefix);
105         final int primaryTableId =
106                 getRoutingProtocolContext().getIndex(parentProtocolName, writeContext.getMappingContext());
107         final Long secondaryTableId = Optional.ofNullable(route.getNextHop().getNextHopOptions())
108                 .filter(nextHopOptions -> nextHopOptions instanceof TableLookupCase)
109                 .map(TableLookupCase.class::cast)
110                 .map(TableLookupCase::getSecondaryVrf)
111                 .map(VniReference::getValue)
112                 .orElseThrow(() -> new IllegalArgumentException("Table lookup option not specified correctly"));
113
114         TableKey key = new TableKey(Ipv6.class, new VniReference(secondaryTableId));
115         KeyedInstanceIdentifier<Table, TableKey> fibIid = FibManagementIIds.FM_FIB_TABLES.child(Table.class, key);
116         if (!writeContext.readAfter(fibIid).isPresent()) {
117             throw new IllegalArgumentException(
118                     String.format("Lookup table: %s not found for route: %s", secondaryTableId, route));
119         }
120
121         VppIpv6Route vppIpv6Route = route.augmentation(VppIpv6RouteAttributesAugmentation.class) != null
122             ? route.augmentation(VppIpv6RouteAttributesAugmentation.class).getVppIpv6Route() : null;
123
124         final Optional<String> optClassifyTable = Optional.ofNullable(vppIpv6Route)
125                 .map(VppRouteAttributes::getClassifyTable);
126         final byte classifyTableSet = booleanToByte(optClassifyTable.isPresent());
127         final byte classifyTableIndex = optClassifyTable
128                 .map(tableName -> classifyTableIndex(tableName, getVppClassifierContextManager(),
129                         writeContext.getMappingContext())).map(Integer::byteValue).orElse(DEFAULT_CLASSIFY_TABLE_INDEX);
130
131         return flaglessAddDelRouteRequest(booleanToByte(add), ~0, DEAGRAGATION_ADDRESS, (byte) 0, (byte) 1,
132                 destinationAddress, destinationPrefix, (byte) 0, primaryTableId, secondaryTableId.intValue(),
133                 classifyTableIndex, classifyTableSet);
134     }
135 }