59198b58a188ebfb5c0305cfe0c1ab58b9cb3600
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vpp / L2FibEntryCustomizer.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 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.rev150105.L2FibFilter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
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.
45  */
46 public class L2FibEntryCustomizer extends FutureJVppCustomizer
47         implements ListWriterCustomizer<L2FibEntry, L2FibEntryKey>, ByteDataTranslator, MacTranslator,
48         JvppReplyConsumer {
49
50     private static final Logger LOG = LoggerFactory.getLogger(L2FibEntryCustomizer.class);
51
52     private final NamingContext bdContext;
53     private final NamingContext interfaceContext;
54
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");
60     }
61
62     @Override
63     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
64                                        @Nonnull final L2FibEntry dataAfter, @Nonnull final WriteContext writeContext)
65             throws WriteFailedException {
66
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);
70     }
71
72     @Override
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.");
78     }
79
80     @Override
81     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<L2FibEntry> id,
82                                         @Nonnull final L2FibEntry dataBefore, @Nonnull final WriteContext writeContext)
83             throws WriteFailedException {
84
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);
88     }
89
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());
94
95         int swIfIndex = -1;
96         final String swIfName = entry.getOutgoingInterface();
97         if (swIfName != null) {
98             swIfIndex = interfaceContext.getIndex(swIfName, writeContext.getMappingContext());
99         }
100
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);
105
106         getReplyForWrite(l2FibAddDelReplyCompletionStage.toCompletableFuture(), id);
107     }
108
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());
112         request.bdId = bdId;
113         request.swIfIndex = swIfIndex;
114         request.isAdd = booleanToByte(isAdd);
115         if (isAdd) {
116             request.staticMac = booleanToByte(entry.isStaticConfig());
117             request.filterMac = booleanToByte(L2FibFilter.class == entry.getAction());
118         }
119         return request;
120     }
121
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);
128     }
129 }