summaryrefslogtreecommitdiffstats
path: root/routing/routing-impl/src/main/java/io/fd/hc2vpp/routing/write/factory/SpecialNextHopRequestFactory.java
blob: 2a5ab9821706e10f608e31f3b3ef3f73c06b6c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.routing.write.factory;

import static com.google.common.base.Preconditions.checkNotNull;

import io.fd.hc2vpp.routing.write.factory.base.ClassifierContextHolder;
import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
import io.fd.hc2vpp.v3po.vppclassifier.VppClassifierContextManager;
import io.fd.honeycomb.translate.MappingContext;
import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.SpecialNextHopGrouping;

public class SpecialNextHopRequestFactory extends ClassifierContextHolder
        implements RouteRequestProducer {

    private SpecialNextHopRequestFactory(final VppClassifierContextManager classifierContextManager) {
        super(classifierContextManager);
    }

    public static SpecialNextHopRequestFactory forClassifierContext(
            @Nonnull final VppClassifierContextManager classifierContextManager) {
        return new SpecialNextHopRequestFactory(classifierContextManager);
    }

    public IpAddDelRoute createIpv4SpecialHopRequest(final boolean add,
                                                     @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,
                                                     @Nonnull final MappingContext mappingContext,
                                                     @Nonnull final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {
        checkNotNull(route, "Route cannot be null");
        checkNotNull(mappingContext, "Mapping Context cannot be null");
        checkNotNull(flagsVariant, "Flags variant cannot be null");

        return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix()), flagsVariant);
    }

    public IpAddDelRoute createIpv6SpecialHopRequest(final boolean add,
                                                     @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,
                                                     @Nonnull final MappingContext mappingContext,
                                                     @Nonnull final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {

        checkNotNull(route, "Route cannot be null");
        checkNotNull(mappingContext, "Mapping Context cannot be null");
        checkNotNull(flagsVariant, "Flags variant cannot be null");

        return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix()), flagsVariant);
    }

    private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv6Prefix destinationAddress) {

        return flaglessAddDelRouteRequest(booleanToByte(isAdd), 0, null, DEFAULT_HOP_WEIGHT, BYTE_TRUE,
                ipv6AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), BYTE_FALSE,
                DEFAULT_VNI, DEFAULT_VNI, DEFAULT_CLASSIFY_TABLE_INDEX, BYTE_FALSE);
    }

    private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv4Prefix destinationAddress) {
        return flaglessAddDelRouteRequest(booleanToByte(isAdd), 0, null, DEFAULT_HOP_WEIGHT, BYTE_FALSE,
                ipv4AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), BYTE_FALSE,
                DEFAULT_VNI, DEFAULT_VNI, DEFAULT_CLASSIFY_TABLE_INDEX, BYTE_FALSE);
    }

    private IpAddDelRoute resolveFlags(IpAddDelRoute request,
                                       final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {
        switch (flagsVariant) {
            case Blackhole:
                return resolveAsBlackholeVariant(request);
            case Unreachable:
                return resolveAsUnreachableVariant(request);
            case Prohibit:
                return resolveAsProhibitedVariant(request);
            case Receive:
                return resolveAsReceiveVariant(request);
            default:
                throw new IllegalArgumentException("Unsupported type");
        }
    }

    private IpAddDelRoute resolveAsBlackholeVariant(IpAddDelRoute request) {
        return bindFlags(request, true, false, false, false);
    }

    private IpAddDelRoute resolveAsReceiveVariant(IpAddDelRoute request) {
        return bindFlags(request, false, true, false, false);
    }

    private IpAddDelRoute resolveAsUnreachableVariant(IpAddDelRoute request) {
        return bindFlags(request, false, false, true, false);
    }

    private IpAddDelRoute resolveAsProhibitedVariant(IpAddDelRoute request) {
        return bindFlags(request, false, false, false, true);
    }

    private IpAddDelRoute bindFlags(IpAddDelRoute request, final boolean isDrop, final boolean isReceive,
                                    final boolean isUnreachable, final boolean isProhibited) {
        request.isDrop = booleanToByte(isDrop);
        request.isLocal = booleanToByte(isReceive);
        request.isUnreach = booleanToByte(isUnreachable);
        request.isProhibit = booleanToByte(isProhibited);

        return request;
    }
}