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;
16 import java.util.concurrent.ConcurrentMap;
18 import org.opendaylight.controller.clustering.services.CacheConfigException;
19 import org.opendaylight.controller.clustering.services.CacheExistException;
20 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
21 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
22 import org.opendaylight.controller.clustering.services.IClusterServices;
23 import org.opendaylight.controller.configuration.ConfigurationEvent;
24 import org.opendaylight.controller.configuration.IConfigurationAware;
25 import org.opendaylight.controller.configuration.IConfigurationService;
26 import org.opendaylight.controller.sal.utils.StatusCode;
27 import org.opendaylight.controller.sal.utils.Status;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * @file ConfigurationImpl.java
34 * @brief Backend functionality for all Configuration related tasks.
39 public class ConfigurationImpl implements IConfigurationService, ICacheUpdateAware<ConfigurationEvent, String> {
40 private static final Logger logger = LoggerFactory
41 .getLogger(ConfigurationImpl.class);
42 private IClusterGlobalServices clusterServices;
43 private ConcurrentMap <ConfigurationEvent, String> configEvent;
45 * Collection containing the configuration objects.
46 * This is configuration world: container names (also the map key)
47 * are maintained as they were configured by user, same case
49 private Set<IConfigurationAware> configurationAwareList = (Set<IConfigurationAware>) Collections
50 .synchronizedSet(new HashSet<IConfigurationAware>());
53 public int getConfigurationAwareListSize() {
54 return this.configurationAwareList.size();
57 public void addConfigurationAware(IConfigurationAware configurationAware) {
58 if (!this.configurationAwareList.contains(configurationAware)) {
59 this.configurationAwareList.add(configurationAware);
63 public void removeConfigurationAware(IConfigurationAware configurationAware) {
64 this.configurationAwareList.remove(configurationAware);
67 public void setClusterServices(IClusterGlobalServices i) {
68 this.clusterServices = i;
69 logger.debug("IClusterServices set");
72 public void unsetClusterServices(IClusterGlobalServices i) {
73 if (this.clusterServices == i) {
74 this.clusterServices = null;
75 logger.debug("IClusterServices Unset");
80 logger.info("ContainerManager startup....");
88 public void destroy() {
90 this.configurationAwareList.clear();
94 public Status saveConfigurations() {
95 if (configEvent != null) {
96 configEvent.put(ConfigurationEvent.SAVE, "");
98 return saveConfigurationsInternal();
101 private Status saveConfigurationsInternal() {
102 boolean success = true;
103 for (IConfigurationAware configurationAware : configurationAwareList) {
104 Status status = configurationAware.saveConfiguration();
105 if (!status.isSuccess()) {
107 logger.info("Failed to save config for {}",
108 configurationAware.getClass().getName());
112 return new Status(StatusCode.SUCCESS);
114 return new Status(StatusCode.INTERNALERROR,
115 "Failed to Save All Configurations");
120 public void entryCreated(ConfigurationEvent key, String cacheName,
121 boolean originLocal) {
122 if (originLocal) return;
126 public void entryUpdated(ConfigurationEvent key, String new_value,
127 String cacheName, boolean originLocal) {
128 if (originLocal) return;
129 if (key == ConfigurationEvent.SAVE) {
130 saveConfigurationsInternal();
135 public void entryDeleted(ConfigurationEvent key, String cacheName,
136 boolean originLocal) {
137 if (originLocal) return;
140 @SuppressWarnings("deprecation")
141 private void allocateCache() {
142 if (this.clusterServices == null) {
143 logger.error("uninitialized clusterServices, can't create cache");
147 this.clusterServices.createCache("config.event.save",
148 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
149 } catch (CacheConfigException cce) {
150 logger.error("Error creating Configuration cache ", cce);
151 } catch (CacheExistException cce) {
152 logger.error("Configuration Cache already exists, destroy and recreate ", cce);
156 @SuppressWarnings({ "unchecked", "deprecation" })
157 private void retrieveCache() {
158 if (this.clusterServices == null) {
159 logger.error("uninitialized clusterServices, can't retrieve cache");
162 configEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache("config.event.save");
163 if (configEvent == null) {
164 logger.error("Failed to retrieve configuration Cache");