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