a6a31e2aee87d5afdfdc5f9edea85e5ca7ac3ddf
[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.v3po.translate.v3po.vppstate;
18
19 import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.byteToBoolean;
20
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.Lists;
24 import com.google.common.primitives.Longs;
25 import io.fd.honeycomb.v3po.translate.read.ReadContext;
26 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
27 import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer;
28 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
29 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
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.L2FibForward;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.L2FibTableBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.fib.attributes.l2.fib.table.L2FibEntryKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.BridgeDomainsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainKey;
45 import org.opendaylight.yangtools.concepts.Builder;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.openvpp.jvpp.dto.BridgeDomainDetails;
49 import org.openvpp.jvpp.dto.BridgeDomainDetailsReplyDump;
50 import org.openvpp.jvpp.dto.BridgeDomainDump;
51 import org.openvpp.jvpp.dto.L2FibTableDump;
52 import org.openvpp.jvpp.dto.L2FibTableEntry;
53 import org.openvpp.jvpp.dto.L2FibTableEntryReplyDump;
54 import org.openvpp.jvpp.future.FutureJVpp;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 public final class BridgeDomainCustomizer extends FutureJVppCustomizer
59         implements ListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder> {
60
61     private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
62     private final NamingContext bdContext;
63     private final NamingContext interfaceContext;
64
65     public BridgeDomainCustomizer(@Nonnull final FutureJVpp futureJVpp, @Nonnull final NamingContext bdContext,
66                                   @Nonnull final NamingContext interfaceContext) {
67         super(futureJVpp);
68         this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
69         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");;
70     }
71
72     @Override
73     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
74                                       @Nonnull final BridgeDomainBuilder builder, @Nonnull final ReadContext context)
75             throws ReadFailedException {
76         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
77                 id, builder, context);
78
79         final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
80         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);
81
82         final int bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
83         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);
84
85         BridgeDomainDetailsReplyDump reply;
86         BridgeDomainDetails bridgeDomainDetails;
87         final BridgeDomainDump request = new BridgeDomainDump();
88         request.bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
89         try {
90             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
91             bridgeDomainDetails = Iterables.getOnlyElement(reply.bridgeDomainDetails);
92         } catch (Exception e) {
93             LOG.debug("Unable to read bridge domain: {}", key.getName(), e);
94             return;
95         }
96
97         logBridgeDomainDetails(bridgeDomainDetails);
98
99         builder.setName(key.getName());
100         builder.setArpTermination(byteToBoolean(bridgeDomainDetails.arpTerm));
101         builder.setFlood(byteToBoolean(bridgeDomainDetails.flood));
102         builder.setForward(byteToBoolean(bridgeDomainDetails.forward));
103         builder.setLearn(byteToBoolean(bridgeDomainDetails.learn));
104         builder.setUnknownUnicastFlood(byteToBoolean(bridgeDomainDetails.uuFlood));
105
106         final L2FibTableDump l2FibRequest = new L2FibTableDump();
107         l2FibRequest.bdId = bdId;
108         try {
109             final L2FibTableEntryReplyDump dump =
110                     getFutureJVpp().l2FibTableDump(l2FibRequest).toCompletableFuture().get();
111             final List<L2FibEntry> l2Fibs;
112
113             if(null == dump || null == dump.l2FibTableEntry) {
114                 l2Fibs = Collections.emptyList();
115             } else {
116                 l2Fibs = Lists.newArrayListWithCapacity(dump.l2FibTableEntry.size());
117                 for (L2FibTableEntry entry : dump.l2FibTableEntry) {
118                     // entry.mac is a long value in the format 66:55:44:33:22:11:XX:XX
119                     // where mac address is 11:22:33:44:55:66
120                     final PhysAddress address = new PhysAddress(getMacAddress(Longs.toByteArray(entry.mac)));
121                     l2Fibs.add(new L2FibEntryBuilder()
122                         .setAction(byteToBoolean(entry.filterMac)
123                                 ? L2FibFilter.class
124                                 : L2FibForward.class)
125                         .setBridgedVirtualInterface(byteToBoolean(entry.bviMac))
126                         .setOutgoingInterface(interfaceContext.getName(entry.swIfIndex, context.getMappingContext()))
127                         .setStaticConfig(byteToBoolean(entry.staticMac))
128                         .setPhysAddress(address)
129                         .setKey(new L2FibEntryKey(address))
130                         .build());
131                 }
132             }
133             builder.setL2FibTable(new L2FibTableBuilder().setL2FibEntry(l2Fibs).build());
134
135         } catch (Exception e) {
136             LOG.warn("Failed to acquire l2FibTableDump for domain id={}", bdId, e);
137         }
138     }
139
140     private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
141         LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
142         if (bridgeDomainDetails != null) {
143             LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
144             LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
145             LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
146             LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
147             LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
148             LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
149             LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
150             LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
151         }
152     }
153
154     // TODO move to some utility class
155     private static String getMacAddress(byte[] mac) {
156         StringBuilder sb = new StringBuilder(18);
157         for (int i=5; i>=0; --i) {
158             if (sb.length() > 0) {
159                 sb.append(':');
160             }
161             sb.append(String.format("%02x", mac[i]));
162         }
163         return sb.toString();
164     }
165
166     @Nonnull
167     @Override
168     public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
169         return new BridgeDomainBuilder();
170     }
171
172     @Nonnull
173     @Override
174     public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
175                                            @Nonnull final ReadContext context) {
176         final BridgeDomainDump request = new BridgeDomainDump();
177         request.bdId = -1; // dump call
178
179         BridgeDomainDetailsReplyDump reply;
180         try {
181             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
182         } catch (Exception e) {
183             throw new IllegalStateException("Bridge domain dump failed", e); // TODO ReadFailedException?
184         }
185
186         if(reply == null || reply.bridgeDomainDetails == null) {
187             return Collections.emptyList();
188         }
189
190         final int bIdsLength = reply.bridgeDomainDetails.size();
191         LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
192         if (bIdsLength == 0) {
193             // No bridge domains
194             return Collections.emptyList();
195         }
196
197         final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
198         for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
199             logBridgeDomainDetails(detail);
200
201             final String bName = bdContext.getName(detail.bdId, context.getMappingContext());
202             LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
203             allIds.add(new BridgeDomainKey(bName));
204         }
205
206         return allIds;
207     }
208
209     @Override
210     public void merge(@Nonnull final Builder<? extends DataObject> builder,
211                       @Nonnull final List<BridgeDomain> readData) {
212         ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);
213     }
214 }