Rework mdsal-netconf-yang-library
[netconf.git] / netconf / mdsal-netconf-yang-library / src / main / java / org / opendaylight / netconf / mdsal / yang / library / SchemaServiceToMdsalWriter.java
diff --git a/netconf/mdsal-netconf-yang-library/src/main/java/org/opendaylight/netconf/mdsal/yang/library/SchemaServiceToMdsalWriter.java b/netconf/mdsal-netconf-yang-library/src/main/java/org/opendaylight/netconf/mdsal/yang/library/SchemaServiceToMdsalWriter.java
deleted file mode 100644 (file)
index 0ea4db4..0000000
+++ /dev/null
@@ -1,141 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.mdsal.yang.library;
-
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.MoreExecutors;
-import java.util.Collection;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-import org.opendaylight.mdsal.binding.api.DataBroker;
-import org.opendaylight.mdsal.binding.api.WriteTransaction;
-import org.opendaylight.mdsal.common.api.CommitInfo;
-import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafsRevisionBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module.ConformanceType;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.Submodule;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.SubmoduleBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.module.SubmoduleKey;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Listens for updates on global schema context, transforms context to ietf-yang-library:modules-state and
- * writes this state to operational data store.
- *
- * @deprecated ietf-yang-library:modules-state was deprecated in the new RFC8525.
- *             Use {@link org.opendaylight.netconf.mdsal.yang.library.rfc8525.SchemaServiceToMdsalWriter}.
- */
-@Deprecated(forRemoval = true)
-public class SchemaServiceToMdsalWriter implements EffectiveModelContextListener, AutoCloseable {
-    private static final Logger LOG = LoggerFactory.getLogger(SchemaServiceToMdsalWriter.class);
-    private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
-            InstanceIdentifier.create(ModulesState.class);
-
-    private final DOMSchemaService schemaService;
-    private final AtomicInteger moduleSetId;
-    private final DataBroker dataBroker;
-
-    public SchemaServiceToMdsalWriter(final DOMSchemaService schemaService,
-                                      final DataBroker dataBroker) {
-        this.schemaService = schemaService;
-        this.dataBroker = dataBroker;
-        this.moduleSetId = new AtomicInteger(0);
-    }
-
-    @Override
-    public void close() {
-        final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
-        tx.delete(LogicalDatastoreType.OPERATIONAL, MODULES_STATE_INSTANCE_IDENTIFIER);
-
-        tx.commit().addCallback(new FutureCallback<CommitInfo>() {
-            @Override
-            public void onSuccess(final CommitInfo info) {
-                LOG.debug("Module state cleared successfully");
-            }
-
-            @Override
-            public void onFailure(final Throwable throwable) {
-                LOG.warn("Unable to clear module state", throwable);
-            }
-        }, MoreExecutors.directExecutor());
-    }
-
-    /**
-     * Invoked by blueprint.
-     */
-    public void start() {
-        schemaService.registerSchemaContextListener(this);
-    }
-
-
-    @Override
-    public void onModelContextUpdated(final EffectiveModelContext context) {
-        final ModulesState newModuleState = createModuleStateFromModules(context.getModules());
-        final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
-        tx.put(LogicalDatastoreType.OPERATIONAL,
-                MODULES_STATE_INSTANCE_IDENTIFIER, newModuleState);
-
-        LOG.debug("Trying to write new module-state: {}", newModuleState);
-        tx.commit().addCallback(new FutureCallback<CommitInfo>() {
-            @Override
-            public void onSuccess(final CommitInfo result) {
-                LOG.debug("Modules state updated successfully");
-            }
-
-            @Override
-            public void onFailure(final Throwable throwable) {
-                LOG.warn("Failed to update modules state", throwable);
-            }
-        }, MoreExecutors.directExecutor());
-    }
-
-    private ModulesState createModuleStateFromModules(final Collection<? extends Module> modules) {
-        return new ModulesStateBuilder()
-                .setModule(modules.stream()
-                    .map(this::createModuleEntryFromModule)
-                    .collect(Collectors.toMap(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library
-                        .rev190104.module.list.Module::key, Function.identity())))
-                .setModuleSetId(String.valueOf(moduleSetId.getAndIncrement()))
-                .build();
-    }
-
-    private org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module
-        createModuleEntryFromModule(final Module module) {
-        return new ModuleBuilder()
-                .setName(new YangIdentifier(module.getName()))
-                .setRevision(CommonLeafsRevisionBuilder.fromYangCommon(module.getQNameModule().getRevision()))
-                .setNamespace(new Uri(module.getNamespace().toString()))
-                // FIXME: Conformance type is always set to Implement value, but it should it really be like this?
-                .setConformanceType(ConformanceType.Implement)
-                .setSubmodule(createSubmodulesForModule(module))
-                // FIXME: Add also deviations and features lists to module entries
-                .build();
-    }
-
-    private static Map<SubmoduleKey, Submodule> createSubmodulesForModule(final Module module) {
-        return module.getSubmodules().stream()
-                .map(subModule -> new SubmoduleBuilder()
-                    .setName(new YangIdentifier(subModule.getName()))
-                    .setRevision(CommonLeafsRevisionBuilder.fromYangCommon(subModule.getQNameModule().getRevision()))
-                    .build())
-                .collect(Collectors.toMap(Submodule::key, Function.identity()));
-    }
-}