Refactor persister.
[controller.git] / opendaylight / netconf / config-persister-impl / src / main / java / org / opendaylight / controller / netconf / persist / impl / PersisterImpl.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.netconf.persist.impl;
10
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;
17
18 import java.io.IOException;
19
20 /**
21  * {@link Persister} implementation that delegates persisting functionality to
22  * underlying {@link Persister} called Storage Adapter.
23  *
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.
27  *
28  * Name of storage adapter class should be located under
29  * {@link #STORAGE_ADAPTER_CLASS_PROP} key.
30  */
31 public final class PersisterImpl implements Persister {
32
33
34     private final StorageAdapter storage;
35
36     public PersisterImpl(StorageAdapter storage) {
37         this.storage = storage;
38     }
39
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");
48         }
49
50         try {
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);
55             }
56             storage = StorageAdapter.class.cast(clazz.newInstance());
57
58             storage.setProperties(propertiesProvider);
59
60         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
61             throw new IllegalArgumentException("Unable to instantiate storage adapter from " + storageAdapterClass, e);
62         }
63
64         return new PersisterImpl(storage);
65     }
66
67     @Override
68     public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
69         storage.persistConfig(holder);
70     }
71
72     @Override
73     public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
74         return storage.loadLastConfig();
75     }
76
77     @VisibleForTesting
78     StorageAdapter getStorage() {
79         return storage;
80     }
81
82     @Override
83     public void close() throws IOException {
84         storage.close();
85     }
86
87     @Override
88     public String toString() {
89         return "PersisterImpl [storage=" + storage + "]";
90     }
91 }

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