Remove yang-test
[controller.git] / opendaylight / config / config-persister-impl / src / main / java / org / opendaylight / controller / config / persist / impl / PersisterAggregator.java
1 /*
2  * Copyright (c) 2013 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.persist.impl;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.ListIterator;
17 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
18 import org.opendaylight.controller.config.persist.api.Persister;
19 import org.opendaylight.controller.config.persist.api.PropertiesProvider;
20 import org.opendaylight.controller.config.persist.api.StorageAdapter;
21 import org.opendaylight.controller.config.persist.impl.osgi.ConfigPersisterActivator;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * {@link Persister} implementation that delegates persisting functionality to
27  * underlying {@link Persister} storages. Each storage has unique id, class, readonly value.
28  *
29  * Storage adapters are low level persisters that do the heavy lifting for this
30  * class. Instances of storage adapters can be injected directly via constructor
31  * or instantiated from a full name of its class provided in a properties file.
32  *
33  * Example configuration:<pre>
34  netconf.config.persister.active=2,3
35  # read startup configuration
36  netconf.config.persister.1.storageAdapterClass=org.opendaylight.controller.config.persist.storage.directory.xml.XmlDirectoryStorageAdapter
37  netconf.config.persister.1.properties.fileStorage=configuration/initial/
38
39  netconf.config.persister.2.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter
40  netconf.config.persister.2.readonly=true
41  netconf.config.persister.2.properties.fileStorage=configuration/current/controller.config.1.xml
42
43  netconf.config.persister.3.storageAdapterClass=org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter
44  netconf.config.persister.3.properties.fileStorage=configuration/current/controller.config.2.xml
45  netconf.config.persister.3.properties.numberOfBackups=3
46
47  </pre>
48  * During server startup {@link ConfigPersisterNotificationHandler} requests last snapshot from underlying storages.
49  * Each storage can respond by giving snapshot or absent response.
50  * The {@link #loadLastConfigs()} will search for first non-absent response from storages ordered backwards as user
51  * specified (first '3', then '2').
52  *
53  * When a commit notification is received, '2' will be omitted because readonly flag is set to true, so
54  * only '3' will have a chance to persist new configuration. If readonly was false or not specified, both storage adapters
55  * would be called in order specified by 'netconf.config.persister' property.
56  *
57  */
58 public final class PersisterAggregator implements Persister {
59     private static final Logger LOG = LoggerFactory.getLogger(PersisterAggregator.class);
60
61     public static class PersisterWithConfiguration {
62
63         private final Persister storage;
64         private final boolean readOnly;
65
66         public PersisterWithConfiguration(final Persister storage, final boolean readOnly) {
67             this.storage = storage;
68             this.readOnly = readOnly;
69         }
70
71         @VisibleForTesting
72         public Persister getStorage() {
73             return storage;
74         }
75
76         @VisibleForTesting
77         public boolean isReadOnly() {
78             return readOnly;
79         }
80
81         @Override
82         public String toString() {
83             return "PersisterWithConfiguration{" +
84                     "storage=" + storage +
85                     ", readOnly=" + readOnly +
86                     '}';
87         }
88     }
89
90     /**
91      * Persisters ordered by 'netconf.config.persister' property.
92      */
93     private final List<PersisterWithConfiguration> persisterWithConfigurations;
94
95     public PersisterAggregator(final List<PersisterWithConfiguration> persisterWithConfigurations) {
96         this.persisterWithConfigurations = persisterWithConfigurations;
97     }
98
99     private static PersisterWithConfiguration loadConfiguration(final String index, final PropertiesProvider propertiesProvider) {
100
101         String classKey = index + "." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX;
102         String storageAdapterClass = propertiesProvider.getProperty(classKey);
103         StorageAdapter storageAdapter;
104         if (storageAdapterClass == null || storageAdapterClass.isEmpty()) {
105             throw new IllegalStateException("No persister is defined in " +
106                     propertiesProvider.getFullKeyForReporting(classKey)
107                     + " property. Persister is not operational");
108         }
109
110         try {
111             Class<?> clazz = Class.forName(storageAdapterClass);
112             boolean implementsCorrectIfc = StorageAdapter.class.isAssignableFrom(clazz);
113             if (!implementsCorrectIfc) {
114                 throw new IllegalArgumentException("Storage adapter " + clazz + " does not implement " + StorageAdapter.class);
115             }
116             storageAdapter = StorageAdapter.class.cast(clazz.newInstance());
117
118             String readOnlyProperty = propertiesProvider.getProperty(index + "." + "readonly");
119             boolean readOnly = Boolean.parseBoolean(readOnlyProperty);
120
121             PropertiesProviderAdapterImpl innerProvider = new PropertiesProviderAdapterImpl(propertiesProvider, index);
122             Persister storage = storageAdapter.instantiate(innerProvider);
123             return new PersisterWithConfiguration(storage, readOnly);
124         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
125             throw new IllegalArgumentException("Unable to instantiate storage adapter from " + storageAdapterClass, e);
126         }
127     }
128
129     public static PersisterAggregator createFromProperties(final PropertiesProvider propertiesProvider) {
130         List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
131         String prefixes = propertiesProvider.getProperty("active");
132         if (prefixes!=null && !prefixes.isEmpty()) {
133             String [] keys = prefixes.split(",");
134             for (String index: keys) {
135                 persisterWithConfigurations.add(PersisterAggregator.loadConfiguration(index, propertiesProvider));
136             }
137         }
138         LOG.debug("Initialized persister with following adapters {}", persisterWithConfigurations);
139         return new PersisterAggregator(persisterWithConfigurations);
140     }
141
142     @Override
143     public void persistConfig(final ConfigSnapshotHolder holder) throws IOException {
144         for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
145             if (!persisterWithConfiguration.readOnly){
146                 LOG.debug("Calling {}.persistConfig", persisterWithConfiguration.getStorage());
147                 persisterWithConfiguration.getStorage().persistConfig(holder);
148             }
149         }
150     }
151
152     /**
153      * @return last non-empty result from input persisters
154      */
155     @Override
156     public List<ConfigSnapshotHolder> loadLastConfigs()  {
157         // iterate in reverse order
158         ListIterator<PersisterWithConfiguration> li = persisterWithConfigurations.listIterator(persisterWithConfigurations.size());
159         while(li.hasPrevious()) {
160             PersisterWithConfiguration persisterWithConfiguration = li.previous();
161             List<ConfigSnapshotHolder> configs = null;
162             try {
163                 configs = persisterWithConfiguration.storage.loadLastConfigs();
164             } catch (final IOException e) {
165                 throw new RuntimeException("Error while calling loadLastConfig on " +  persisterWithConfiguration, e);
166             }
167             if (!configs.isEmpty()) {
168                 LOG.debug("Found non empty configs using {}:{}", persisterWithConfiguration, configs);
169                 return configs;
170             }
171         }
172         // no storage had an answer
173         LOG.debug("No non-empty list of configuration snapshots found");
174         return Collections.emptyList();
175     }
176
177     @VisibleForTesting
178     List<PersisterWithConfiguration> getPersisterWithConfigurations() {
179         return persisterWithConfigurations;
180     }
181
182     @Override
183     public void close() {
184         RuntimeException lastException = null;
185         for (PersisterWithConfiguration persisterWithConfiguration: persisterWithConfigurations){
186             try{
187                 persisterWithConfiguration.storage.close();
188             }catch(final RuntimeException e) {
189                 LOG.error("Error while closing {}", persisterWithConfiguration.storage, e);
190                 if (lastException == null){
191                     lastException = e;
192                 } else {
193                     lastException.addSuppressed(e);
194                 }
195             }
196         }
197         if (lastException != null){
198             throw lastException;
199         }
200     }
201
202     @Override
203     public String toString() {
204         return "PersisterAggregator{" +
205                 "persisterWithConfigurations=" + persisterWithConfigurations +
206                 '}';
207     }
208 }

©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.