2a5ab9821706e10f608e31f3b3ef3f73c06b6c92
[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.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;
30
31 public class SpecialNextHopRequestFactory extends ClassifierContextHolder
32         implements RouteRequestProducer {
33
34     private SpecialNextHopRequestFactory(final VppClassifierContextManager classifierContextManager) {
35         super(classifierContextManager);
36     }
37
38     public static SpecialNextHopRequestFactory forClassifierContext(
39             @Nonnull final VppClassifierContextManager classifierContextManager) {
40         return new SpecialNextHopRequestFactory(classifierContextManager);
41     }
42
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");
50
51         return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix()), flagsVariant);
52     }
53
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) {
58
59         checkNotNull(route, "Route cannot be null");
60         checkNotNull(mappingContext, "Mapping Context cannot be null");
61         checkNotNull(flagsVariant, "Flags variant cannot be null");
62
63         return resolveFlags(getSpecialHopRequest(add, route.getDestinationPrefix()), flagsVariant);
64     }
65
66     private IpAddDelRoute getSpecialHopRequest(final boolean isAdd, @Nonnull final Ipv6Prefix destinationAddress) {
67
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);
71     }
72
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);
77     }
78
79     private IpAddDelRoute resolveFlags(IpAddDelRoute request,
80                                        final SpecialNextHopGrouping.SpecialNextHop flagsVariant) {
81         switch (flagsVariant) {
82             case Blackhole:
83                 return resolveAsBlackholeVariant(request);
84             case Unreachable:
85                 return resolveAsUnreachableVariant(request);
86             case Prohibit:
87                 return resolveAsProhibitedVariant(request);
88             case Receive:
89                 return resolveAsReceiveVariant(request);
90             default:
91                 throw new IllegalArgumentException("Unsupported type");
92         }
93     }
94
95     private IpAddDelRoute resolveAsBlackholeVariant(IpAddDelRoute request) {
96         return bindFlags(request, true, false, false, false);
97     }
98
99     private IpAddDelRoute resolveAsReceiveVariant(IpAddDelRoute request) {
100         return bindFlags(request, false, true, false, false);
101     }
102
103     private IpAddDelRoute resolveAsUnreachableVariant(IpAddDelRoute request) {
104         return bindFlags(request, false, false, true, false);
105     }
106
107     private IpAddDelRoute resolveAsProhibitedVariant(IpAddDelRoute request) {
108         return bindFlags(request, false, false, false, true);
109     }
110
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);
117
118         return request;
119     }
120 }