Search in sources :

Example 6 with DataObject

use of org.opendaylight.yangtools.yang.binding.DataObject in project openflowplugin by opendaylight.

the class WakeupOnNode method onDataTreeChanged.

@Override
public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Table>> modifications) {
    Short requiredTableId = 0;
    for (DataTreeModification modification : modifications) {
        if (modification.getRootNode().getModificationType() == ModificationType.SUBTREE_MODIFIED) {
            DataObject table = modification.getRootNode().getDataAfter();
            if (table instanceof Table) {
                Table tableSure = (Table) table;
                LOG.trace("table: {}", table);
                if (requiredTableId.equals(tableSure.getId())) {
                    InstanceIdentifier<Table> tablePath = modification.getRootPath().getRootIdentifier();
                    learningSwitchHandler.onSwitchAppeared(tablePath);
                }
            }
        }
    }
}
Also used : DataTreeModification(org.opendaylight.controller.md.sal.binding.api.DataTreeModification) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) Table(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table)

Example 7 with DataObject

use of org.opendaylight.yangtools.yang.binding.DataObject in project controller by opendaylight.

the class DataStoreAppConfigDefaultXMLReader method createDefaultInstance.

@SuppressWarnings("unchecked")
public T createDefaultInstance(final FallbackConfigProvider fallback) throws ConfigXMLReaderException, URISyntaxException, ParserConfigurationException, XMLStreamException, SAXException, IOException {
    YangInstanceIdentifier yangPath = bindingSerializer.toYangInstanceIdentifier(bindingContext.appConfigPath);
    LOG.debug("{}: Creating app config instance from path {}, Qname: {}", logName, yangPath, bindingContext.bindingQName);
    checkNotNull(schemaService, "%s: Could not obtain the SchemaService OSGi service", logName);
    SchemaContext schemaContext = schemaService.getGlobalContext();
    Module module = schemaContext.findModule(bindingContext.bindingQName.getModule()).orElse(null);
    checkNotNull(module, "%s: Could not obtain the module schema for namespace %s, revision %s", logName, bindingContext.bindingQName.getNamespace(), bindingContext.bindingQName.getRevision());
    DataSchemaNode dataSchema = module.getDataChildByName(bindingContext.bindingQName);
    checkNotNull(dataSchema, "%s: Could not obtain the schema for %s", logName, bindingContext.bindingQName);
    checkCondition(bindingContext.schemaType.isAssignableFrom(dataSchema.getClass()), "%s: Expected schema type %s for %s but actual type is %s", logName, bindingContext.schemaType, bindingContext.bindingQName, dataSchema.getClass());
    NormalizedNode<?, ?> dataNode = parsePossibleDefaultAppConfigXMLFile(schemaContext, dataSchema);
    if (dataNode == null) {
        dataNode = fallback.get(schemaService.getGlobalContext(), dataSchema);
    }
    DataObject appConfig = bindingSerializer.fromNormalizedNode(yangPath, dataNode).getValue();
    // This shouldn't happen but need to handle it in case...
    checkNotNull(appConfig, "%s: Could not create instance for app config binding %s", logName, bindingContext.appConfigBindingClass);
    return (T) appConfig;
}
Also used : DataObject(org.opendaylight.yangtools.yang.binding.DataObject) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) Module(org.opendaylight.yangtools.yang.model.api.Module) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 8 with DataObject

use of org.opendaylight.yangtools.yang.binding.DataObject in project controller by opendaylight.

the class DataStoreAppConfigMetadata method createDefaultInstance.

private DataObject createDefaultInstance() {
    try {
        ConfigURLProvider inputStreamProvider = appConfigFileName -> {
            File appConfigFile = new File(DEFAULT_APP_CONFIG_FILE_PATH, appConfigFileName);
            LOG.debug("{}: parsePossibleDefaultAppConfigXMLFile looking for file {}", logName(), appConfigFile.getAbsolutePath());
            if (!appConfigFile.exists()) {
                return Optional.absent();
            }
            LOG.debug("{}: Found file {}", logName(), appConfigFile.getAbsolutePath());
            return Optional.of(appConfigFile.toURI().toURL());
        };
        DataStoreAppConfigDefaultXMLReader<?> reader = new DataStoreAppConfigDefaultXMLReader<>(logName(), defaultAppConfigFileName, getOSGiService(DOMSchemaService.class), bindingSerializer, bindingContext, inputStreamProvider);
        return reader.createDefaultInstance((schemaContext, dataSchema) -> {
            // Fallback if file cannot be read, try XML from Config
            NormalizedNode<?, ?> dataNode = parsePossibleDefaultAppConfigElement(schemaContext, dataSchema);
            if (dataNode == null) {
                // or, as last resort, defaults from the model
                return bindingContext.newDefaultNode(dataSchema);
            } else {
                return dataNode;
            }
        });
    } catch (final ConfigXMLReaderException | IOException | SAXException | XMLStreamException | ParserConfigurationException | URISyntaxException e) {
        if (e.getCause() == null) {
            setFailureMessage(e.getMessage());
        } else {
            setFailure(e.getMessage(), e);
        }
        return null;
    }
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ClusteredDataTreeChangeListener(org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ListenerRegistration(org.opendaylight.yangtools.concepts.ListenerRegistration) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) ExtendedBlueprintContainer(org.apache.aries.blueprint.services.ExtendedBlueprintContainer) Optional(com.google.common.base.Optional) XMLStreamException(javax.xml.stream.XMLStreamException) DataObjectModification(org.opendaylight.controller.md.sal.binding.api.DataObjectModification) BindingNormalizedNodeSerializer(org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer) DataObject(org.opendaylight.yangtools.yang.binding.DataObject) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) Logger(org.slf4j.Logger) Collection(java.util.Collection) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) IOException(java.io.IOException) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) Objects(java.util.Objects) ModificationType(org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType) Futures(com.google.common.util.concurrent.Futures) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) SAXException(org.xml.sax.SAXException) DataTreeModification(org.opendaylight.controller.md.sal.binding.api.DataTreeModification) DOMSchemaService(org.opendaylight.mdsal.dom.api.DOMSchemaService) DataTreeIdentifier(org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ConfigURLProvider(org.opendaylight.controller.blueprint.ext.DataStoreAppConfigDefaultXMLReader.ConfigURLProvider) ConfigURLProvider(org.opendaylight.controller.blueprint.ext.DataStoreAppConfigDefaultXMLReader.ConfigURLProvider) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) DOMSchemaService(org.opendaylight.mdsal.dom.api.DOMSchemaService)

Example 9 with DataObject

use of org.opendaylight.yangtools.yang.binding.DataObject in project controller by opendaylight.

the class DataStoreAppConfigMetadata method setInitialAppConfig.

private boolean setInitialAppConfig(final Optional<DataObject> possibleAppConfig) {
    boolean result = readingInitialAppConfig.compareAndSet(true, false);
    if (result) {
        DataObject localAppConfig;
        if (possibleAppConfig.isPresent()) {
            localAppConfig = possibleAppConfig.get();
        } else {
            // No app config data is present so create an empty instance via the bindingSerializer service.
            // This will also return default values for leafs that haven't been explicitly set.
            localAppConfig = createDefaultInstance();
        }
        LOG.debug("{}: Setting currentAppConfig instance: {}", logName(), localAppConfig);
        // Now publish the app config instance to the volatile field and notify the callback to let the
        // container know our dependency is now satisfied.
        currentAppConfig = localAppConfig;
        setSatisfied();
    }
    return result;
}
Also used : DataObject(org.opendaylight.yangtools.yang.binding.DataObject)

Example 10 with DataObject

use of org.opendaylight.yangtools.yang.binding.DataObject in project controller by opendaylight.

the class DataStoreAppConfigMetadata method readInitialAppConfig.

private void readInitialAppConfig(final DataBroker dataBroker) {
    final ReadOnlyTransaction readOnlyTx = dataBroker.newReadOnlyTransaction();
    ListenableFuture<Optional<DataObject>> future = readOnlyTx.read(LogicalDatastoreType.CONFIGURATION, bindingContext.appConfigPath);
    Futures.addCallback(future, new FutureCallback<Optional<DataObject>>() {

        @Override
        public void onSuccess(final Optional<DataObject> possibleAppConfig) {
            LOG.debug("{}: Read of app config {} succeeded: {}", logName(), bindingContext.appConfigBindingClass.getName(), possibleAppConfig);
            readOnlyTx.close();
            setInitialAppConfig(possibleAppConfig);
        }

        @Override
        public void onFailure(final Throwable failure) {
            readOnlyTx.close();
            // We may have gotten the app config via the data tree change listener so only retry if not.
            if (readingInitialAppConfig.get()) {
                LOG.warn("{}: Read of app config {} failed - retrying", logName(), bindingContext.appConfigBindingClass.getName(), failure);
                readInitialAppConfig(dataBroker);
            }
        }
    }, MoreExecutors.directExecutor());
}
Also used : DataObject(org.opendaylight.yangtools.yang.binding.DataObject) Optional(com.google.common.base.Optional) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)

Aggregations

DataObject (org.opendaylight.yangtools.yang.binding.DataObject)44 InstanceIdentifier (org.opendaylight.yangtools.yang.binding.InstanceIdentifier)24 Test (org.junit.Test)14 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)11 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)10 Optional (com.google.common.base.Optional)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 LogicalDatastoreType (org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType)7 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ReadOnlyTransaction (org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)6 AbstractDataChangeListenerTest (org.opendaylight.controller.md.sal.binding.test.AbstractDataChangeListenerTest)6 Node (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node)6 Collection (java.util.Collection)5 Nonnull (javax.annotation.Nonnull)5 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)5 TopLevelListKey (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey)5 DataObjectModification (org.opendaylight.controller.md.sal.binding.api.DataObjectModification)4