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