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.translate.v3po.vpp;
19 import com.google.common.base.Preconditions;
20 import com.google.common.primitives.Longs;
21 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
23 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.vpp.util.MacTranslator;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.vpp.jvpp.core.dto.L2FibAddDel;
30 import io.fd.vpp.jvpp.core.dto.L2FibAddDelReply;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.L2FibFilter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.fib.attributes.l2.fib.table.L2FibEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomain;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Writer Customizer responsible for L2 FIB create/delete operations.<br> Sends {@code l2_fib_add_del} message to
44 * VPP.<br> Equivalent of invoking {@code vppctl l2fib add/del} command.
46 public class L2FibEntryCustomizer extends FutureJVppCustomizer
47 implements ListWriterCustomizer<L2FibEntry, L2FibEntryKey>, ByteDataTranslator, MacTranslator,
50 private static final Logger LOG = LoggerFactory.getLogger(L2FibEntryCustomizer.class);
52 private final NamingContext bdContext;
53 private final NamingContext interfaceContext;
55 public L2FibEntryCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext bdContext,
56 @Nonnull final NamingContext interfaceContext) {
57 super(futureJVppCore);
58 this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
59 this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");
63 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
64 @Nonnull final L2FibEntry dataAfter, @Nonnull final WriteContext writeContext)
65 throws WriteFailedException {
67 LOG.debug("Creating L2 FIB entry: {} {}", id, dataAfter);
68 l2FibAddDel(id, dataAfter, writeContext, true);
69 LOG.debug("L2 FIB entry created successfully: {} {}", id, dataAfter);
73 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
74 @Nonnull final L2FibEntry dataBefore, @Nonnull final L2FibEntry dataAfter,
75 @Nonnull final WriteContext writeContext) throws WriteFailedException {
76 throw new UnsupportedOperationException(
77 "L2 FIB entry update is not supported. It has to be deleted and then created.");
81 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
82 @Nonnull final L2FibEntry dataBefore, @Nonnull final WriteContext writeContext)
83 throws WriteFailedException {
85 LOG.debug("Deleting L2 FIB entry: {} {}", id, dataBefore);
86 l2FibAddDel(id, dataBefore, writeContext, false);
87 LOG.debug("L2 FIB entry deleted successfully: {} {}", id, dataBefore);
90 private void l2FibAddDel(@Nonnull final InstanceIdentifier<L2FibEntry> id, @Nonnull final L2FibEntry entry,
91 final WriteContext writeContext, boolean isAdd) throws WriteFailedException {
92 final String bdName = id.firstKeyOf(BridgeDomain.class).getName();
93 final int bdId = bdContext.getIndex(bdName, writeContext.getMappingContext());
96 final String swIfName = entry.getOutgoingInterface();
97 if (swIfName != null) {
98 swIfIndex = interfaceContext.getIndex(swIfName, writeContext.getMappingContext());
101 final L2FibAddDel l2FibRequest = createL2FibRequest(entry, bdId, swIfIndex, isAdd);
102 LOG.debug("Sending l2FibAddDel request: {}", l2FibRequest);
103 final CompletionStage<L2FibAddDelReply> l2FibAddDelReplyCompletionStage =
104 getFutureJVpp().l2FibAddDel(l2FibRequest);
106 getReplyForWrite(l2FibAddDelReplyCompletionStage.toCompletableFuture(), id);
109 private L2FibAddDel createL2FibRequest(final L2FibEntry entry, final int bdId, final int swIfIndex, boolean isAdd) {
110 final L2FibAddDel request = new L2FibAddDel();
111 request.mac = macToLong(entry.getPhysAddress().getValue());
113 request.swIfIndex = swIfIndex;
114 request.isAdd = booleanToByte(isAdd);
116 request.staticMac = booleanToByte(entry.isStaticConfig());
117 request.filterMac = booleanToByte(L2FibFilter.class == entry.getAction());
122 // mac address is string of the form: 11:22:33:44:55:66
123 // but VPP expects long value in the format 11:22:33:44:55:66:XX:XX
124 private long macToLong(final String macAddress) {
125 final byte[] mac = parseMac(macAddress);
126 return Longs.fromBytes(mac[0], mac[1], mac[2], mac[3],
127 mac[4], mac[5], (byte) 0, (byte) 0);