03acfa88eb04e1737650af810c11b1afe809ad88
[controller.git] / opendaylight / configuration / implementation / src / main / java / org / opendaylight / controller / configuration / internal / ConfigurationService.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.configuration.internal;
11
12 import java.util.Collections;
13 import java.util.EnumSet;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentMap;
18
19 import org.opendaylight.controller.clustering.services.CacheConfigException;
20 import org.opendaylight.controller.clustering.services.CacheExistException;
21 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
22 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
23 import org.opendaylight.controller.clustering.services.IClusterServices;
24 import org.opendaylight.controller.configuration.ConfigurationEvent;
25 import org.opendaylight.controller.configuration.ConfigurationObject;
26 import org.opendaylight.controller.configuration.IConfigurationAware;
27 import org.opendaylight.controller.configuration.IConfigurationService;
28 import org.opendaylight.controller.sal.utils.GlobalConstants;
29 import org.opendaylight.controller.sal.utils.IObjectReader;
30 import org.opendaylight.controller.sal.utils.ObjectReader;
31 import org.opendaylight.controller.sal.utils.ObjectWriter;
32 import org.opendaylight.controller.sal.utils.Status;
33 import org.opendaylight.controller.sal.utils.StatusCode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * @file   ConfigurationImpl.java
39  *
40  * @brief  Backend functionality for all ConfigurationService related tasks.
41  *
42  */
43
44 public class ConfigurationService implements IConfigurationService, ICacheUpdateAware<ConfigurationEvent, String> {
45     private static final Logger logger = LoggerFactory
46             .getLogger(ConfigurationService.class);
47     public static final String SAVE_EVENT_CACHE = "config.event.save";
48     private static final Object ROOT = GlobalConstants.STARTUPHOME.toString();
49     private IClusterGlobalServices clusterServices;
50     private ConcurrentMap <ConfigurationEvent, String> configEvent;
51     private Set<IConfigurationAware> configurationAwareList = Collections
52             .synchronizedSet(new HashSet<IConfigurationAware>());
53     private ObjectReader objReader;
54     private ObjectWriter objWriter;
55
56
57     public int getConfigurationAwareListSize() {
58         return this.configurationAwareList.size();
59     }
60
61     public void addConfigurationAware(IConfigurationAware configurationAware) {
62         if (!this.configurationAwareList.contains(configurationAware)) {
63             this.configurationAwareList.add(configurationAware);
64         }
65     }
66
67     public void removeConfigurationAware(IConfigurationAware configurationAware) {
68         this.configurationAwareList.remove(configurationAware);
69     }
70
71     public void setClusterServices(IClusterGlobalServices i) {
72         this.clusterServices = i;
73         logger.debug("IClusterServices set");
74     }
75
76     public void unsetClusterServices(IClusterGlobalServices i) {
77         if (this.clusterServices == i) {
78             this.clusterServices = null;
79             logger.debug("IClusterServices Unset");
80         }
81     }
82
83     public void init() {
84         logger.info("ConfigurationService Manager init");
85     }
86
87     public void start() {
88         allocateCache();
89         retrieveCache();
90         objReader = new ObjectReader();
91         objWriter = new ObjectWriter();
92     }
93
94     public void destroy() {
95         // Clear local states
96         this.configurationAwareList.clear();
97     }
98
99     @Override
100     public Status saveConfigurations() {
101         if (configEvent != null) {
102             configEvent.put(ConfigurationEvent.SAVE, "");
103         }
104         return saveConfigurationsInternal();
105     }
106
107     private Status saveConfigurationsInternal() {
108         boolean success = true;
109         for (IConfigurationAware configurationAware : configurationAwareList) {
110             Status status = configurationAware.saveConfiguration();
111             if (!status.isSuccess()) {
112                 success = false;
113                 logger.warn("Failed to save config for {}",
114                         configurationAware.getClass().getName());
115             }
116         }
117         if (success) {
118             return new Status(StatusCode.SUCCESS);
119         } else {
120             return new Status(StatusCode.INTERNALERROR,
121                     "Failed to Save All Configurations");
122         }
123     }
124
125     @Override
126     public void entryCreated(ConfigurationEvent key, String cacheName,
127             boolean originLocal) {
128         if (originLocal) {
129             return;
130         }
131     }
132
133     @Override
134     public void entryUpdated(ConfigurationEvent key, String new_value,
135             String cacheName, boolean originLocal) {
136         if (originLocal) {
137             return;
138         }
139         if (key == ConfigurationEvent.SAVE) {
140             saveConfigurationsInternal();
141         }
142     }
143
144     @Override
145     public void entryDeleted(ConfigurationEvent key, String cacheName,
146             boolean originLocal) {
147         if (originLocal) {
148             return;
149         }
150     }
151
152     private void allocateCache() {
153         if (this.clusterServices == null) {
154             logger.error("uninitialized clusterServices, can't create cache");
155             return;
156         }
157         try {
158             this.clusterServices.createCache(SAVE_EVENT_CACHE,
159                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
160         } catch (CacheConfigException cce) {
161             logger.debug("Error creating ConfigurationService cache ", cce);
162         } catch (CacheExistException cce) {
163             logger.debug("ConfigurationService Cache already exists, destroy and recreate ", cce);
164         }
165     }
166
167     @SuppressWarnings({ "unchecked" })
168     private void retrieveCache() {
169         if (this.clusterServices == null) {
170             logger.error("uninitialized clusterServices, can't retrieve cache");
171             return;
172         }
173         configEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache(SAVE_EVENT_CACHE);
174         if (configEvent == null) {
175             logger.error("Failed to retrieve configuration Cache");
176         }
177     }
178
179     @Override
180     public Status persistConfiguration(List<ConfigurationObject> config, String fileName) {
181         String destination = String.format("%s%s", ROOT, fileName);
182         return objWriter.write(config, destination);
183     }
184
185     @Override
186     public List<ConfigurationObject> retrieveConfiguration(IObjectReader reader, String fileName) {
187         if (!clusterServices.amICoordinator()) {
188             return Collections.emptyList();
189         }
190         String source = String.format("%s%s", ROOT, fileName);
191         Object obj = objReader.read(reader, source);
192         return (obj == null || !(obj instanceof List)) ? Collections.<ConfigurationObject> emptyList()
193                 : (List<ConfigurationObject>) obj;
194     }
195 }