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