3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.configuration.internal;
12 import java.util.Collections;
13 import java.util.EnumSet;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.concurrent.ConcurrentMap;
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;
38 * @file ConfigurationImpl.java
40 * @brief Backend functionality for all ConfigurationService related tasks.
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;
57 public int getConfigurationAwareListSize() {
58 return this.configurationAwareList.size();
61 public void addConfigurationAware(IConfigurationAware configurationAware) {
62 if (!this.configurationAwareList.contains(configurationAware)) {
63 this.configurationAwareList.add(configurationAware);
67 public void removeConfigurationAware(IConfigurationAware configurationAware) {
68 this.configurationAwareList.remove(configurationAware);
71 public void setClusterServices(IClusterGlobalServices i) {
72 this.clusterServices = i;
73 logger.debug("IClusterServices set");
76 public void unsetClusterServices(IClusterGlobalServices i) {
77 if (this.clusterServices == i) {
78 this.clusterServices = null;
79 logger.debug("IClusterServices Unset");
84 logger.info("ConfigurationService Manager init");
90 objReader = new ObjectReader();
91 objWriter = new ObjectWriter();
94 public void destroy() {
96 this.configurationAwareList.clear();
100 public Status saveConfigurations() {
101 if (configEvent != null) {
102 configEvent.put(ConfigurationEvent.SAVE, "");
104 return saveConfigurationsInternal();
107 private Status saveConfigurationsInternal() {
108 boolean success = true;
109 for (IConfigurationAware configurationAware : configurationAwareList) {
110 Status status = configurationAware.saveConfiguration();
111 if (!status.isSuccess()) {
113 logger.warn("Failed to save config for {}",
114 configurationAware.getClass().getName());
118 return new Status(StatusCode.SUCCESS);
120 return new Status(StatusCode.INTERNALERROR,
121 "Failed to Save All Configurations");
126 public void entryCreated(ConfigurationEvent key, String cacheName,
127 boolean originLocal) {
134 public void entryUpdated(ConfigurationEvent key, String new_value,
135 String cacheName, boolean originLocal) {
139 if (key == ConfigurationEvent.SAVE) {
140 saveConfigurationsInternal();
145 public void entryDeleted(ConfigurationEvent key, String cacheName,
146 boolean originLocal) {
152 private void allocateCache() {
153 if (this.clusterServices == null) {
154 logger.error("uninitialized clusterServices, can't create cache");
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);
167 @SuppressWarnings({ "unchecked" })
168 private void retrieveCache() {
169 if (this.clusterServices == null) {
170 logger.error("uninitialized clusterServices, can't retrieve cache");
173 configEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache(SAVE_EVENT_CACHE);
174 if (configEvent == null) {
175 logger.error("Failed to retrieve configuration Cache");
180 public Status persistConfiguration(List<ConfigurationObject> config, String fileName) {
181 String destination = String.format("%s%s", ROOT, fileName);
182 return objWriter.write(config, destination);
186 public List<ConfigurationObject> retrieveConfiguration(IObjectReader reader, String fileName) {
187 if (!clusterServices.amICoordinator()) {
188 return Collections.emptyList();
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;