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