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