summaryrefslogtreecommitdiffstats
path: root/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/write/ifc/AbstractInterfaceNatCustomizer.java
blob: c25e0ccb27e1ccf0c74cf6e78665d8e5fa450424 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (c) 2017 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.nat.write.ifc;

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

import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.jvpp.nat.dto.Nat44InterfaceAddDelFeature;
import io.fd.jvpp.nat.dto.Nat44InterfaceAddDelOutputFeature;
import io.fd.jvpp.nat.dto.Nat64AddDelInterface;
import io.fd.jvpp.nat.future.FutureJVppNatFacade;
import io.fd.jvpp.nat.types.InterfaceIndex;
import io.fd.jvpp.nat.types.NatConfigFlags;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang._interface.nat.rev170816.InterfaceNatVppFeatureAttributes;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;

abstract class AbstractInterfaceNatCustomizer<D extends InterfaceNatVppFeatureAttributes & DataObject>
        implements ByteDataTranslator, JvppReplyConsumer, WriterCustomizer<D> {

    private final FutureJVppNatFacade jvppNat;
    private final NamingContext ifcContext;

    AbstractInterfaceNatCustomizer(@Nonnull final FutureJVppNatFacade jvppNat,
                                   @Nonnull final NamingContext ifcContext) {
        this.jvppNat = jvppNat;
        this.ifcContext = ifcContext;
    }

    @Override
    public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter,
                                       @Nonnull final WriteContext writeContext) throws WriteFailedException {
        final String ifcName = getName(id);
        getLog().debug("Enabling " + getType() + " NAT on interface: {}", ifcName);
        getLog().debug("Enabling {} NAT: {}", dataAfter, id);

        final int ifcIndex = ifcContext.getIndex(ifcName, writeContext.getMappingContext());
        if (dataAfter.isPostRouting()) {
            postRoutingNat(id, dataAfter, ifcIndex, true);
        } else {
            preRoutingNat(id, dataAfter, ifcIndex, true);
        }
        getLog().debug("NAT {} enabled successfully on: {}", dataAfter, ifcName);
    }

    @Override
    public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
                                        @Nonnull final D dataBefore, @Nonnull final WriteContext writeContext)
            throws WriteFailedException {
        final String ifcName = getName(id);
        getLog().debug("Disabling " + getType() + " NAT on interface: {}", ifcName);
        getLog().debug("Disabling {} NAT: {}", dataBefore, id);

        final int ifcIndex = ifcContext.getIndex(ifcName, writeContext.getMappingContext());
        if (dataBefore.isPostRouting()) {
            postRoutingNat(id, dataBefore, ifcIndex, false);
        } else {
            preRoutingNat(id, dataBefore, ifcIndex, false);
        }
        getLog().debug("NAT {} disabled successfully on: {}", dataBefore, ifcName);
    }

    protected String getName(final InstanceIdentifier<D> id) {
        return id.firstKeyOf(Interface.class).getName();
    }

    private void postRoutingNat(@Nonnull final InstanceIdentifier<D> id, final D natAttributes, final int ifcIndex,
                                final boolean enable)
            throws WriteFailedException {
        checkArgument(!isNat64Supported(natAttributes), "Post routing Nat64 is not supported by VPP");
        final Nat44InterfaceAddDelOutputFeature request = new Nat44InterfaceAddDelOutputFeature();
        request.isAdd = enable;
        if (request.flags == null) {
            request.flags = new NatConfigFlags();
        }
        if (getType() == NatType.INBOUND) {
            request.flags.add(NatConfigFlags.NatConfigFlagsOptions.NAT_IS_INSIDE);
        } else if (getType() == NatType.OUTBOUND) {
            request.flags.add(NatConfigFlags.NatConfigFlagsOptions.NAT_IS_OUTSIDE);
        }
        request.swIfIndex = new InterfaceIndex();
        request.swIfIndex.interfaceindex = ifcIndex;
        getReplyForWrite(jvppNat.nat44InterfaceAddDelOutputFeature(request).toCompletableFuture(), id);
    }

    private void preRoutingNat(@Nonnull final InstanceIdentifier<D> id, final D natAttributes, final int ifcIndex,
                               final boolean enable)
            throws WriteFailedException {
        if (natAttributes.isNat44Support()) {
            // default value is defined for nat44-support, so no need for null check
            preRoutingNat44(id, ifcIndex, enable);
        }
        if (isNat64Supported(natAttributes)) {
            preRoutingNat64(id, ifcIndex, enable);
        }
    }

    private boolean isNat64Supported(final D natAttributes) {
        return natAttributes.isNat64Support() != null && natAttributes.isNat64Support();
    }

    private void preRoutingNat44(@Nonnull final InstanceIdentifier<D> id, final int ifcIndex, final boolean enable)
            throws WriteFailedException {
        final Nat44InterfaceAddDelFeature request = new Nat44InterfaceAddDelFeature();
        request.isAdd = enable;
        if (request.flags == null) {
            request.flags = new NatConfigFlags();
        }
        if (getType() == NatType.INBOUND) {
            request.flags.add(NatConfigFlags.NatConfigFlagsOptions.NAT_IS_INSIDE);
        } else if (getType() == NatType.OUTBOUND) {
            request.flags.add(NatConfigFlags.NatConfigFlagsOptions.NAT_IS_OUTSIDE);
        }
        request.swIfIndex = new InterfaceIndex();
        request.swIfIndex.interfaceindex = ifcIndex;
        getReplyForWrite(jvppNat.nat44InterfaceAddDelFeature(request).toCompletableFuture(), id);
    }

    private void preRoutingNat64(@Nonnull final InstanceIdentifier<D> id, final int ifcIndex, final boolean enable)
            throws WriteFailedException {
        final Nat64AddDelInterface request = new Nat64AddDelInterface();
        request.isAdd = enable;
        if (request.flags == null) {
            request.flags = new NatConfigFlags();
        }
        if (getType() == NatType.INBOUND) {
            request.flags.add(NatConfigFlags.NatConfigFlagsOptions.NAT_IS_INSIDE);
        } else if (getType() == NatType.OUTBOUND) {
            request.flags.add(NatConfigFlags.NatConfigFlagsOptions.NAT_IS_OUTSIDE);
        }
        request.swIfIndex = new InterfaceIndex();
        request.swIfIndex.interfaceindex = ifcIndex;
        getReplyForWrite(jvppNat.nat64AddDelInterface(request).toCompletableFuture(), id);
    }

    enum NatType {
        INBOUND((byte) 1), OUTBOUND((byte) 0);

        private final byte isInside;

        NatType(final byte isInside) {
            this.isInside = isInside;
        }
    }

    abstract NatType getType();

    abstract Logger getLog();
}