2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.vpp;
19 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte;
20 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.parseMac;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Preconditions;
24 import com.google.common.primitives.Longs;
25 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
26 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
29 import io.fd.honeycomb.v3po.translate.write.WriteContext;
30 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
31 import java.util.List;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.L2FibFilter;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.L2FibTable;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.openvpp.jvpp.VppBaseCallException;
43 import org.openvpp.jvpp.dto.L2FibAddDel;
44 import org.openvpp.jvpp.dto.L2FibAddDelReply;
45 import org.openvpp.jvpp.future.FutureJVpp;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Writer Customizer responsible for L2 FIB create/delete operations.<br> Sends {@code l2_fib_add_del} message to
51 * VPP.<br> Equivalent of invoking {@code vppctl l2fib add/del} command.
53 public class L2FibEntryCustomizer extends FutureJVppCustomizer
54 implements ListWriterCustomizer<L2FibEntry, L2FibEntryKey> {
56 private static final Logger LOG = LoggerFactory.getLogger(L2FibEntryCustomizer.class);
58 private final NamingContext bdContext;
59 private final NamingContext interfaceContext;
61 public L2FibEntryCustomizer(@Nonnull final FutureJVpp futureJvpp, @Nonnull final NamingContext bdContext,
62 @Nonnull final NamingContext interfaceContext) {
64 this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
65 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
70 public Optional<List<L2FibEntry>> extract(@Nonnull final InstanceIdentifier<L2FibEntry> currentId,
71 @Nonnull final DataObject parentData) {
72 return Optional.fromNullable(((L2FibTable) parentData).getL2FibEntry());
76 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
77 @Nonnull final L2FibEntry dataAfter, @Nonnull final WriteContext writeContext)
78 throws WriteFailedException.CreateFailedException {
80 LOG.debug("Creating L2 FIB entry: {} {}", id, dataAfter);
81 l2FibAddDel(id, dataAfter, writeContext, true);
82 LOG.debug("L2 FIB entry created successfully: {} {}", id, dataAfter);
83 } catch (VppBaseCallException e) {
84 LOG.warn("Failed to create L2 FIB entry: {} {}", id, dataAfter);
85 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
90 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
91 @Nonnull final L2FibEntry dataBefore, @Nonnull final L2FibEntry dataAfter,
92 @Nonnull final WriteContext writeContext) throws WriteFailedException {
93 throw new UnsupportedOperationException(
94 "L2 FIB entry update is not supported. It has to be deleted and then created.");
98 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
99 @Nonnull final L2FibEntry dataBefore, @Nonnull final WriteContext writeContext)
100 throws WriteFailedException.DeleteFailedException {
102 LOG.debug("Deleting L2 FIB entry: {} {}", id, dataBefore);
103 l2FibAddDel(id, dataBefore, writeContext, false);
104 LOG.debug("L2 FIB entry deleted successfully: {} {}", id, dataBefore);
105 } catch (VppBaseCallException e) {
106 LOG.warn("Failed to delete L2 FIB entry: {} {}", id, dataBefore);
107 throw new WriteFailedException.DeleteFailedException(id, e);
111 private void l2FibAddDel(@Nonnull final InstanceIdentifier<L2FibEntry> id, @Nonnull final L2FibEntry entry,
112 final WriteContext writeContext, boolean isAdd) throws VppBaseCallException {
113 final String bdName = id.firstKeyOf(BridgeDomain.class).getName();
114 final int bdId = bdContext.getIndex(bdName, writeContext.getMappingContext());
117 final String swIfName = entry.getOutgoingInterface();
118 if (swIfName != null) {
119 swIfIndex = interfaceContext.getIndex(swIfName, writeContext.getMappingContext());
122 final L2FibAddDel l2FibRequest = createL2FibRequest(entry, bdId, swIfIndex, isAdd);
123 LOG.debug("Sending l2FibAddDel request: {}", ReflectionToStringBuilder.toString(l2FibRequest));
124 final CompletionStage<L2FibAddDelReply> l2FibAddDelReplyCompletionStage =
125 getFutureJVpp().l2FibAddDel(l2FibRequest);
127 TranslateUtils.getReply(l2FibAddDelReplyCompletionStage.toCompletableFuture());
130 private L2FibAddDel createL2FibRequest(final L2FibEntry entry, final int bdId, final int swIfIndex, boolean isAdd) {
131 final L2FibAddDel request = new L2FibAddDel();
132 request.mac = macToLong(entry.getPhysAddress().getValue());
134 request.swIfIndex = swIfIndex;
135 request.isAdd = booleanToByte(isAdd);
137 request.staticMac = booleanToByte(entry.isStaticConfig());
138 request.filterMac = booleanToByte(L2FibFilter.class == entry.getAction());
143 // mac address is string of the form: 11:22:33:44:55:66
144 // but VPP expects long value in the format 11:22:33:44:55:66:XX:XX
145 private static long macToLong(final String macAddress) {
146 final byte[] mac = parseMac(macAddress);
147 return Longs.fromBytes(mac[0], mac[1], mac[2], mac[3],
148 mac[4], mac[5], (byte) 0, (byte) 0);