HONEYCOMB-154: update revison of models that changed since 16.09
[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 io.fd.vpp.jvpp.core.dto.ProxyArpAddDel;
26 import io.fd.vpp.jvpp.core.dto.ProxyArpAddDelReply;
27 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
28 import java.net.InetAddress;
29 import java.util.concurrent.Future;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.ProxyArp;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class ProxyArpCustomizer extends FutureJVppCustomizer implements WriterCustomizer<ProxyArp>, JvppReplyConsumer {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ProxyArpCustomizer.class);
41
42     public ProxyArpCustomizer(final FutureJVppCore vppApi) {
43         super(vppApi);
44     }
45
46     @Override
47     public void writeCurrentAttributes(@Nonnull InstanceIdentifier<ProxyArp> id, @Nonnull ProxyArp dataAfter,
48                                        @Nonnull WriteContext writeContext) throws WriteFailedException {
49         final String swIfName = id.firstKeyOf(Interface.class).getName();
50         createProxyArp(getProxyArpRequestFuture(id, swIfName, dataAfter, (byte) 1 /* 1 is add */), id, dataAfter);
51     }
52
53     @Override
54     public void updateCurrentAttributes(@Nonnull InstanceIdentifier<ProxyArp> id, @Nonnull ProxyArp dataBefore,
55                                         @Nonnull ProxyArp dataAfter, @Nonnull WriteContext writeContext)
56             throws WriteFailedException.UpdateFailedException {
57         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
58                 new UnsupportedOperationException("ARP proxy update is not supported"));
59     }
60
61     @Override
62     public void deleteCurrentAttributes(@Nonnull InstanceIdentifier<ProxyArp> id, @Nonnull ProxyArp dataBefore,
63                                         @Nonnull WriteContext writeContext) throws WriteFailedException {
64
65         final String swIfName = id.firstKeyOf(Interface.class).getName();
66         deleteProxyArp(getProxyArpRequestFuture(id, swIfName, dataBefore, (byte) 0 /* 0 is delete */), id);
67     }
68
69     private Future<ProxyArpAddDelReply> getProxyArpRequestFuture(InstanceIdentifier<ProxyArp> id, String swIfName,
70                                                                  ProxyArp proxyArp, byte operation)
71             throws WriteFailedException {
72         LOG.debug("Setting Proxy ARP settings for interface: {}", swIfName);
73         final InetAddress srcAddress = InetAddresses.forString(getv4AddressString(proxyArp.getLowAddr()));
74         final InetAddress dstAddress = InetAddresses.forString(getv4AddressString(proxyArp.getHighAddr()));
75         final int vrfId = proxyArp.getVrfId().intValue();
76         return getFutureJVpp().proxyArpAddDel(
77                 getProxyArpConfRequest(operation, srcAddress.getAddress(), dstAddress.getAddress(), vrfId))
78                 .toCompletableFuture();
79     }
80
81     private void createProxyArp(final Future<ProxyArpAddDelReply> future, final InstanceIdentifier<ProxyArp> identifier,
82                                 final ProxyArp data)
83             throws WriteFailedException {
84         final ProxyArpAddDelReply reply = getReplyForCreate(future, identifier, data);
85         LOG.debug("Proxy ARP setting create successful, with reply context:", reply.context);
86     }
87
88     private void deleteProxyArp(final Future<ProxyArpAddDelReply> future, final InstanceIdentifier<ProxyArp> identifier)
89             throws WriteFailedException {
90         final ProxyArpAddDelReply reply = getReplyForDelete(future, identifier);
91         LOG.debug("Proxy ARP setting delete successful, with reply context:", reply.context);
92     }
93
94     private static ProxyArpAddDel getProxyArpConfRequest(final byte isAdd, final byte[] lAddr, final byte[] hAddr,
95                                                          final int vrfId) {
96
97         final ProxyArpAddDel proxyArpAddDel = new ProxyArpAddDel();
98         proxyArpAddDel.isAdd = isAdd;
99         proxyArpAddDel.lowAddress = lAddr;
100         proxyArpAddDel.hiAddress = hAddr;
101         proxyArpAddDel.vrfId = vrfId;
102         return proxyArpAddDel;
103     }
104
105     private String getv4AddressString(@Nonnull final Ipv4Address addr) {
106         return addr.getValue();
107     }
108 }