2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.persist.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import org.opendaylight.controller.config.persist.api.Persister;
14 import org.opendaylight.controller.config.persist.api.storage.StorageAdapter;
15 import org.opendaylight.controller.config.persist.api.storage.StorageAdapter.PropertiesProvider;
16 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
18 import java.io.IOException;
21 * {@link Persister} implementation that delegates persisting functionality to
22 * underlying {@link Persister} called Storage Adapter.
24 * Storage adapters are low level persisters that do the heavy lifting for this
25 * class. Instances of storage adapters can be injected directly via constructor
26 * or instantiated from a full name of its class provided in a properties file.
28 * Name of storage adapter class should be located under
29 * {@link #STORAGE_ADAPTER_CLASS_PROP} key.
31 public final class PersisterImpl implements Persister {
34 private final StorageAdapter storage;
36 public PersisterImpl(StorageAdapter storage) {
37 this.storage = storage;
40 public static PersisterImpl createFromProperties(PropertiesProvider propertiesProvider) {
41 String storageAdapterClass = propertiesProvider.getProperty(ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
42 StorageAdapter storage;
43 if (storageAdapterClass == null || storageAdapterClass.equals("")) {
44 throw new IllegalStateException("No persister is defined in " +
45 propertiesProvider.getFullKeyForReporting(ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX)
46 + " property. For noop persister use " + NoOpStorageAdapter.class.getCanonicalName()
47 + " . Persister is not operational");
51 Class<?> clazz = Class.forName(storageAdapterClass);
52 boolean implementsCorrectIfc = StorageAdapter.class.isAssignableFrom(clazz);
53 if (implementsCorrectIfc == false) {
54 throw new IllegalArgumentException("Storage adapter " + clazz + " does not implement " + StorageAdapter.class);
56 storage = StorageAdapter.class.cast(clazz.newInstance());
58 storage.setProperties(propertiesProvider);
60 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
61 throw new IllegalArgumentException("Unable to instantiate storage adapter from " + storageAdapterClass, e);
64 return new PersisterImpl(storage);
68 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
69 storage.persistConfig(holder);
73 public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
74 return storage.loadLastConfig();
78 StorageAdapter getStorage() {
83 public void close() throws IOException {
88 public String toString() {
89 return "PersisterImpl [storage=" + storage + "]";