2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.vppstate;
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Iterables;
21 import io.fd.honeycomb.translate.read.ReadContext;
22 import io.fd.honeycomb.translate.read.ReadFailedException;
23 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
24 import io.fd.honeycomb.translate.v3po.util.ByteDataTranslator;
25 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.v3po.util.NamingContext;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.BridgeDomainsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainKey;
35 import org.opendaylight.yangtools.concepts.Builder;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.openvpp.jvpp.core.dto.BridgeDomainDetails;
39 import org.openvpp.jvpp.core.dto.BridgeDomainDetailsReplyDump;
40 import org.openvpp.jvpp.core.dto.BridgeDomainDump;
41 import org.openvpp.jvpp.core.future.FutureJVppCore;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public final class BridgeDomainCustomizer extends FutureJVppCustomizer
46 implements ListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder>, ByteDataTranslator {
48 private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
49 private final NamingContext bdContext;
51 public BridgeDomainCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
52 @Nonnull final NamingContext bdContext) {
53 super(futureJVppCore);
54 this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
58 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
59 @Nonnull final BridgeDomainBuilder builder, @Nonnull final ReadContext context)
60 throws ReadFailedException {
61 LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
62 id, builder, context);
64 final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
65 LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);
67 final int bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
68 LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);
70 BridgeDomainDetailsReplyDump reply;
71 BridgeDomainDetails bridgeDomainDetails;
72 final BridgeDomainDump request = new BridgeDomainDump();
73 request.bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
75 reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
76 bridgeDomainDetails = Iterables.getOnlyElement(reply.bridgeDomainDetails);
77 } catch (Exception e) {
78 LOG.debug("Unable to read bridge domain: {}", key.getName(), e);
82 logBridgeDomainDetails(bridgeDomainDetails);
84 builder.setName(key.getName());
85 builder.setArpTermination(byteToBoolean(bridgeDomainDetails.arpTerm));
86 builder.setFlood(byteToBoolean(bridgeDomainDetails.flood));
87 builder.setForward(byteToBoolean(bridgeDomainDetails.forward));
88 builder.setLearn(byteToBoolean(bridgeDomainDetails.learn));
89 builder.setUnknownUnicastFlood(byteToBoolean(bridgeDomainDetails.uuFlood));
92 private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
93 LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
94 if (bridgeDomainDetails != null) {
95 LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
96 LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
97 LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
98 LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
99 LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
100 LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
101 LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
102 LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
108 public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
109 return new BridgeDomainBuilder();
114 public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
115 @Nonnull final ReadContext context) throws ReadFailedException {
116 final BridgeDomainDump request = new BridgeDomainDump();
117 request.bdId = -1; // dump call
119 BridgeDomainDetailsReplyDump reply;
121 reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
122 } catch (Exception e) {
123 throw new ReadFailedException(id, e);
126 if (reply == null || reply.bridgeDomainDetails == null) {
127 return Collections.emptyList();
130 final int bIdsLength = reply.bridgeDomainDetails.size();
131 LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
132 if (bIdsLength == 0) {
134 return Collections.emptyList();
137 final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
138 for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
139 logBridgeDomainDetails(detail);
141 final String bName = bdContext.getName(detail.bdId, context.getMappingContext());
142 LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
143 allIds.add(new BridgeDomainKey(bName));
150 public void merge(@Nonnull final Builder<? extends DataObject> builder,
151 @Nonnull final List<BridgeDomain> readData) {
152 ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);