85b99820484a48b118f378cee09c2bb9163ca141
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 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 static com.google.common.base.Preconditions.checkNotNull;
20
21 import io.fd.hc2vpp.common.translate.util.NamingContext;
22 import io.fd.hc2vpp.routing.write.factory.base.BasicHopRequestFactory;
23 import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
24 import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
25 import io.fd.honeycomb.translate.MappingContext;
26 import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.SpecialNextHop;
32
33 public class SpecialNextHopRequestFactory extends BasicHopRequestFactory
34         implements RouteRequestProducer {
35
36     private SpecialNextHopRequestFactory(final VppClassifierContextManager classifierContextManager,
37                                          final NamingContext interfaceContext,
38                                          final NamingContext routingProtocolContext) {
39         super(classifierContextManager, interfaceContext, routingProtocolContext);
40     }
41
42     public static SpecialNextHopRequestFactory forContexts(
43             @Nonnull final VppClassifierContextManager classifierContextManager,
44             @Nonnull final NamingContext interfaceContext,
45             @Nonnull final NamingContext routingProtocolContext) {
46         return new SpecialNextHopRequestFactory(classifierContextManager, interfaceContext, routingProtocolContext);
47     }
48
49     public IpAddDelRoute createIpv4SpecialHopRequest(final boolean add,
50                                                      @Nonnull final String parentProtocolName,
51                                                      @Nonnull final Route route,
52                                                      @Nonnull final MappingContext mappingContext,
53                                                      @Nonnull final SpecialNextHop.SpecialNextHopEnum flagsVariant) {
54         checkNotNull(route, "Route cannot be null");
55         checkNotNull(mappingContext, "Mapping Context cannot be null");
56         checkNotNull(flagsVariant, "Flags variant cannot be null");
57
58         final int parentProtocolTableId = getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext);
59         return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix(), (byte) parentProtocolTableId, DEFAULT_VNI), flagsVariant);
60     }
61
62     public IpAddDelRoute createIpv6SpecialHopRequest(final boolean add,
63                                                      @Nonnull final String parentProtocolName,
64                                                      @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,
65                                                      @Nonnull final MappingContext mappingContext,
66                                                      @Nonnull final SpecialNextHop.SpecialNextHopEnum flagsVariant) {
67
68         checkNotNull(route, "Route cannot be null");
69         checkNotNull(mappingContext, "Mapping Context cannot be null");
70         checkNotNull(flagsVariant, "Flags variant cannot be null");
71
72         final int parentProtocolTableId = getRoutingProtocolContext().getIndex(parentProtocolName, mappingContext);
73         return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix(), (byte) parentProtocolTableId, DEFAULT_VNI), flagsVariant);
74     }
75
76     private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv6Prefix destinationAddress,
77                                                final byte primaryTableId, final byte secondaryTableId) {
78
79         return flaglessAddDelRouteRequest(booleanToByte(isAdd), 0, null, DEFAULT_HOP_WEIGHT, BYTE_TRUE,
80                 ipv6AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), BYTE_FALSE,
81                 primaryTableId, secondaryTableId, DEFAULT_CLASSIFY_TABLE_INDEX, BYTE_FALSE);
82     }
83
84     private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv4Prefix destinationAddress,
85                                                final byte primaryTableId, final byte secondaryTableId) {
86         return flaglessAddDelRouteRequest(booleanToByte(isAdd), 0, null, DEFAULT_HOP_WEIGHT, BYTE_FALSE,
87                 ipv4AddressPrefixToArray(destinationAddress), extractPrefix(destinationAddress.getValue()), BYTE_FALSE,
88                 primaryTableId, secondaryTableId, DEFAULT_CLASSIFY_TABLE_INDEX, BYTE_FALSE);
89     }
90
91     private IpAddDelRoute resolveFlags(IpAddDelRoute request,
92                                        final SpecialNextHop.SpecialNextHopEnum flagsVariant) {
93         switch (flagsVariant) {
94             case Blackhole:
95                 return resolveAsBlackholeVariant(request);
96             case Unreachable:
97                 return resolveAsUnreachableVariant(request);
98             case Prohibit:
99                 return resolveAsProhibitedVariant(request);
100             case Receive:
101                 return resolveAsReceiveVariant(request);
102             default:
103                 throw new IllegalArgumentException("Unsupported type");
104         }
105     }
106
107     private IpAddDelRoute resolveAsBlackholeVariant(IpAddDelRoute request) {
108         return bindFlags(request, true, false, false, false);
109     }
110
111     private IpAddDelRoute resolveAsReceiveVariant(IpAddDelRoute request) {
112         return bindFlags(request, false, true, false, false);
113     }
114
115     private IpAddDelRoute resolveAsUnreachableVariant(IpAddDelRoute request) {
116         return bindFlags(request, false, false, true, false);
117     }
118
119     private IpAddDelRoute resolveAsProhibitedVariant(IpAddDelRoute request) {
120         return bindFlags(request, false, false, false, true);
121     }
122
123     private IpAddDelRoute bindFlags(IpAddDelRoute request, final boolean isDrop, final boolean isReceive,
124                                     final boolean isUnreachable, final boolean isProhibited) {
125         request.isDrop = booleanToByte(isDrop);
126         request.isLocal = booleanToByte(isReceive);
127         request.isUnreach = booleanToByte(isUnreachable);
128         request.isProhibit = booleanToByte(isProhibited);
129
130         return request;
131     }
132 }