addb425f6d4d5bd294d08c8bd057181adeab51f9
[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 = Lists.newArrayListWithCapacity(dump.l2FibTableEntry.size());
113             for (L2FibTableEntry entry : dump.l2FibTableEntry) {
114                 // entry.mac is a long value in the format 66:55:44:33:22:11:XX:XX
115                 // where mac address is 11:22:33:44:55:66
116                 final PhysAddress address = new PhysAddress(getMacAddress(Longs.toByteArray(entry.mac)));
117                 l2Fibs.add(new L2FibBuilder()
118                         .setAction((byteToBoolean(entry.filterMac)
119                                 ? L2Fib.Action.Filter
120                                 : L2Fib.Action.Forward))
121                         .setBridgedVirtualInterface(byteToBoolean(entry.bviMac))
122                         .setOutgoingInterface(interfaceContext.getName(entry.swIfIndex))
123                         .setStaticConfig(byteToBoolean(entry.staticMac))
124                         .setPhysAddress(address)
125                         .setKey(new L2FibKey(address))
126                         .build());
127             }
128             builder.setL2Fib(l2Fibs);
129
130         } catch (Exception e) {
131             LOG.warn("Failed to acquire l2FibTableDump for domain id={}", bdId, e);
132         }
133     }
134
135     // TODO move to utils
136     private static Boolean byteToBoolean(final byte aByte) {
137         if (aByte == 0) {
138             return Boolean.FALSE;
139         } else if (aByte == 1) {
140             return Boolean.TRUE;
141         }
142         throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", aByte));
143     }
144
145     private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
146         LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
147         if (bridgeDomainDetails != null) {
148             LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
149             LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
150             LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
151             LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
152             LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
153             LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
154             LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
155             LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
156         }
157     }
158
159     // TODO move to some utility class
160     private static String getMacAddress(byte[] mac) {
161         StringBuilder sb = new StringBuilder(18);
162         for (int i=5; i>=0; --i) {
163             if (sb.length() > 0) {
164                 sb.append(':');
165             }
166             sb.append(String.format("%02x", mac[i]));
167         }
168         return sb.toString();
169     }
170
171     private List<Interface> getIfcs(final BridgeDomainDetails bridgeDomainDetails,
172                                     final List<BridgeDomainSwIfDetails> bridgeDomainSwIfDetails) {
173         final List<Interface> ifcs = new ArrayList<>(bridgeDomainSwIfDetails.size());
174         for (BridgeDomainSwIfDetails anInterface : bridgeDomainSwIfDetails) {
175             final String interfaceName = interfaceContext.getName(anInterface.swIfIndex);
176             if (anInterface.bdId == bridgeDomainDetails.bdId) {
177                 ifcs.add(new InterfaceBuilder()
178                         .setBridgedVirtualInterface(bridgeDomainDetails.bviSwIfIndex == anInterface.swIfIndex)
179                         .setSplitHorizonGroup((short) anInterface.shg)
180                         .setName(interfaceName)
181                         .setKey(new InterfaceKey(interfaceName))
182                         .build());
183             }
184
185
186         }
187         return ifcs;
188     }
189
190     @Nonnull
191     @Override
192     public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
193         return new BridgeDomainBuilder();
194     }
195
196     @Nonnull
197     @Override
198     public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
199                                            @Nonnull final Context context) {
200         final BridgeDomainDump request = new BridgeDomainDump();
201         request.bdId = -1; // dump call
202
203         BridgeDomainDetailsReplyDump reply;
204         try {
205             reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
206         } catch (Exception e) {
207             throw new IllegalStateException("Bridge domain dump failed", e); // TODO ReadFailedException?
208         }
209
210         if(reply == null || reply.bridgeDomainDetails == null) {
211             return Collections.emptyList();
212         }
213
214         final int bIdsLength = reply.bridgeDomainDetails.size();
215         LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
216         if (bIdsLength == 0) {
217             // No bridge domains
218             return Collections.emptyList();
219         }
220
221         final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
222         for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
223             logBridgeDomainDetails(detail);
224
225             final String bName = bdContext.getName(detail.bdId);
226             LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
227             allIds.add(new BridgeDomainKey(bName));
228         }
229
230         return allIds;
231     }
232
233     @Override
234     public void merge(@Nonnull final Builder<? extends DataObject> builder,
235                       @Nonnull final List<BridgeDomain> readData) {
236         ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);
237     }
238 }