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