b5720a42785fc42fcbf2f38013e3ce63e426bc09
[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, final Interconnection ic, final WriteContext writeContext)
63         throws WriteFailedException {
64         try {
65             if (ic == null) { // TODO in case of update we should delete interconnection
66                 LOG.trace("Interconnection is not set. Skipping");
67             } else if (ic instanceof XconnectBased) {
68                 setXconnectBasedL2(swIfIndex, ifcName, (XconnectBased) ic, writeContext, (byte) 1 /*enable*/);
69             } else if (ic instanceof BridgeBased) {
70                 setBridgeBasedL2(swIfIndex, ifcName, (BridgeBased) ic, writeContext, (byte) 1 /*enable*/);
71             } else {
72                 // FIXME how does choice extensibility work
73                 // FIXME it is not even possible to create a dedicated customizer for Interconnection, since it's not a DataObject
74                 // FIXME we might need a choice customizer
75                 // THis choice is already from augment, so its probably not possible to augment augmented choice
76                 LOG.error("Unable to handle Interconnection of type {}", ic.getClass());
77                 throw new WriteFailedException(id, "Unable to handle Interconnection of type " + ic.getClass());
78             }
79         } catch (VppBaseCallException e) {
80             LOG.warn("Failed to update bridge/xconnect based interconnection flags for: {}, interconnection: {}",
81                 ifcName, ic);
82             throw new WriteFailedException(id, "Unable to handle Interconnection of type " + ic.getClass(), e);
83         }
84     }
85
86     void deleteInterconnection(final InstanceIdentifier<? extends DataObject> id, final int swIfIndex,
87                                final String ifcName, final Interconnection ic, final WriteContext writeContext)
88         throws WriteFailedException {
89         try {
90             if (ic == null) { // TODO in case of update we should delete interconnection
91                 LOG.trace("Interconnection is not set. Skipping");
92             } else if (ic instanceof XconnectBased) {
93                 setXconnectBasedL2(swIfIndex, ifcName, (XconnectBased) ic, writeContext, (byte) 0 /*disable*/);
94             } else if (ic instanceof BridgeBased) {
95                 setBridgeBasedL2(swIfIndex, ifcName, (BridgeBased) ic, writeContext, (byte) 0 /*disable*/);
96             } else {
97                 LOG.error("Unable to delete Interconnection of type {}", ic.getClass());
98                 throw new WriteFailedException(id, "Unable to delete Interconnection of type " + ic.getClass());
99             }
100         } catch (VppBaseCallException e) {
101             LOG.warn("Failed to delete bridge/xconnect based interconnection flags for: {}, interconnection: {}",
102                 ifcName, ic);
103             throw new WriteFailedException(id, "Unable to delete Interconnection of type " + ic.getClass(), e);
104         }
105     }
106
107     private void setBridgeBasedL2(final int swIfIndex, final String ifcName, final BridgeBased bb,
108                                   final WriteContext writeContext, final byte enabled)
109         throws VppBaseCallException {
110         LOG.debug("Setting bridge based interconnection(bridge-domain={}) for interface: {}", bb.getBridgeDomain(),
111             ifcName);
112
113         String bdName = bb.getBridgeDomain();
114
115         int bdId = bridgeDomainContext.getIndex(bdName, writeContext.getMappingContext());
116         checkArgument(bdId > 0, "Unable to set Interconnection for Interface: %s, bridge domain: %s does not exist",
117             ifcName, bdName);
118
119         byte bvi = bb.isBridgedVirtualInterface()
120             ? (byte) 1
121             : (byte) 0;
122         byte shg = 0;
123         if (bb.getSplitHorizonGroup() != null) {
124             shg = bb.getSplitHorizonGroup().byteValue();
125         }
126
127         final CompletionStage<SwInterfaceSetL2BridgeReply> swInterfaceSetL2BridgeReplyCompletionStage = futureJvpp
128             .swInterfaceSetL2Bridge(getL2BridgeRequest(swIfIndex, bdId, shg, bvi, enabled));
129         TranslateUtils.getReply(swInterfaceSetL2BridgeReplyCompletionStage.toCompletableFuture());
130
131         LOG.debug("Bridge based interconnection updated successfully for: {}, interconnection: {}", ifcName, bb);
132     }
133
134     private SwInterfaceSetL2Bridge getL2BridgeRequest(final int swIfIndex, final int bdId, final byte shg,
135                                                       final byte bvi, final byte enabled) {
136         final SwInterfaceSetL2Bridge swInterfaceSetL2Bridge = new SwInterfaceSetL2Bridge();
137         swInterfaceSetL2Bridge.rxSwIfIndex = swIfIndex;
138         swInterfaceSetL2Bridge.bdId = bdId;
139         swInterfaceSetL2Bridge.shg = shg;
140         swInterfaceSetL2Bridge.bvi = bvi;
141         swInterfaceSetL2Bridge.enable = enabled;
142         return swInterfaceSetL2Bridge;
143     }
144
145     private void setXconnectBasedL2(final int swIfIndex, final String ifcName, final XconnectBased ic,
146                                     final WriteContext writeContext, final byte enabled)
147         throws VppBaseCallException {
148         String outSwIfName = ic.getXconnectOutgoingInterface();
149         LOG.debug("Setting xconnect based interconnection(outgoing ifc={}) for interface: {}", outSwIfName, ifcName);
150
151         int outSwIfIndex = interfaceContext.getIndex(outSwIfName, writeContext.getMappingContext());
152         checkArgument(outSwIfIndex > 0,
153             "Unable to set Interconnection for Interface: %s, outgoing interface: %s does not exist",
154             ifcName, outSwIfIndex);
155
156         final CompletionStage<SwInterfaceSetL2XconnectReply> swInterfaceSetL2XconnectReplyCompletionStage =
157             futureJvpp
158                 .swInterfaceSetL2Xconnect(getL2XConnectRequest(swIfIndex, outSwIfIndex, enabled));
159         TranslateUtils.getReply(swInterfaceSetL2XconnectReplyCompletionStage.toCompletableFuture());
160         LOG.debug("Xconnect based interconnection updated successfully for: {}, interconnection: {}", ifcName, ic);
161     }
162
163     private SwInterfaceSetL2Xconnect getL2XConnectRequest(final int rxIfc, final int txIfc,
164                                                           final byte enabled) {
165
166         final SwInterfaceSetL2Xconnect swInterfaceSetL2Xconnect = new SwInterfaceSetL2Xconnect();
167         swInterfaceSetL2Xconnect.enable = enabled;
168         swInterfaceSetL2Xconnect.rxSwIfIndex = rxIfc;
169         swInterfaceSetL2Xconnect.txSwIfIndex = txIfc;
170         return swInterfaceSetL2Xconnect;
171     }
172 }