2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
19 import static com.google.common.base.Preconditions.checkState;
20 import static java.util.Objects.requireNonNull;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import java.util.Optional;
26 import java.util.concurrent.CompletableFuture;
27 import javax.annotation.Nonnull;
28 import javax.annotation.Nullable;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.Interconnection;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBasedBuilder;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import io.fd.vpp.jvpp.VppBaseCallException;
33 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetails;
34 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetailsReplyDump;
35 import io.fd.vpp.jvpp.core.dto.BridgeDomainDump;
36 import io.fd.vpp.jvpp.core.dto.BridgeDomainSwIfDetails;
37 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
38 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Utility class providing Interconnection read support.
45 final class InterconnectionReadUtils implements InterfaceDataTranslator {
47 private static final Logger LOG = LoggerFactory.getLogger(InterconnectionReadUtils.class);
49 private final FutureJVppCore futureJVppCore;
50 private final NamingContext interfaceContext;
51 private final NamingContext bridgeDomainContext;
53 InterconnectionReadUtils(@Nonnull final FutureJVppCore futureJVppCore,
54 @Nonnull final NamingContext interfaceContext,
55 @Nonnull final NamingContext bridgeDomainContext) {
56 this.futureJVppCore = requireNonNull(futureJVppCore, "futureJVppCore should not be null");
57 this.interfaceContext = requireNonNull(interfaceContext, "interfaceContext should not be null");
58 this.bridgeDomainContext = requireNonNull(bridgeDomainContext, "bridgeDomainContext should not be null");
62 Interconnection readInterconnection(@Nonnull final InstanceIdentifier<?> id, @Nonnull final String ifaceName,
63 @Nonnull final ReadContext ctx)
64 throws ReadFailedException {
65 final int ifaceId = interfaceContext.getIndex(ifaceName, ctx.getMappingContext());
67 final SwInterfaceDetails iface = getVppInterfaceDetails(futureJVppCore, id, ifaceName,
68 ifaceId, ctx.getModificationCache(), LOG);
69 LOG.debug("Interface details for interface: {}, details: {}", ifaceName, iface);
71 final BridgeDomainDetailsReplyDump dumpReply = getDumpReply(id);
72 final Optional<BridgeDomainSwIfDetails> bdForInterface = getBridgeDomainForInterface(ifaceId, dumpReply);
73 if (bdForInterface.isPresent()) {
74 final BridgeDomainSwIfDetails bdSwIfDetails = bdForInterface.get();
75 final BridgeBasedBuilder bbBuilder = new BridgeBasedBuilder();
76 bbBuilder.setBridgeDomain(bridgeDomainContext.getName(bdSwIfDetails.bdId, ctx.getMappingContext()));
78 // Set BVI if the bridgeDomainDetails.bviSwIfIndex == current sw if index
79 final Optional<BridgeDomainDetails> bridgeDomainForInterface =
80 getBridgeDomainForInterface(dumpReply, bdForInterface.get().bdId);
81 // Since we already found an interface assigned to a bridge domain, the details for BD must be present
82 checkState(bridgeDomainForInterface.isPresent());
83 if (bridgeDomainForInterface.get().bviSwIfIndex == ifaceId) {
84 bbBuilder.setBridgedVirtualInterface(true);
86 bbBuilder.setBridgedVirtualInterface(false);
89 if (bdSwIfDetails.shg != 0) {
90 bbBuilder.setSplitHorizonGroup((short) bdSwIfDetails.shg);
92 return bbBuilder.build();
94 // TODO HONEYCOMB-190 is there a way to check if interconnection is XconnectBased?
99 private Optional<BridgeDomainSwIfDetails> getBridgeDomainForInterface(final int ifaceId,
100 final BridgeDomainDetailsReplyDump reply) {
101 if (null == reply || null == reply.bridgeDomainSwIfDetails || reply.bridgeDomainSwIfDetails.isEmpty()) {
102 return Optional.empty();
104 // interface can be added to only one BD only
105 return reply.bridgeDomainSwIfDetails.stream().filter(a -> a.swIfIndex == ifaceId).findFirst();
108 private Optional<BridgeDomainDetails> getBridgeDomainForInterface(final BridgeDomainDetailsReplyDump reply,
110 return reply.bridgeDomainDetails.stream().filter(a -> a.bdId == bdId).findFirst();
113 private BridgeDomainDetailsReplyDump getDumpReply(@Nonnull final InstanceIdentifier<?> id)
114 throws ReadFailedException {
116 // We need to perform full bd dump, because there is no way
117 // to ask VPP for BD details given interface id/name (TODO HONEYCOMB-190 add it to vpp.api?)
118 // TODO HONEYCOMB-190 cache dump result
119 final BridgeDomainDump request = new BridgeDomainDump();
122 final CompletableFuture<BridgeDomainDetailsReplyDump> bdCompletableFuture =
123 futureJVppCore.bridgeDomainSwIfDump(request).toCompletableFuture();
124 return getReplyForRead(bdCompletableFuture, id);
125 } catch (VppBaseCallException e) {
126 throw new ReadFailedException(id, e);