summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/l2state/BridgeDomainCustomizer.java
blob: f3d90f21c543f9725931d8a40d09f43bb226062c (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * 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.hc2vpp.v3po.l2state;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.read.ReadContext;
import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.spi.read.Initialized;
import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
import io.fd.jvpp.core.dto.BridgeDomainDetails;
import io.fd.jvpp.core.dto.BridgeDomainDetailsReplyDump;
import io.fd.jvpp.core.dto.BridgeDomainDump;
import io.fd.jvpp.core.future.FutureJVppCore;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.BridgeDomains;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.BridgeDomainsStateBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.state.BridgeDomain;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.state.BridgeDomainBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.state.BridgeDomainKey;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class BridgeDomainCustomizer extends FutureJVppCustomizer
        implements InitializingListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder>, ByteDataTranslator {

    private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
    private final NamingContext bdContext;

    public BridgeDomainCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
                                  @Nonnull final NamingContext bdContext) {
        super(futureJVppCore);
        this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
    }

    @Override
    public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
                                      @Nonnull final BridgeDomainBuilder builder, @Nonnull final ReadContext context)
            throws ReadFailedException {
        LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
                id, builder, context);

        final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
        LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);

        final int bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
        LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);

        BridgeDomainDetailsReplyDump reply;
        BridgeDomainDetails bridgeDomainDetails;
        final BridgeDomainDump request = new BridgeDomainDump();
        request.bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
        try {
            reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
            bridgeDomainDetails = Iterables.getOnlyElement(reply.bridgeDomainDetails);
        } catch (Exception e) {
            LOG.debug("Unable to read bridge domain: {}", key.getName(), e);
            return;
        }

        logBridgeDomainDetails(bridgeDomainDetails);

        builder.setName(key.getName());
        builder.setArpTermination(byteToBoolean(bridgeDomainDetails.arpTerm));
        builder.setFlood(byteToBoolean(bridgeDomainDetails.flood));
        builder.setForward(byteToBoolean(bridgeDomainDetails.forward));
        builder.setLearn(byteToBoolean(bridgeDomainDetails.learn));
        builder.setUnknownUnicastFlood(byteToBoolean(bridgeDomainDetails.uuFlood));
    }

    private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
        LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
        LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
        LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
        LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
        LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
        LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
        LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
        LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
        LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
    }

    @Nonnull
    @Override
    public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
        return new BridgeDomainBuilder();
    }

    @Nonnull
    @Override
    public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
                                           @Nonnull final ReadContext context) throws ReadFailedException {
        final BridgeDomainDump request = new BridgeDomainDump();
        request.bdId = -1; // dump call

        BridgeDomainDetailsReplyDump reply;
        try {
            reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
        } catch (Exception e) {
            throw new ReadFailedException(id, e);
        }

        if (reply == null || reply.bridgeDomainDetails == null) {
            return Collections.emptyList();
        }

        final int bIdsLength = reply.bridgeDomainDetails.size();
        LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
        if (bIdsLength == 0) {
            // No bridge domains
            return Collections.emptyList();
        }

        final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
        for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
            logBridgeDomainDetails(detail);

            final String bName = bdContext.getName(detail.bdId, context.getMappingContext());
            LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
            allIds.add(new BridgeDomainKey(bName));
        }

        return allIds;
    }

    @Override
    public void merge(@Nonnull final Builder<? extends DataObject> builder,
                      @Nonnull final List<BridgeDomain> readData) {
        ((BridgeDomainsStateBuilder) builder).setBridgeDomain(readData);
    }

    @Override
    public Initialized<org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.BridgeDomain> init(
            @Nonnull final InstanceIdentifier<BridgeDomain> id,
            @Nonnull final BridgeDomain readValue,
            @Nonnull final ReadContext ctx) {
        return Initialized.create(getCfgId(id),
                new org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.BridgeDomainBuilder()
                        .setName(readValue.getName())
                        .setLearn(readValue.isLearn())
                        .setUnknownUnicastFlood(readValue.isUnknownUnicastFlood())
                        .setArpTermination(readValue.isArpTermination())
                        .setFlood(readValue.isFlood())
                        .setForward(readValue.isForward())
                        .build());
    }

    static InstanceIdentifier<org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.BridgeDomain> getCfgId(
            final InstanceIdentifier<BridgeDomain> id) {
        return InstanceIdentifier.create(BridgeDomains.class).child(
                org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.BridgeDomain.class,
                new org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.bridge.domains.BridgeDomainKey(
                        id.firstKeyOf(BridgeDomain.class).getName()));
    }
}