/* * 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.translate.v3po.vppstate; import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.byteToBoolean; import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer; import io.fd.honeycomb.translate.v3po.util.NamingContext; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.BridgeDomainsBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomain; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.state.bridge.domains.BridgeDomainKey; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.openvpp.jvpp.core.dto.BridgeDomainDetails; import org.openvpp.jvpp.core.dto.BridgeDomainDetailsReplyDump; import org.openvpp.jvpp.core.dto.BridgeDomainDump; import org.openvpp.jvpp.core.future.FutureJVppCore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class BridgeDomainCustomizer extends FutureJVppCustomizer implements ListReaderCustomizer { 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 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); if (bridgeDomainDetails != null) { 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 id) { return new BridgeDomainBuilder(); } @Nonnull @Override public List getAllIds(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext context) { final BridgeDomainDump request = new BridgeDomainDump(); request.bdId = -1; // dump call BridgeDomainDetailsReplyDump reply; try { reply = getFutureJVpp().bridgeDomainDump(request).toCompletableFuture().get(); } catch (Exception e) { throw new IllegalStateException("Bridge domain dump failed", e); // TODO ReadFailedException? } 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 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 builder, @Nonnull final List readData) { ((BridgeDomainsBuilder) builder).setBridgeDomain(readData); } }