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.Initialized;
24 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
26 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetails;
29 import io.fd.vpp.jvpp.core.dto.BridgeDomainDetailsReplyDump;
30 import io.fd.vpp.jvpp.core.dto.BridgeDomainDump;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.Vpp;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.BridgeDomains;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.state.BridgeDomainsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.state.bridge.domains.BridgeDomain;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.state.bridge.domains.BridgeDomainBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.state.bridge.domains.BridgeDomainKey;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public final class BridgeDomainCustomizer extends FutureJVppCustomizer
49 implements InitializingListReaderCustomizer<BridgeDomain, BridgeDomainKey, BridgeDomainBuilder>, ByteDataTranslator {
51 private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
52 private final NamingContext bdContext;
54 public BridgeDomainCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
55 @Nonnull final NamingContext bdContext) {
56 super(futureJVppCore);
57 this.bdContext = Preconditions.checkNotNull(bdContext, "bdContext should not be null");
61 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
62 @Nonnull final BridgeDomainBuilder builder, @Nonnull final ReadContext context)
63 throws ReadFailedException {
64 LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: id={}, builderbuilder={}, context={}",
65 id, builder, context);
67 final BridgeDomainKey key = id.firstKeyOf(id.getTargetType());
68 LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: key={}", key);
70 final int bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
71 LOG.debug("vppstate.BridgeDomainCustomizer.readCurrentAttributes: bdId={}", bdId);
73 BridgeDomainDetailsReplyDump reply;
74 BridgeDomainDetails bridgeDomainDetails;
75 final BridgeDomainDump request = new BridgeDomainDump();
76 request.bdId = bdContext.getIndex(key.getName(), context.getMappingContext());
78 reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
79 bridgeDomainDetails = Iterables.getOnlyElement(reply.bridgeDomainDetails);
80 } catch (Exception e) {
81 LOG.debug("Unable to read bridge domain: {}", key.getName(), e);
85 logBridgeDomainDetails(bridgeDomainDetails);
87 builder.setName(key.getName());
88 builder.setArpTermination(byteToBoolean(bridgeDomainDetails.arpTerm));
89 builder.setFlood(byteToBoolean(bridgeDomainDetails.flood));
90 builder.setForward(byteToBoolean(bridgeDomainDetails.forward));
91 builder.setLearn(byteToBoolean(bridgeDomainDetails.learn));
92 builder.setUnknownUnicastFlood(byteToBoolean(bridgeDomainDetails.uuFlood));
95 private void logBridgeDomainDetails(final BridgeDomainDetails bridgeDomainDetails) {
96 LOG.debug("bridgeDomainDetails={}", bridgeDomainDetails);
97 if (bridgeDomainDetails != null) {
98 LOG.debug("bridgeDomainDetails.arpTerm={}", bridgeDomainDetails.arpTerm);
99 LOG.debug("bridgeDomainDetails.bdId={}", bridgeDomainDetails.bdId);
100 LOG.debug("bridgeDomainDetails.bviSwIfIndex={}", bridgeDomainDetails.bviSwIfIndex);
101 LOG.debug("bridgeDomainDetails.flood={}", bridgeDomainDetails.flood);
102 LOG.debug("bridgeDomainDetails.forward={}", bridgeDomainDetails.forward);
103 LOG.debug("bridgeDomainDetails.learn={}", bridgeDomainDetails.learn);
104 LOG.debug("bridgeDomainDetails.nSwIfs={}", bridgeDomainDetails.nSwIfs);
105 LOG.debug("bridgeDomainDetails.uuFlood={}", bridgeDomainDetails.uuFlood);
111 public BridgeDomainBuilder getBuilder(@Nonnull final InstanceIdentifier<BridgeDomain> id) {
112 return new BridgeDomainBuilder();
117 public List<BridgeDomainKey> getAllIds(@Nonnull final InstanceIdentifier<BridgeDomain> id,
118 @Nonnull final ReadContext context) throws ReadFailedException {
119 final BridgeDomainDump request = new BridgeDomainDump();
120 request.bdId = -1; // dump call
122 BridgeDomainDetailsReplyDump reply;
124 reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get();
125 } catch (Exception e) {
126 throw new ReadFailedException(id, e);
129 if (reply == null || reply.bridgeDomainDetails == null) {
130 return Collections.emptyList();
133 final int bIdsLength = reply.bridgeDomainDetails.size();
134 LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bIds.length={}", bIdsLength);
135 if (bIdsLength == 0) {
137 return Collections.emptyList();
140 final List<BridgeDomainKey> allIds = new ArrayList<>(bIdsLength);
141 for (BridgeDomainDetails detail : reply.bridgeDomainDetails) {
142 logBridgeDomainDetails(detail);
144 final String bName = bdContext.getName(detail.bdId, context.getMappingContext());
145 LOG.debug("vppstate.BridgeDomainCustomizer.getAllIds: bName={}", bName);
146 allIds.add(new BridgeDomainKey(bName));
153 public void merge(@Nonnull final Builder<? extends DataObject> builder,
154 @Nonnull final List<BridgeDomain> readData) {
155 ((BridgeDomainsBuilder) builder).setBridgeDomain(readData);
159 public Initialized<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomain> init(
160 @Nonnull final InstanceIdentifier<BridgeDomain> id,
161 @Nonnull final BridgeDomain readValue,
162 @Nonnull final ReadContext ctx) {
163 return Initialized.create(getCfgId(id),
164 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomainBuilder()
165 .setName(readValue.getName())
166 .setLearn(readValue.isLearn())
167 .setUnknownUnicastFlood(readValue.isUnknownUnicastFlood())
168 .setArpTermination(readValue.isArpTermination())
169 .setFlood(readValue.isFlood())
170 .setForward(readValue.isForward())
174 static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomain> getCfgId(
175 final InstanceIdentifier<BridgeDomain> id) {
176 return InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(
177 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomain.class,
178 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.vpp.bridge.domains.BridgeDomainKey(
179 id.firstKeyOf(BridgeDomain.class).getName()));