2 * Copyright (c) 2016 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 static com.google.common.base.Preconditions.checkNotNull;
21 import io.fd.hc2vpp.routing.write.factory.base.ClassifierContextHolder;
22 import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
23 import io.fd.hc2vpp.v3po.vppclassifier.VppClassifierContextManager;
24 import io.fd.honeycomb.translate.MappingContext;
25 import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
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.routing.rev140524.SpecialNextHopGrouping;
31 public class SpecialNextHopRequestFactory extends ClassifierContextHolder
32 implements RouteRequestProducer {
34 private SpecialNextHopRequestFactory(final VppClassifierContextManager classifierContextManager) {
35 super(classifierContextManager);
38 public static SpecialNextHopRequestFactory forClassifierContext(
39 @Nonnull final VppClassifierContextManager classifierContextManager) {
40 return new SpecialNextHopRequestFactory(classifierContextManager);
43 public IpAddDelRoute createIpv4SpecialHopRequest(final boolean add,
44 @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,
45 @Nonnull final MappingContext mappingContext,
46 @Nonnull final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {
47 checkNotNull(route, "Route cannot be null");
48 checkNotNull(mappingContext, "Mapping Context cannot be null");
49 checkNotNull(flagsVariant, "Flags variant cannot be null");
51 return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix()), flagsVariant);
54 public IpAddDelRoute createIpv6SpecialHopRequest(final boolean add,
55 @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,
56 @Nonnull final MappingContext mappingContext,
57 @Nonnull final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {
59 checkNotNull(route, "Route cannot be null");
60 checkNotNull(mappingContext, "Mapping Context cannot be null");
61 checkNotNull(flagsVariant, "Flags variant cannot be null");
63 return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix()), flagsVariant);
66 private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv6Prefix destinationAddress) {
68 return flaglessAddDelRouteRequest(booleanToByte(isAdd), 0, null, DEFAULT_HOP_WEIGHT, BYTE_TRUE,
69 ipv6AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), BYTE_FALSE,
70 DEFAULT_VNI, DEFAULT_VNI, DEFAULT_CLASSIFY_TABLE_INDEX, BYTE_FALSE);
73 private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv4Prefix destinationAddress) {
74 return flaglessAddDelRouteRequest(booleanToByte(isAdd), 0, null, DEFAULT_HOP_WEIGHT, BYTE_FALSE,
75 ipv4AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), BYTE_FALSE,
76 DEFAULT_VNI, DEFAULT_VNI, DEFAULT_CLASSIFY_TABLE_INDEX, BYTE_FALSE);
79 private IpAddDelRoute resolveFlags(IpAddDelRoute request,
80 final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {
81 switch (flagsVariant) {
83 return resolveAsBlackholeVariant(request);
85 return resolveAsUnreachableVariant(request);
87 return resolveAsProhibitedVariant(request);
89 return resolveAsReceiveVariant(request);
91 throw new IllegalArgumentException("Unsupported type");
95 private IpAddDelRoute resolveAsBlackholeVariant(IpAddDelRoute request) {
96 return bindFlags(request, true, false, false, false);
99 private IpAddDelRoute resolveAsReceiveVariant(IpAddDelRoute request) {
100 return bindFlags(request, false, true, false, false);
103 private IpAddDelRoute resolveAsUnreachableVariant(IpAddDelRoute request) {
104 return bindFlags(request, false, false, true, false);
107 private IpAddDelRoute resolveAsProhibitedVariant(IpAddDelRoute request) {
108 return bindFlags(request, false, false, false, true);
111 private IpAddDelRoute bindFlags(IpAddDelRoute request, final boolean isDrop, final boolean isReceive,
112 final boolean isUnreachable, final boolean isProhibited) {
113 request.isDrop = booleanToByte(isDrop);
114 request.isLocal = booleanToByte(isReceive);
115 request.isUnreach = booleanToByte(isUnreachable);
116 request.isProhibit = booleanToByte(isProhibited);