Update MRI projects for Aluminium
[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.Collection;
13 import java.util.Map;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import java.util.function.Function;
16 import java.util.stream.Collectors;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
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.library.rev160621.module.list.module.SubmoduleKey;
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.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
35 import org.opendaylight.yangtools.yang.model.api.Module;
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 public class SchemaServiceToMdsalWriter implements EffectiveModelContextListener, AutoCloseable {
44     private static final Logger LOG = LoggerFactory.getLogger(SchemaServiceToMdsalWriter.class);
45     private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
46             InstanceIdentifier.create(ModulesState.class);
47
48     private final DOMSchemaService schemaService;
49     private final AtomicInteger moduleSetId;
50     private final DataBroker dataBroker;
51
52     public SchemaServiceToMdsalWriter(final DOMSchemaService schemaService,
53                                       final DataBroker dataBroker) {
54         this.schemaService = schemaService;
55         this.dataBroker = dataBroker;
56         this.moduleSetId = new AtomicInteger(0);
57     }
58
59     @Override
60     public void close() {
61         // TODO Delete modules-state from operational data store
62     }
63
64     /**
65      * Invoked by blueprint.
66      */
67     public void start() {
68         schemaService.registerSchemaContextListener(this);
69     }
70
71
72     @Override
73     public void onModelContextUpdated(final EffectiveModelContext context) {
74         final ModulesState newModuleState = createModuleStateFromModules(context.getModules());
75         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
76         tx.put(LogicalDatastoreType.OPERATIONAL,
77                 MODULES_STATE_INSTANCE_IDENTIFIER, newModuleState);
78
79         LOG.debug("Trying to write new module-state: {}", newModuleState);
80         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
81             @Override
82             public void onSuccess(final CommitInfo result) {
83                 LOG.debug("Modules state updated successfully");
84             }
85
86             @Override
87             public void onFailure(final Throwable throwable) {
88                 LOG.warn("Failed to update modules state", throwable);
89             }
90         }, MoreExecutors.directExecutor());
91     }
92
93     private ModulesState createModuleStateFromModules(final Collection<? extends Module> modules) {
94         return new ModulesStateBuilder()
95                 .setModule(modules.stream()
96                     .map(this::createModuleEntryFromModule)
97                     .collect(Collectors.toMap(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library
98                         .rev160621.module.list.Module::key, Function.identity())))
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 Map<SubmoduleKey, Submodule> createSubmodulesForModule(final Module module) {
117         return module.getSubmodules().stream()
118                 .map(subModule -> new SubmoduleBuilder()
119                     .setName(new YangIdentifier(subModule.getName()))
120                     .setRevision(RevisionUtils.fromYangCommon(subModule.getQNameModule().getRevision()))
121                     .build())
122                 .collect(Collectors.toMap(Submodule::key, Function.identity()));
123     }
124 }