Eliminate use of SchemaService
[netconf.git] / netconf / mdsal-netconf-yang-library / src / main / java / org / opendaylight / netconf / mdsal / yang / library / SchemaServiceToMdsalWriter.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.mdsal.yang.library;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.List;
13 import java.util.Set;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import java.util.stream.Collectors;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesStateBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.RevisionUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module.ConformanceType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.ModuleBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.Submodule;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.module.SubmoduleBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Listens for updates on global schema context, transforms context to ietf-yang-library:modules-state and
39  * writes this state to operational data store.
40  */
41 // TODO Implement also yang-library-change notfication
42 public class SchemaServiceToMdsalWriter implements SchemaContextListener, AutoCloseable {
43
44     private static final Logger LOG = LoggerFactory.getLogger(SchemaServiceToMdsalWriter.class);
45
46     private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
47             InstanceIdentifier.create(ModulesState.class);
48
49     private final DOMSchemaService schemaService;
50     private final AtomicInteger moduleSetId;
51     private final DataBroker dataBroker;
52
53     public SchemaServiceToMdsalWriter(final DOMSchemaService schemaService,
54                                       final DataBroker dataBroker) {
55         this.schemaService = schemaService;
56         this.dataBroker = dataBroker;
57         this.moduleSetId = new AtomicInteger(0);
58     }
59
60     @Override
61     public void close() throws Exception {
62         // TODO Delete modules-state from operational data store
63     }
64
65     /**
66      * Invoked by blueprint.
67      */
68     public void start() {
69         schemaService.registerSchemaContextListener(this);
70     }
71
72
73     @Override
74     public void onGlobalContextUpdated(final SchemaContext context) {
75         final ModulesState newModuleState = createModuleStateFromModules(context.getModules());
76         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
77         tx.put(LogicalDatastoreType.OPERATIONAL,
78                 MODULES_STATE_INSTANCE_IDENTIFIER, newModuleState);
79
80         LOG.debug("Trying to write new module-state: {}", newModuleState);
81         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
82             @Override
83             public void onSuccess(final CommitInfo result) {
84                 LOG.debug("Modules state updated successfully");
85             }
86
87             @Override
88             public void onFailure(final Throwable throwable) {
89                 LOG.warn("Failed to update modules state", throwable);
90             }
91         }, MoreExecutors.directExecutor());
92     }
93
94     private ModulesState createModuleStateFromModules(final Set<Module> modules) {
95         return new ModulesStateBuilder()
96                 .setModule(modules.stream().map(this::createModuleEntryFromModule).collect(Collectors.toList()))
97                 .setModuleSetId(String.valueOf(moduleSetId.getAndIncrement()))
98                 .build();
99     }
100
101     private org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module
102         createModuleEntryFromModule(final Module module) {
103         return new ModuleBuilder()
104                 .setName(new YangIdentifier(module.getName()))
105                 .setRevision(RevisionUtils.fromYangCommon(module.getQNameModule().getRevision()))
106                 .setNamespace(new Uri(module.getNamespace().toString()))
107                 // FIXME: Conformance type is always set to Implement value, but it should it really be like this?
108                 .setConformanceType(ConformanceType.Implement)
109                 .setSubmodule(createSubmodulesForModule(module))
110                 // FIXME: Add also deviations and features lists to module entries
111                 .build();
112     }
113
114     private static List<Submodule> createSubmodulesForModule(final Module module) {
115         return module.getSubmodules().stream().map(subModule -> new SubmoduleBuilder()
116             .setName(new YangIdentifier(subModule.getName()))
117             .setRevision(RevisionUtils.fromYangCommon(subModule.getQNameModule().getRevision()))
118             .build()).collect(Collectors.toList());
119     }
120 }