HONEYCOMB-234: update YANG model to support egress ACLs
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / ProxyArpCustomizer.java
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.honeycomb.translate.v3po.interfaces;
18
19 import com.google.common.net.InetAddresses;
20 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
23 import io.fd.honeycomb.translate.write.WriteContext;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import java.net.InetAddress;
26 import java.util.concurrent.CompletionStage;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.ProxyArp;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import io.fd.vpp.jvpp.VppBaseCallException;
33 import io.fd.vpp.jvpp.core.dto.ProxyArpAddDel;
34 import io.fd.vpp.jvpp.core.dto.ProxyArpAddDelReply;
35 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class ProxyArpCustomizer extends FutureJVppCustomizer implements WriterCustomizer<ProxyArp>, JvppReplyConsumer {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ProxyArpCustomizer.class);
42
43     public ProxyArpCustomizer(final FutureJVppCore vppApi) {
44         super(vppApi);
45     }
46
47     @Override
48     public void writeCurrentAttributes(@Nonnull InstanceIdentifier<ProxyArp> id, @Nonnull ProxyArp dataAfter,
49                                        @Nonnull WriteContext writeContext) throws WriteFailedException {
50         final String swIfName = id.firstKeyOf(Interface.class).getName();
51
52         try {
53             setProxyArp(id, swIfName, dataAfter, (byte) 1 /* 1 is add */);
54         } catch (VppBaseCallException e) {
55             LOG.error("Failed to set Proxy ARP settings: {}, for interface: {}", dataAfter, swIfName);
56             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
57         }
58     }
59
60     @Override
61     public void updateCurrentAttributes(@Nonnull InstanceIdentifier<ProxyArp> id, @Nonnull ProxyArp dataBefore,
62                                         @Nonnull ProxyArp dataAfter, @Nonnull WriteContext writeContext)
63             throws WriteFailedException.UpdateFailedException {
64         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
65                 new UnsupportedOperationException("ARP proxy update is not supported"));
66     }
67
68     @Override
69     public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<ProxyArp> id, @Nonnull ProxyArp dataBefore,
70                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
71
72         final String swIfName = id.firstKeyOf(Interface.class).getName();
73         try {
74             setProxyArp(id, swIfName, dataBefore, (byte) 0 /* 0 is delete */);
75         } catch (VppBaseCallException e) {
76             LOG.debug("Failed to delete Proxy ARP settings: {}, for interface: {}", dataBefore, swIfName);
77             throw new WriteFailedException.DeleteFailedException(id, e);
78         }
79     }
80
81     private void setProxyArp(InstanceIdentifier<ProxyArp> id, String swIfName, ProxyArp proxyArp, byte operation)
82         throws VppBaseCallException, WriteFailedException {
83         LOG.debug("Setting Proxy ARP settings for interface: {}", swIfName);
84         final InetAddress srcAddress = InetAddresses.forString(getv4AddressString(proxyArp.getLowAddr()));
85         final InetAddress dstAddress = InetAddresses.forString(getv4AddressString(proxyArp.getHighAddr()));
86         final int vrfId = proxyArp.getVrfId().intValue();
87         final CompletionStage<ProxyArpAddDelReply> proxyArpAddDelReplyCompletionStage =
88                 getFutureJVpp().proxyArpAddDel(getProxyArpConfRequest(operation, srcAddress.getAddress(),
89                         dstAddress.getAddress(), vrfId));
90
91         final ProxyArpAddDelReply reply =
92                 getReplyForWrite(proxyArpAddDelReplyCompletionStage.toCompletableFuture(), id);
93         LOG.debug("Proxy ARP setting applied, with reply context:", reply.context);
94     }
95
96     private static ProxyArpAddDel getProxyArpConfRequest(final byte isAdd, final byte[] lAddr, final byte[] hAddr,
97                                                          final int vrfId) {
98
99         final ProxyArpAddDel proxyArpAddDel = new ProxyArpAddDel();
100         proxyArpAddDel.isAdd = isAdd;
101         proxyArpAddDel.lowAddress = lAddr;
102         proxyArpAddDel.hiAddress = hAddr;
103         proxyArpAddDel.vrfId = vrfId;
104         return proxyArpAddDel;
105     }
106
107     private String getv4AddressString(@Nonnull final Ipv4Address addr) {
108         return addr.getValue();
109     }
110 }