Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / ConfigSubsystemFacade.java
1 /*
2  * Copyright (c) 2015, 2017 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.controller.config.facade.xml;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Multimap;
13 import java.io.Closeable;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import javax.management.InstanceNotFoundException;
19 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.ConflictingVersionException;
21 import org.opendaylight.controller.config.api.ValidationException;
22 import org.opendaylight.controller.config.api.jmx.CommitStatus;
23 import org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping;
24 import org.opendaylight.controller.config.facade.xml.mapping.config.Config;
25 import org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfig;
26 import org.opendaylight.controller.config.facade.xml.mapping.config.InstanceConfigElementResolved;
27 import org.opendaylight.controller.config.facade.xml.mapping.config.ModuleConfig;
28 import org.opendaylight.controller.config.facade.xml.mapping.config.ModuleElementDefinition;
29 import org.opendaylight.controller.config.facade.xml.mapping.config.ModuleElementResolved;
30 import org.opendaylight.controller.config.facade.xml.mapping.config.ServiceRegistryWrapper;
31 import org.opendaylight.controller.config.facade.xml.mapping.config.Services;
32 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreContext;
33 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
34 import org.opendaylight.controller.config.facade.xml.runtime.InstanceRuntime;
35 import org.opendaylight.controller.config.facade.xml.runtime.ModuleRuntime;
36 import org.opendaylight.controller.config.facade.xml.runtime.Runtime;
37 import org.opendaylight.controller.config.facade.xml.strategy.EditConfigStrategy;
38 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
39 import org.opendaylight.controller.config.facade.xml.transactions.TransactionProvider;
40 import org.opendaylight.controller.config.util.BeanReader;
41 import org.opendaylight.controller.config.util.ConfigRegistryClient;
42 import org.opendaylight.controller.config.util.ConfigTransactionClient;
43 import org.opendaylight.controller.config.util.xml.DocumentedException;
44 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
45 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
46 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
47 import org.opendaylight.controller.config.util.xml.XmlElement;
48 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
49 import org.opendaylight.controller.config.util.xml.XmlUtil;
50 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
51 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
52 import org.opendaylight.yangtools.yang.common.Revision;
53 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.Module;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.w3c.dom.Document;
58 import org.w3c.dom.Element;
59
60 /**
61  * Config subsystem facade for xml format.
62  *
63  * <p>
64  * TODO extract generic interface for config subsystem facades
65  */
66 public class ConfigSubsystemFacade implements Closeable {
67
68     private static final Logger LOG = LoggerFactory.getLogger(ConfigSubsystemFacade.class);
69     private final YangStoreService yangStoreService;
70     private final TransactionProvider transactionProvider;
71     private final ConfigRegistryClient configRegistryClient;
72     private final ConfigRegistryClient configRegistryClientNoNotifications;
73     private final RpcFacade rpcFacade;
74
75     public ConfigSubsystemFacade(final ConfigRegistryClient configRegistryClient,
76                                  final ConfigRegistryClient configRegistryClientNoNotifications,
77                                  final YangStoreService yangStoreService,
78                                  final String id) {
79         this.configRegistryClient = configRegistryClient;
80         this.configRegistryClientNoNotifications = configRegistryClientNoNotifications;
81         this.yangStoreService = yangStoreService;
82         this.transactionProvider = new TransactionProvider(configRegistryClient, id);
83         rpcFacade = new RpcFacade(yangStoreService, configRegistryClient);
84     }
85
86     public ConfigSubsystemFacade(final ConfigRegistryClient configRegistryClient,
87                                  final ConfigRegistryClient configRegistryClientNoNotifications,
88                                  final YangStoreService yangStoreService,
89                                  final TransactionProvider txProvider) {
90         this.configRegistryClient = configRegistryClient;
91         this.configRegistryClientNoNotifications = configRegistryClientNoNotifications;
92         this.yangStoreService = yangStoreService;
93         this.transactionProvider = txProvider;
94         rpcFacade = new RpcFacade(yangStoreService, configRegistryClient);
95     }
96
97     public Element getConfiguration(final Document document, final Datastore source,
98                                     final Optional<String> maybeNamespace) {
99
100         final ConfigTransactionClient registryClient;
101         // Read current state from a transaction, if running is source, then start new
102         // transaction just for reading
103         // in case of candidate, get current transaction representing candidate
104         if (source == Datastore.running) {
105             final ObjectName readTx = transactionProvider.getOrCreateReadTransaction();
106             registryClient = configRegistryClient.getConfigTransactionClient(readTx);
107         } else {
108             registryClient = configRegistryClient
109                     .getConfigTransactionClient(transactionProvider.getOrCreateTransaction());
110         }
111
112         try {
113             Element dataElement = XmlUtil.createElement(document, XmlMappingConstants.DATA_KEY,
114                     Optional.<String>absent());
115             final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
116                     .queryInstances(configRegistryClient);
117
118             final Config configMapping = new Config(
119                     transformMbeToModuleConfigs(yangStoreService.getModuleMXBeanEntryMap()),
120                     yangStoreService.getEnumResolver());
121
122             ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(registryClient);
123             dataElement = configMapping.toXml(instances, maybeNamespace, document, dataElement, serviceTracker);
124
125             return dataElement;
126         } finally {
127             if (source == Datastore.running) {
128                 transactionProvider.closeReadTransaction();
129             }
130         }
131     }
132
133     public void executeConfigExecution(final ConfigExecution configExecution)
134             throws DocumentedException, ValidationException {
135         if (configExecution.shouldTest()) {
136             executeTests(configExecution);
137         }
138
139         if (configExecution.shouldSet()) {
140             executeSet(configExecution);
141         }
142     }
143
144     public CommitStatus commitTransaction()
145             throws DocumentedException, ValidationException, ConflictingVersionException {
146         final CommitStatus status = this.transactionProvider.commitTransaction();
147         LOG.trace("Transaction committed successfully: {}", status);
148         return status;
149     }
150
151     public CommitStatus commitSilentTransaction()
152             throws DocumentedException, ValidationException, ConflictingVersionException {
153         final CommitStatus status = this.transactionProvider.commitTransaction(configRegistryClientNoNotifications);
154         LOG.trace("Transaction committed successfully: {}", status);
155         return status;
156     }
157
158     private void executeSet(final ConfigExecution configExecution) throws DocumentedException {
159         set(configExecution);
160         LOG.debug("Set phase for {} operation successful, element: ", configExecution.getDefaultStrategy(),
161                 configExecution.getConfigElement());
162     }
163
164     private void executeTests(final ConfigExecution configExecution) throws DocumentedException, ValidationException {
165         test(configExecution, configExecution.getDefaultStrategy());
166         LOG.debug("Test phase for {} operation successful, element: ", configExecution.getDefaultStrategy(),
167                 configExecution.getConfigElement());
168     }
169
170     private void test(final ConfigExecution execution, final EditStrategyType editStrategyType)
171             throws ValidationException, DocumentedException {
172         ObjectName taON = transactionProvider.getTestTransaction();
173         try {
174             // default strategy = replace wipes config
175             if (editStrategyType == EditStrategyType.replace) {
176                 transactionProvider.wipeTestTransaction(taON);
177             }
178
179             ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(taON);
180
181             handleMisssingInstancesOnTransaction(ta, execution);
182             setServicesOnTransaction(ta, execution);
183             setOnTransaction(ta, execution);
184             transactionProvider.validateTestTransaction(taON);
185         } finally {
186             transactionProvider.abortTestTransaction(taON);
187         }
188     }
189
190     private void set(final ConfigExecution configExecution) throws DocumentedException {
191         ObjectName taON = transactionProvider.getOrCreateTransaction();
192
193         // default strategy = replace wipes config
194         if (configExecution.getDefaultStrategy() == EditStrategyType.replace) {
195             transactionProvider.wipeTransaction();
196         }
197
198         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(taON);
199
200         handleMisssingInstancesOnTransaction(ta, configExecution);
201         setServicesOnTransaction(ta, configExecution);
202         setOnTransaction(ta, configExecution);
203     }
204
205     private static void setServicesOnTransaction(final ConfigTransactionClient ta, final ConfigExecution execution)
206             throws DocumentedException {
207         Services services = execution.getServices();
208
209         Map<String, Map<String, Map<String, Services.ServiceInstance>>> namespaceToServiceNameToRefNameToInstance =
210                 services.getNamespaceToServiceNameToRefNameToInstance();
211
212         for (Map.Entry<String, Map<String, Map<String, Services.ServiceInstance>>>
213                 namespaceToServiceToRefEntry : namespaceToServiceNameToRefNameToInstance
214                 .entrySet()) {
215             for (Map.Entry<String, Map<String, Services.ServiceInstance>> serviceToRefEntry
216                     : namespaceToServiceToRefEntry.getValue().entrySet()) {
217                 String qnameOfService = getQname(ta, namespaceToServiceToRefEntry.getKey(), serviceToRefEntry.getKey());
218                 Map<String, Services.ServiceInstance> refNameToInstance = serviceToRefEntry.getValue();
219
220                 for (Map.Entry<String, Services.ServiceInstance> refNameToServiceEntry : refNameToInstance.entrySet()) {
221                     ObjectName on = refNameToServiceEntry.getValue().getObjectName(ta.getTransactionName());
222                     try {
223                         if (Services.ServiceInstance.EMPTY_SERVICE_INSTANCE == refNameToServiceEntry.getValue()) {
224                             ta.removeServiceReference(qnameOfService, refNameToServiceEntry.getKey());
225                             LOG.debug("Removing service {} with name {}", qnameOfService,
226                                     refNameToServiceEntry.getKey());
227                         } else {
228                             ObjectName saved = ta.saveServiceReference(qnameOfService, refNameToServiceEntry.getKey(),
229                                     on);
230                             LOG.debug("Saving service {} with on {} under name {} with service on {}", qnameOfService,
231                                     on, refNameToServiceEntry.getKey(), saved);
232                         }
233                     } catch (final InstanceNotFoundException e) {
234                         throw new DocumentedException(
235                                 String.format("Unable to edit ref name " + refNameToServiceEntry.getKey()
236                                         + " for instance " + on, e),
237                                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
238                     }
239                 }
240             }
241         }
242     }
243
244     private static String getQname(final ConfigTransactionClient ta, final String namespace,
245             final String serviceName) {
246         return ta.getServiceInterfaceName(namespace, serviceName);
247     }
248
249     private static void setOnTransaction(final ConfigTransactionClient ta, final ConfigExecution execution)
250             throws DocumentedException {
251
252         for (Multimap<String, ModuleElementResolved> modulesToResolved : execution.getResolvedXmlElements(ta)
253                 .values()) {
254
255             for (Map.Entry<String, ModuleElementResolved> moduleToResolved : modulesToResolved.entries()) {
256                 String moduleName = moduleToResolved.getKey();
257
258                 ModuleElementResolved moduleElementResolved = moduleToResolved.getValue();
259                 String instanceName = moduleElementResolved.getInstanceName();
260
261                 InstanceConfigElementResolved ice = moduleElementResolved.getInstanceConfigElementResolved();
262                 EditConfigStrategy strategy = ice.getEditStrategy();
263                 strategy.executeConfiguration(moduleName, instanceName, ice.getConfiguration(), ta,
264                         execution.getServiceRegistryWrapper(ta));
265             }
266         }
267     }
268
269     private static void handleMisssingInstancesOnTransaction(final ConfigTransactionClient ta,
270             final ConfigExecution execution) throws DocumentedException {
271
272         for (Multimap<String, ModuleElementDefinition> modulesToResolved : execution.getModulesDefinition(ta)
273                 .values()) {
274             for (Map.Entry<String, ModuleElementDefinition> moduleToResolved : modulesToResolved.entries()) {
275                 String moduleName = moduleToResolved.getKey();
276
277                 ModuleElementDefinition moduleElementDefinition = moduleToResolved.getValue();
278
279                 EditConfigStrategy strategy = moduleElementDefinition.getEditStrategy();
280                 strategy.executeConfiguration(moduleName, moduleElementDefinition.getInstanceName(), null, ta,
281                         execution.getServiceRegistryWrapper(ta));
282             }
283         }
284     }
285
286     public Config getConfigMapping() {
287         final YangStoreContext snapshot = yangStoreService.getCurrentSnapshot();
288         Map<String, Map<String, ModuleConfig>> factories = transformMbeToModuleConfigs(
289                 snapshot.getModuleMXBeanEntryMap());
290         Map<String, Map<Optional<Revision>, IdentityMapping>> identitiesMap =
291             transformIdentities(snapshot.getModules());
292         return new Config(factories, identitiesMap, snapshot.getEnumResolver());
293     }
294
295     private static Map<String, Map<Optional<Revision>, IdentityMapping>> transformIdentities(
296             final Set<Module> modules) {
297         Map<String, Map<Optional<Revision>, IdentityMapping>> mappedIds = new HashMap<>();
298         for (Module module : modules) {
299             String namespace = module.getNamespace().toString();
300             Map<Optional<Revision>, IdentityMapping> revisionsByNamespace = mappedIds.computeIfAbsent(namespace,
301                 k -> new HashMap<>());
302
303             Optional<Revision> revision = Optional.fromJavaUtil(module.getRevision());
304
305             IdentityMapping identityMapping = revisionsByNamespace.computeIfAbsent(revision,
306                 k -> new IdentityMapping());
307
308             for (IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
309                 identityMapping.addIdSchemaNode(identitySchemaNode);
310             }
311
312         }
313
314         return mappedIds;
315     }
316
317     public Map<String/* Namespace from yang file */,
318                     Map<String /* Name of module entry from yang file */,
319                     ModuleConfig>> transformMbeToModuleConfigs(
320             final Map<String/* Namespace from yang file */,
321                     Map<String /* Name of module entry from yang file */,
322                             ModuleMXBeanEntry>> mbeanentries) {
323         return transformMbeToModuleConfigs(configRegistryClient, mbeanentries);
324     }
325
326     public Map<String/* Namespace from yang file */,
327             Map<String /* Name of module entry from yang file */,
328                     ModuleConfig>> transformMbeToModuleConfigs(
329             final BeanReader reader,
330             final Map<String/* Namespace from yang file */,
331                     Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> mbeanentries) {
332         Map<String, Map<String, ModuleConfig>> namespaceToModuleNameToModuleConfig = new HashMap<>();
333
334         for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleToMbe : mbeanentries.entrySet()) {
335             for (Map.Entry<String, ModuleMXBeanEntry> moduleNameToMbe : namespaceToModuleToMbe.getValue().entrySet()) {
336                 String moduleName = moduleNameToMbe.getKey();
337                 ModuleMXBeanEntry moduleMXBeanEntry = moduleNameToMbe.getValue();
338
339                 ModuleConfig moduleConfig = new ModuleConfig(moduleName, new InstanceConfig(reader,
340                         moduleMXBeanEntry.getAttributes(), moduleMXBeanEntry.getNullableDummyContainerName()));
341
342                 Map<String, ModuleConfig> moduleNameToModuleConfig = namespaceToModuleNameToModuleConfig
343                         .computeIfAbsent(namespaceToModuleToMbe.getKey(), k -> new HashMap<>());
344
345                 moduleNameToModuleConfig.put(moduleName, moduleConfig);
346             }
347         }
348
349         return namespaceToModuleNameToModuleConfig;
350     }
351
352     public ConfigExecution getConfigExecution(final Config configMapping, final Element xmlToBePersisted)
353             throws DocumentedException {
354         return new ConfigExecution(configMapping, XmlElement.fromDomElement(xmlToBePersisted), TestOption.testThenSet,
355                 EditStrategyType.getDefaultStrategy());
356     }
357
358     private Map<String, Map<String, ModuleRuntime>> createModuleRuntimes(
359             final ConfigRegistryClient client,
360             final Map<String, Map<String, ModuleMXBeanEntry>> mbeanentries) {
361         Map<String, Map<String, ModuleRuntime>> retVal = new HashMap<>();
362
363         for (Map.Entry<String, Map<String, ModuleMXBeanEntry>> namespaceToModuleEntry : mbeanentries.entrySet()) {
364
365             Map<String, ModuleRuntime> innerMap = new HashMap<>();
366             Map<String, ModuleMXBeanEntry> entriesFromNamespace = namespaceToModuleEntry.getValue();
367             for (Map.Entry<String, ModuleMXBeanEntry> moduleToMXEntry : entriesFromNamespace.entrySet()) {
368
369                 ModuleMXBeanEntry mbe = moduleToMXEntry.getValue();
370
371                 Map<RuntimeBeanEntry, InstanceConfig> cache = new HashMap<>();
372                 RuntimeBeanEntry root = null;
373                 for (RuntimeBeanEntry rbe : mbe.getRuntimeBeans()) {
374                     cache.put(rbe, new InstanceConfig(client, rbe.getYangPropertiesToTypesMap(),
375                             mbe.getNullableDummyContainerName()));
376                     if (rbe.isRoot()) {
377                         root = rbe;
378                     }
379                 }
380
381                 if (root == null) {
382                     continue;
383                 }
384
385                 InstanceRuntime rootInstanceRuntime = createInstanceRuntime(root, cache);
386                 ModuleRuntime moduleRuntime = new ModuleRuntime(rootInstanceRuntime);
387                 innerMap.put(moduleToMXEntry.getKey(), moduleRuntime);
388             }
389
390             retVal.put(namespaceToModuleEntry.getKey(), innerMap);
391         }
392         return retVal;
393     }
394
395     private InstanceRuntime createInstanceRuntime(final RuntimeBeanEntry root,
396                                                   final Map<RuntimeBeanEntry, InstanceConfig> cache) {
397         Map<String, InstanceRuntime> children = new HashMap<>();
398         for (RuntimeBeanEntry child : root.getChildren()) {
399             children.put(child.getJavaNamePrefix(), createInstanceRuntime(child, cache));
400         }
401
402         return new InstanceRuntime(cache.get(root), children, createJmxToYangMap(root.getChildren()));
403     }
404
405     private Map<String, String> createJmxToYangMap(final List<RuntimeBeanEntry> children) {
406         Map<String, String> jmxToYangNamesForChildRbe = new HashMap<>();
407         for (RuntimeBeanEntry rbe : children) {
408             jmxToYangNamesForChildRbe.put(rbe.getJavaNamePrefix(), rbe.getYangName());
409         }
410         return jmxToYangNamesForChildRbe;
411     }
412
413     public Element get(final Document document) throws DocumentedException {
414         final ObjectName testTransaction = transactionProvider.getOrCreateReadTransaction();
415         final ConfigTransactionClient txClient = configRegistryClient.getConfigTransactionClient(testTransaction);
416
417         try {
418             // Runtime beans are not parts of transactions and have to be queried against
419             // the central registry
420             final Set<ObjectName> runtimeBeans = configRegistryClient.lookupRuntimeBeans();
421
422             final Set<ObjectName> configBeans = Datastore
423                     .getInstanceQueryStrategy(Datastore.running, transactionProvider)
424                     .queryInstances(configRegistryClient);
425
426             final Map<String, Map<String, ModuleRuntime>> moduleRuntimes = createModuleRuntimes(configRegistryClient,
427                     yangStoreService.getModuleMXBeanEntryMap());
428
429             final YangStoreContext yangStoreSnapshot = yangStoreService.getCurrentSnapshot();
430             final Map<String, Map<String, ModuleConfig>> moduleConfigs = transformMbeToModuleConfigs(txClient,
431                     yangStoreSnapshot.getModuleMXBeanEntryMap());
432
433             final org.opendaylight.controller.config.facade.xml.runtime.Runtime runtime = new Runtime(moduleRuntimes,
434                     moduleConfigs);
435
436             return runtime.toXml(runtimeBeans, configBeans, document, yangStoreSnapshot.getEnumResolver());
437         } finally {
438             transactionProvider.closeReadTransaction();
439         }
440     }
441
442     public void abortConfiguration() {
443         if (transactionProvider.getTransaction().isPresent()) {
444             this.transactionProvider.abortTransaction();
445         }
446     }
447
448     public void validateConfiguration() throws ValidationException {
449         transactionProvider.validateTransaction();
450     }
451
452     @Override
453     public void close() {
454         transactionProvider.close();
455     }
456
457     public RpcFacade getRpcFacade() {
458         return rpcFacade;
459     }
460
461 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.