summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterconnectionWriteUtils.java
blob: 463acc84e82e5ff91b87f272060e9da3da65f007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.v3po.translate.v3po.interfaces;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
import io.fd.honeycomb.v3po.translate.write.WriteContext;
import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.Interconnection;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBased;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.XconnectBased;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.openvpp.jvpp.VppBaseCallException;
import org.openvpp.jvpp.dto.SwInterfaceSetL2Bridge;
import org.openvpp.jvpp.dto.SwInterfaceSetL2BridgeReply;
import org.openvpp.jvpp.dto.SwInterfaceSetL2Xconnect;
import org.openvpp.jvpp.dto.SwInterfaceSetL2XconnectReply;
import org.openvpp.jvpp.future.FutureJVpp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class providing Interconnection CUD support.
 */
final class InterconnectionWriteUtils {

    private static final Logger LOG = LoggerFactory.getLogger(InterconnectionWriteUtils.class);

    private final FutureJVpp futureJvpp;
    private final NamingContext interfaceContext;
    private final NamingContext bridgeDomainContext;

    InterconnectionWriteUtils(@Nonnull final FutureJVpp futureJvpp,
                              @Nonnull final NamingContext interfaceContext,
                              @Nonnull final NamingContext bridgeDomainContext) {
        this.futureJvpp = requireNonNull(futureJvpp, "futureJvpp should not be null");
        this.interfaceContext = requireNonNull(interfaceContext, "interfaceContext should not be null");
        this.bridgeDomainContext = requireNonNull(bridgeDomainContext, "bridgeDomainContext should not be null");
    }

    void setInterconnection(final InstanceIdentifier<? extends DataObject> id, final int swIfIndex,
                            final String ifcName,
                            final Interconnection ic, final WriteContext writeContext)
        throws WriteFailedException {
        try {
            if (ic == null) { // TODO in case of update we should delete interconnection
                LOG.trace("Interconnection is not set. Skipping");
            } else if (ic instanceof XconnectBased) {
                setXconnectBasedL2(swIfIndex, ifcName, (XconnectBased) ic, writeContext);
            } else if (ic instanceof BridgeBased) {
                setBridgeBasedL2(swIfIndex, ifcName, (BridgeBased) ic, writeContext);
            } else {
                // FIXME how does choice extensibility work
                // FIXME it is not even possible to create a dedicated customizer for Interconnection, since it's not a DataObject
                // FIXME we might need a choice customizer
                // THis choice is already from augment, so its probably not possible to augment augmented choice
                LOG.error("Unable to handle Interconnection of type {}", ic.getClass());
                throw new WriteFailedException(id, "Unable to handle Interconnection of type " + ic.getClass());
            }
        } catch (VppBaseCallException e) {
            LOG.warn("Failed to update bridge/xconnect based interconnection flags for: {}, interconnection: {}",
                ifcName, ic);
            throw new WriteFailedException(id, "Unable to handle Interconnection of type " + ic.getClass(), e);
        }
    }

    private void setBridgeBasedL2(final int swIfIndex, final String ifcName, final BridgeBased bb,
                                  final WriteContext writeContext)
        throws VppBaseCallException {
        LOG.debug("Setting bridge based interconnection(bridge-domain={}) for interface: {}", bb.getBridgeDomain(),
            ifcName);

        String bdName = bb.getBridgeDomain();

        int bdId = bridgeDomainContext.getIndex(bdName, writeContext.getMappingContext());
        checkArgument(bdId > 0, "Unable to set Interconnection for Interface: %s, bridge domain: %s does not exist",
            ifcName, bdName);

        byte bvi = bb.isBridgedVirtualInterface()
            ? (byte) 1
            : (byte) 0;
        byte shg = 0;
        if (bb.getSplitHorizonGroup() != null) {
            shg = bb.getSplitHorizonGroup().byteValue();
        }

        final CompletionStage<SwInterfaceSetL2BridgeReply> swInterfaceSetL2BridgeReplyCompletionStage = futureJvpp
            .swInterfaceSetL2Bridge(getL2BridgeRequest(swIfIndex, bdId, shg, bvi, (byte) 1 /* enable */));
        final SwInterfaceSetL2BridgeReply reply =
            TranslateUtils.getReply(swInterfaceSetL2BridgeReplyCompletionStage.toCompletableFuture());

        LOG.debug("Bridge based interconnection updated successfully for: {}, interconnection: {}", ifcName, bb);
    }

    private SwInterfaceSetL2Bridge getL2BridgeRequest(final int swIfIndex, final int bdId, final byte shg,
                                                      final byte bvi, final byte enabled) {
        final SwInterfaceSetL2Bridge swInterfaceSetL2Bridge = new SwInterfaceSetL2Bridge();
        swInterfaceSetL2Bridge.rxSwIfIndex = swIfIndex;
        swInterfaceSetL2Bridge.bdId = bdId;
        swInterfaceSetL2Bridge.shg = shg;
        swInterfaceSetL2Bridge.bvi = bvi;
        swInterfaceSetL2Bridge.enable = enabled;
        return swInterfaceSetL2Bridge;
    }

    private void setXconnectBasedL2(final int swIfIndex, final String ifcName, final XconnectBased ic,
                                    final WriteContext writeContext)
        throws VppBaseCallException {
        String outSwIfName = ic.getXconnectOutgoingInterface();
        LOG.debug("Setting xconnect based interconnection(outgoing ifc={}) for interface: {}", outSwIfName, ifcName);

        int outSwIfIndex = interfaceContext.getIndex(outSwIfName, writeContext.getMappingContext());
        checkArgument(outSwIfIndex > 0,
            "Unable to set Interconnection for Interface: %s, outgoing interface: %s does not exist",
            ifcName, outSwIfIndex);

        final CompletionStage<SwInterfaceSetL2XconnectReply> swInterfaceSetL2XconnectReplyCompletionStage =
            futureJvpp
                .swInterfaceSetL2Xconnect(getL2XConnectRequest(swIfIndex, outSwIfIndex, (byte) 1 /* enable */));
        final SwInterfaceSetL2XconnectReply reply =
            TranslateUtils.getReply(swInterfaceSetL2XconnectReplyCompletionStage.toCompletableFuture());
        LOG.debug("Xconnect based interconnection updated successfully for: {}, interconnection: {}", ifcName, ic);
    }

    private SwInterfaceSetL2Xconnect getL2XConnectRequest(final int rxIfc, final int txIfc,
                                                          final byte enabled) {

        final SwInterfaceSetL2Xconnect swInterfaceSetL2Xconnect = new SwInterfaceSetL2Xconnect();
        swInterfaceSetL2Xconnect.enable = enabled;
        swInterfaceSetL2Xconnect.rxSwIfIndex = rxIfc;
        swInterfaceSetL2Xconnect.txSwIfIndex = txIfc;
        return swInterfaceSetL2Xconnect;
    }
}