463acc84e82e5ff91b87f272060e9da3da65f007
[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.interfaces;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static java.util.Objects.requireNonNull;
21
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
24 import io.fd.honeycomb.v3po.translate.write.WriteContext;
25 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
26 import java.util.concurrent.CompletionStage;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.Interconnection;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBased;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.XconnectBased;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.openvpp.jvpp.VppBaseCallException;
34 import org.openvpp.jvpp.dto.SwInterfaceSetL2Bridge;
35 import org.openvpp.jvpp.dto.SwInterfaceSetL2BridgeReply;
36 import org.openvpp.jvpp.dto.SwInterfaceSetL2Xconnect;
37 import org.openvpp.jvpp.dto.SwInterfaceSetL2XconnectReply;
38 import org.openvpp.jvpp.future.FutureJVpp;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Utility class providing Interconnection CUD support.
44  */
45 final class InterconnectionWriteUtils {
46
47     private static final Logger LOG = LoggerFactory.getLogger(InterconnectionWriteUtils.class);
48
49     private final FutureJVpp futureJvpp;
50     private final NamingContext interfaceContext;
51     private final NamingContext bridgeDomainContext;
52
53     InterconnectionWriteUtils(@Nonnull final FutureJVpp futureJvpp,
54                               @Nonnull final NamingContext interfaceContext,
55                               @Nonnull final NamingContext bridgeDomainContext) {
56         this.futureJvpp = requireNonNull(futureJvpp, "futureJvpp should not be null");
57         this.interfaceContext = requireNonNull(interfaceContext, "interfaceContext should not be null");
58         this.bridgeDomainContext = requireNonNull(bridgeDomainContext, "bridgeDomainContext should not be null");
59     }
60
61     void setInterconnection(final InstanceIdentifier<? extends DataObject> id, final int swIfIndex,
62                             final String ifcName,
63                             final Interconnection ic, final WriteContext writeContext)
64         throws WriteFailedException {
65         try {
66             if (ic == null) { // TODO in case of update we should delete interconnection
67                 LOG.trace("Interconnection is not set. Skipping");
68             } else if (ic instanceof XconnectBased) {
69                 setXconnectBasedL2(swIfIndex, ifcName, (XconnectBased) ic, writeContext);
70             } else if (ic instanceof BridgeBased) {
71                 setBridgeBasedL2(swIfIndex, ifcName, (BridgeBased) ic, writeContext);
72             } else {
73                 // FIXME how does choice extensibility work
74                 // FIXME it is not even possible to create a dedicated customizer for Interconnection, since it's not a DataObject
75                 // FIXME we might need a choice customizer
76                 // THis choice is already from augment, so its probably not possible to augment augmented choice
77                 LOG.error("Unable to handle Interconnection of type {}", ic.getClass());
78                 throw new WriteFailedException(id, "Unable to handle Interconnection of type " + ic.getClass());
79             }
80         } catch (VppBaseCallException e) {
81             LOG.warn("Failed to update bridge/xconnect based interconnection flags for: {}, interconnection: {}",
82                 ifcName, ic);
83             throw new WriteFailedException(id, "Unable to handle Interconnection of type " + ic.getClass(), e);
84         }
85     }
86
87     private void setBridgeBasedL2(final int swIfIndex, final String ifcName, final BridgeBased bb,
88                                   final WriteContext writeContext)
89         throws VppBaseCallException {
90         LOG.debug("Setting bridge based interconnection(bridge-domain={}) for interface: {}", bb.getBridgeDomain(),
91             ifcName);
92
93         String bdName = bb.getBridgeDomain();
94
95         int bdId = bridgeDomainContext.getIndex(bdName, writeContext.getMappingContext());
96         checkArgument(bdId > 0, "Unable to set Interconnection for Interface: %s, bridge domain: %s does not exist",
97             ifcName, bdName);
98
99         byte bvi = bb.isBridgedVirtualInterface()
100             ? (byte) 1
101             : (byte) 0;
102         byte shg = 0;
103         if (bb.getSplitHorizonGroup() != null) {
104             shg = bb.getSplitHorizonGroup().byteValue();
105         }
106
107         final CompletionStage<SwInterfaceSetL2BridgeReply> swInterfaceSetL2BridgeReplyCompletionStage = futureJvpp
108             .swInterfaceSetL2Bridge(getL2BridgeRequest(swIfIndex, bdId, shg, bvi, (byte) 1 /* enable */));
109         final SwInterfaceSetL2BridgeReply reply =
110             TranslateUtils.getReply(swInterfaceSetL2BridgeReplyCompletionStage.toCompletableFuture());
111
112         LOG.debug("Bridge based interconnection updated successfully for: {}, interconnection: {}", ifcName, bb);
113     }
114
115     private SwInterfaceSetL2Bridge getL2BridgeRequest(final int swIfIndex, final int bdId, final byte shg,
116                                                       final byte bvi, final byte enabled) {
117         final SwInterfaceSetL2Bridge swInterfaceSetL2Bridge = new SwInterfaceSetL2Bridge();
118         swInterfaceSetL2Bridge.rxSwIfIndex = swIfIndex;
119         swInterfaceSetL2Bridge.bdId = bdId;
120         swInterfaceSetL2Bridge.shg = shg;
121         swInterfaceSetL2Bridge.bvi = bvi;
122         swInterfaceSetL2Bridge.enable = enabled;
123         return swInterfaceSetL2Bridge;
124     }
125
126     private void setXconnectBasedL2(final int swIfIndex, final String ifcName, final XconnectBased ic,
127                                     final WriteContext writeContext)
128         throws VppBaseCallException {
129         String outSwIfName = ic.getXconnectOutgoingInterface();
130         LOG.debug("Setting xconnect based interconnection(outgoing ifc={}) for interface: {}", outSwIfName, ifcName);
131
132         int outSwIfIndex = interfaceContext.getIndex(outSwIfName, writeContext.getMappingContext());
133         checkArgument(outSwIfIndex > 0,
134             "Unable to set Interconnection for Interface: %s, outgoing interface: %s does not exist",
135             ifcName, outSwIfIndex);
136
137         final CompletionStage<SwInterfaceSetL2XconnectReply> swInterfaceSetL2XconnectReplyCompletionStage =
138             futureJvpp
139                 .swInterfaceSetL2Xconnect(getL2XConnectRequest(swIfIndex, outSwIfIndex, (byte) 1 /* enable */));
140         final SwInterfaceSetL2XconnectReply reply =
141             TranslateUtils.getReply(swInterfaceSetL2XconnectReplyCompletionStage.toCompletableFuture());
142         LOG.debug("Xconnect based interconnection updated successfully for: {}, interconnection: {}", ifcName, ic);
143     }
144
145     private SwInterfaceSetL2Xconnect getL2XConnectRequest(final int rxIfc, final int txIfc,
146                                                           final byte enabled) {
147
148         final SwInterfaceSetL2Xconnect swInterfaceSetL2Xconnect = new SwInterfaceSetL2Xconnect();
149         swInterfaceSetL2Xconnect.enable = enabled;
150         swInterfaceSetL2Xconnect.rxSwIfIndex = rxIfc;
151         swInterfaceSetL2Xconnect.txSwIfIndex = txIfc;
152         return swInterfaceSetL2Xconnect;
153     }
154 }