HONEYCOMB-117: add support for jvpp plugins
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vpp / ArpTerminationTableEntryCustomizer.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.vpp;
18
19 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte;
20
21 import com.google.common.base.Preconditions;
22 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
26 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import java.util.concurrent.CompletionStage;
30 import javax.annotation.Nonnull;
31 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntry;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.bridge.domain.attributes.arp.termination.table.ArpTerminationTableEntryKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.openvpp.jvpp.VppBaseCallException;
39 import org.openvpp.jvpp.core.dto.BdIpMacAddDel;
40 import org.openvpp.jvpp.core.dto.BdIpMacAddDelReply;
41 import org.openvpp.jvpp.core.future.FutureJVppCore;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Writer Customizer responsible for ARP termination table management.<br> Sends {@code bd_ip_mac_add_del} message to
47  * VPP.<br> Equivalent of invoking {@code vppctl set bridge-domain arp term} command.
48  */
49 public class ArpTerminationTableEntryCustomizer extends FutureJVppCustomizer
50     implements ListWriterCustomizer<ArpTerminationTableEntry, ArpTerminationTableEntryKey> {
51
52     private static final Logger LOG = LoggerFactory.getLogger(ArpTerminationTableEntryCustomizer.class);
53
54     private final NamingContext bdContext;
55
56     public ArpTerminationTableEntryCustomizer(@Nonnull final FutureJVppCore futureJvpp,
57                                               @Nonnull final NamingContext bdContext) {
58         super(futureJvpp);
59         this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
60     }
61
62     @Override
63     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
64                                        @Nonnull final ArpTerminationTableEntry dataAfter,
65                                        @Nonnull final WriteContext writeContext)
66         throws WriteFailedException {
67         try {
68             LOG.debug("Creating ARP termination table entry: {} {}", id, dataAfter);
69             bdIpMacAddDel(id, dataAfter, writeContext, true);
70             LOG.debug("L2 ARP termination table entry created successfully: {} {}", id, dataAfter);
71         } catch (VppBaseCallException e) {
72             LOG.warn("Failed to create ARP termination table entry: {} {}", id, dataAfter);
73             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
74         }
75     }
76
77     @Override
78     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
79                                         @Nonnull final ArpTerminationTableEntry dataBefore,
80                                         @Nonnull final ArpTerminationTableEntry dataAfter,
81                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
82         throw new UnsupportedOperationException(
83             "ARP termination table entry update is not supported. It has to be deleted and then created.");
84     }
85
86     @Override
87     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
88                                         @Nonnull final ArpTerminationTableEntry dataBefore,
89                                         @Nonnull final WriteContext writeContext)
90         throws WriteFailedException {
91         try {
92             LOG.debug("Deleting ARP termination table entry entry: {} {}", id, dataBefore);
93             bdIpMacAddDel(id, dataBefore, writeContext, false);
94             LOG.debug("ARP termination table entry deleted successfully: {} {}", id, dataBefore);
95         } catch (VppBaseCallException e) {
96             LOG.warn("Failed to delete ARP termination table entry: {} {}", id, dataBefore);
97             throw new WriteFailedException.DeleteFailedException(id, e);
98         }
99     }
100
101     private void bdIpMacAddDel(@Nonnull final InstanceIdentifier<ArpTerminationTableEntry> id,
102                                @Nonnull final ArpTerminationTableEntry entry,
103                                final WriteContext writeContext, boolean isAdd)
104         throws VppBaseCallException, WriteTimeoutException {
105         final String bdName = id.firstKeyOf(BridgeDomain.class).getName();
106         final int bdId = bdContext.getIndex(bdName, writeContext.getMappingContext());
107
108         final BdIpMacAddDel request = createRequest(entry, bdId, isAdd);
109         LOG.debug("Sending l2FibAddDel request: {}", ReflectionToStringBuilder.toString(request));
110         final CompletionStage<BdIpMacAddDelReply> replyCompletionStage =
111             getFutureJVpp().bdIpMacAddDel(request);
112
113         TranslateUtils.getReplyForWrite(replyCompletionStage.toCompletableFuture(), id);
114     }
115
116     private BdIpMacAddDel createRequest(final ArpTerminationTableEntry entry, final int bdId, boolean isAdd) {
117         final BdIpMacAddDel request = new BdIpMacAddDel();
118         request.bdId = bdId;
119         request.isAdd = booleanToByte(isAdd);
120         request.macAddress = TranslateUtils.parseMac(entry.getPhysAddress().getValue());
121
122         final IpAddress ipAddress = entry.getIpAddress();
123         if (ipAddress.getIpv6Address() != null) {
124             // FIXME: vpp does not support ipv6 in arp-termination table (based on analysis of l2_bd.c)
125             throw new UnsupportedOperationException("IPv6 address for ARP termination table is not supported yet");
126         }
127
128         request.ipAddress = TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(ipAddress.getIpv4Address()));
129         return request;
130     }
131 }