dabd73ed95e41d60261e0074210db2bf5c487c61
[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.vpp.state.BridgeDomainsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.Interface;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.InterfaceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.InterfaceKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.L2Fib;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.L2FibBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.bridge.domain.L2FibKey;
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.BridgeDomainSwIfDetails;
52 import org.openvpp.jvpp.dto.L2FibTableDump;
53 import org.openvpp.jvpp.dto.L2FibTableEntry;
54 import org.openvpp.jvpp.dto.L2FibTableEntryReplyDump;
55 import org.openvpp.jvpp.future.FutureJVpp;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public final class BridgeDomainCustomizer extends FutureJVppCustomizer
60         implements ListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder> {
61
62     private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
63     private final NamingContext bdContext;
64     private final NamingContext interfaceContext;
65
66     public BridgeDomainCustomizer(@Nonnull final FutureJVpp futureJVpp, @Nonnull final NamingContext bdContext,
67                                   @Nonnull final NamingContext interfaceContext) {
68         super(futureJVpp);
69         this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
70         this.interfaceContext = Preconditions.checkNotNull(interfaceContext, "interfaceContext should not be null");;
71     }
72
73     @Override
74     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
75                                       @Nonnull final BridgeDomainBuilder builder, @Nonnull final ReadContext context)
76             throws ReadFailedException {
77         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
78                 id, builder, context);
79
80         final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
81         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);
82
83         final int bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
84         LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);
85
86         BridgeDomainDetailsReplyDump reply;
87         BridgeDomainDetails bridgeDomainDetails;
88         final BridgeDomainDump request = new BridgeDomainDump();
89         request.bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
90         try {
91             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
92             bridgeDomainDetails = Iterables.getOnlyElement(reply.bridgeDomainDetails);
93         } catch (Exception e) {
94             LOG.debug("Unable to read bridge domain: {}", key.getName(), e);
95             return;
96         }
97
98         logBridgeDomainDetails(bridgeDomainDetails);
99
100         builder.setName(key.getName());
101         builder.setArpTermination(byteToBoolean(bridgeDomainDetails.arpTerm));
102         builder.setFlood(byteToBoolean(bridgeDomainDetails.flood));
103         builder.setForward(byteToBoolean(bridgeDomainDetails.forward));
104         builder.setLearn(byteToBoolean(bridgeDomainDetails.learn));
105         builder.setUnknownUnicastFlood(byteToBoolean(bridgeDomainDetails.uuFlood));
106
107         builder.setInterface(getIfcs(bridgeDomainDetails, reply.bridgeDomainSwIfDetails, context));
108
109         final L2FibTableDump l2FibRequest = new L2FibTableDump();
110         l2FibRequest.bdId = bdId;
111         try {
112             final L2FibTableEntryReplyDump dump =
113                     getFutureJVpp().l2FibTableDump(l2FibRequest).toCompletableFuture().get();
114             final List<L2Fib> l2Fibs;
115
116             if(null == dump || null == dump.l2FibTableEntry) {
117                 l2Fibs = Collections.emptyList();
118             } else {
119                 l2Fibs = Lists.newArrayListWithCapacity(dump.l2FibTableEntry.size());
120                 for (L2FibTableEntry entry : dump.l2FibTableEntry) {
121                     // entry.mac is a long value in the format 66:55:44:33:22:11:XX:XX
122                     // where mac address is 11:22:33:44:55:66
123                     final PhysAddress address = new PhysAddress(getMacAddress(Longs.toByteArray(entry.mac)));
124                     l2Fibs.add(new L2FibBuilder()
125                         .setAction((byteToBoolean(entry.filterMac)
126                             ? L2Fib.Action.Filter
127                             : L2Fib.Action.Forward))
128                         .setBridgedVirtualInterface(byteToBoolean(entry.bviMac))
129                         .setOutgoingInterface(interfaceContext.getName(entry.swIfIndex, context.getMappingContext()))
130                         .setStaticConfig(byteToBoolean(entry.staticMac))
131                         .setPhysAddress(address)
132                         .setKey(new L2FibKey(address))
133                         .build());
134                 }
135             }
136             builder.setL2Fib(l2Fibs);
137
138         } catch (Exception e) {
139             LOG.warn("Failed to acquire l2FibTableDump for domain id={}", bdId, e);
140         }
141     }
142
143     private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
144         LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
145         if (bridgeDomainDetails != null) {
146             LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
147             LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
148             LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
149             LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
150             LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
151             LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
152             LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
153             LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
154         }
155     }
156
157     // TODO move to some utility class
158     private static String getMacAddress(byte[] mac) {
159         StringBuilder sb = new StringBuilder(18);
160         for (int i=5; i>=0; --i) {
161             if (sb.length() > 0) {
162                 sb.append(':');
163             }
164             sb.append(String.format("%02x", mac[i]));
165         }
166         return sb.toString();
167     }
168
169     private List<Interface> getIfcs(final BridgeDomainDetails bridgeDomainDetails,
170                                     final List<BridgeDomainSwIfDetails> bridgeDomainSwIfDetails,
171                                     final ReadContext context) {
172         final List<Interface> ifcs = new ArrayList<>(bridgeDomainSwIfDetails.size());
173         for (BridgeDomainSwIfDetails anInterface : bridgeDomainSwIfDetails) {
174             final String interfaceName = interfaceContext.getName(anInterface.swIfIndex, context.getMappingContext());
175             if (anInterface.bdId == bridgeDomainDetails.bdId) {
176                 ifcs.add(new InterfaceBuilder()
177                         .setBridgedVirtualInterface(bridgeDomainDetails.bviSwIfIndex == anInterface.swIfIndex)
178                         .setSplitHorizonGroup((short) anInterface.shg)
179                         .setName(interfaceName)
180                         .setKey(new InterfaceKey(interfaceName))
181                         .build());
182             }
183
184
185         }
186         return ifcs;
187     }
188
189     @Nonnull
190     @Override
191     public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
192         return new BridgeDomainBuilder();
193     }
194
195     @Nonnull
196     @Override
197     public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
198                                            @Nonnull final ReadContext context) {
199         final BridgeDomainDump request = new BridgeDomainDump();
200         request.bdId = -1; // dump call
201
202         BridgeDomainDetailsReplyDump reply;
203         try {
204             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
205         } catch (Exception e) {
206             throw new IllegalStateException("Bridge domain dump failed", e); // TODO ReadFailedException?
207         }
208
209         if(reply == null || reply.bridgeDomainDetails == null) {
210             return Collections.emptyList();
211         }
212
213         final int bIdsLength = reply.bridgeDomainDetails.size();
214         LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
215         if (bIdsLength == 0) {
216             // No bridge domains
217             return Collections.emptyList();
218         }
219
220         final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
221         for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
222             logBridgeDomainDetails(detail);
223
224             final String bName = bdContext.getName(detail.bdId, context.getMappingContext());
225             LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
226             allIds.add(new BridgeDomainKey(bName));
227         }
228
229         return allIds;
230     }
231
232     @Override
233     public void merge(@Nonnull final Builder<? extends DataObject> builder,
234                       @Nonnull final List<BridgeDomain> readData) {
235         ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);
236     }
237 }