146d97c5c5430759e10a5a54380dcbf15f52ee58
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / GreCustomizer.java
1 /*
2  * Copyright (c) 2016 Intel 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.honeycomb.translate.v3po.interfaces;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.net.InetAddresses;
22 import io.fd.honeycomb.translate.vpp.util.AbstractInterfaceTypeCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.vpp.jvpp.core.dto.GreAddDelTunnel;
28 import io.fd.vpp.jvpp.core.dto.GreAddDelTunnelReply;
29 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
30 import java.net.InetAddress;
31 import java.util.concurrent.CompletionStage;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.GreTunnel;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Gre;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class GreCustomizer extends AbstractInterfaceTypeCustomizer<Gre> implements JvppReplyConsumer {
43
44     private static final Logger LOG = LoggerFactory.getLogger(GreCustomizer.class);
45     private final NamingContext interfaceContext;
46
47     public GreCustomizer(final FutureJVppCore vppApi, final NamingContext interfaceContext) {
48         super(vppApi);
49         this.interfaceContext = interfaceContext;
50     }
51
52     private static GreAddDelTunnel getGreTunnelRequest(final byte isAdd, final byte[] srcAddr, final byte[] dstAddr,
53                                                        final int outerFibId, final byte isIpv6) {
54         final GreAddDelTunnel greAddDelTunnel = new GreAddDelTunnel();
55         greAddDelTunnel.isAdd = isAdd;
56         greAddDelTunnel.srcAddress = srcAddr;
57         greAddDelTunnel.dstAddress = dstAddr;
58         greAddDelTunnel.outerFibId = outerFibId;
59         greAddDelTunnel.isIpv6 = isIpv6;
60         return greAddDelTunnel;
61     }
62
63     @Override
64     protected Class<? extends InterfaceType> getExpectedInterfaceType() {
65         return GreTunnel.class;
66     }
67
68     @Override
69     protected final void writeInterface(@Nonnull final InstanceIdentifier<Gre> id, @Nonnull final Gre dataAfter,
70                                         @Nonnull final WriteContext writeContext)
71             throws WriteFailedException {
72         final String swIfName = id.firstKeyOf(Interface.class).getName();
73
74         createGreTunnel(id, swIfName, dataAfter, writeContext);
75     }
76
77     @Override
78     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Gre> id, @Nonnull final Gre dataBefore,
79                                         @Nonnull final Gre dataAfter, @Nonnull final WriteContext writeContext)
80             throws WriteFailedException.UpdateFailedException {
81         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
82                 new UnsupportedOperationException("Gre tunnel update is not supported"));
83     }
84
85     @Override
86     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Gre> id, @Nonnull final Gre dataBefore,
87                                         @Nonnull final WriteContext writeContext)
88             throws WriteFailedException {
89         final String swIfName = id.firstKeyOf(Interface.class).getName();
90         deleteGreTunnel(id, swIfName, dataBefore, writeContext);
91     }
92
93     private void createGreTunnel(final InstanceIdentifier<Gre> id, final String swIfName, final Gre gre,
94                                  final WriteContext writeContext) throws WriteFailedException {
95         final byte isIpv6 = (byte) (isIpv6(gre)
96                 ? 1
97                 : 0);
98         final InetAddress srcAddress = InetAddresses.forString(getAddressString(gre.getSrc()));
99         final InetAddress dstAddress = InetAddresses.forString(getAddressString(gre.getDst()));
100
101         int outerFibId = gre.getOuterFibId().intValue();
102
103         LOG.debug("Setting gre tunnel for interface: {}. Gre: {}", swIfName, gre);
104         final CompletionStage<GreAddDelTunnelReply> greAddDelTunnelReplyCompletionStage =
105                 getFutureJVpp().greAddDelTunnel(getGreTunnelRequest((byte) 1 /* is add */, srcAddress.getAddress(),
106                         dstAddress.getAddress(), outerFibId, isIpv6));
107
108         final GreAddDelTunnelReply reply =
109                 getReplyForCreate(greAddDelTunnelReplyCompletionStage.toCompletableFuture(), id, gre);
110         LOG.debug("Gre tunnel set successfully for: {}, gre: {}", swIfName, gre);
111         if (interfaceContext.containsName(reply.swIfIndex, writeContext.getMappingContext())) {
112             // VPP keeps gre tunnels present even after they are delete(reserving ID for next tunnel)
113             // This may cause inconsistencies in mapping context when configuring tunnels like this:
114             // 1. Add tunnel 2. Delete tunnel 3. Read interfaces (reserved mapping e.g. gre_tunnel0 -> 6
115             // will get into mapping context) 4. Add tunnel (this will add another mapping with the same
116             // reserved ID and context is invalid)
117             // That's why a check has to be performed here removing mapping gre_tunnel0 -> 6 mapping and storing
118             // new name for that ID
119             final String formerName = interfaceContext.getName(reply.swIfIndex, writeContext.getMappingContext());
120             LOG.debug("Removing updated mapping of a gre tunnel, id: {}, former name: {}, new name: {}",
121                     reply.swIfIndex, formerName, swIfName);
122             interfaceContext.removeName(formerName, writeContext.getMappingContext());
123         }
124         // Add new interface to our interface context
125         interfaceContext.addName(reply.swIfIndex, swIfName, writeContext.getMappingContext());
126     }
127
128     private boolean isIpv6(final Gre gre) {
129         if (gre.getSrc().getIpv4Address() == null) {
130             checkArgument(gre.getDst().getIpv4Address() == null, "Inconsistent ip addresses: %s, %s", gre.getSrc(),
131                     gre.getDst());
132             return true;
133         } else {
134             checkArgument(gre.getDst().getIpv6Address() == null, "Inconsistent ip addresses: %s, %s", gre.getSrc(),
135                     gre.getDst());
136             return false;
137         }
138     }
139
140     private String getAddressString(final IpAddress addr) {
141         return addr.getIpv4Address() == null
142                 ? addr.getIpv6Address().getValue()
143                 : addr.getIpv4Address().getValue();
144     }
145
146     private void deleteGreTunnel(final InstanceIdentifier<Gre> id, final String swIfName, final Gre gre,
147                                  final WriteContext writeContext) throws WriteFailedException {
148         final byte isIpv6 = (byte) (isIpv6(gre)
149                 ? 1
150                 : 0);
151         final InetAddress srcAddress = InetAddresses.forString(getAddressString(gre.getSrc()));
152         final InetAddress dstAddress = InetAddresses.forString(getAddressString(gre.getDst()));
153
154         int outerFibId = gre.getOuterFibId().intValue();
155
156         LOG.debug("Deleting gre tunnel for interface: {}. Gre: {}", swIfName, gre);
157         final CompletionStage<GreAddDelTunnelReply> greAddDelTunnelReplyCompletionStage =
158                 getFutureJVpp().greAddDelTunnel(getGreTunnelRequest((byte) 0 /* is add */, srcAddress.getAddress(),
159                         dstAddress.getAddress(), outerFibId, isIpv6));
160
161         getReplyForDelete(greAddDelTunnelReplyCompletionStage.toCompletableFuture(), id);
162         LOG.debug("Gre tunnel deleted successfully for: {}, gre: {}", swIfName, gre);
163         // Remove interface from our interface context
164         interfaceContext.removeName(swIfName, writeContext.getMappingContext());
165     }
166 }