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.IClusterContainerServices;
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.IConfigurationContainerAware;
26 import org.opendaylight.controller.configuration.IConfigurationContainerService;
27 import org.opendaylight.controller.sal.utils.Status;
28 import org.opendaylight.controller.sal.utils.StatusCode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * @file ConfigurationImpl.java
35 * @brief Backend functionality for all ConfigurationService related tasks.
40 public class ContainerConfigurationService implements IConfigurationContainerService, IConfigurationAware,
41 ICacheUpdateAware<ConfigurationEvent, String> {
42 public static final String CONTAINER_SAVE_EVENT_CACHE = "config.container.event.save";
43 private static final Logger logger = LoggerFactory.getLogger(ContainerConfigurationService.class);
44 private IClusterContainerServices clusterServices;
45 private ConcurrentMap <ConfigurationEvent, String> containerConfigEvent;
47 * Collection containing the configuration objects.
48 * This is configuration world: container names (also the map key)
49 * are maintained as they were configured by user, same case
51 private Set<IConfigurationContainerAware> configurationAwareList = Collections
52 .synchronizedSet(new HashSet<IConfigurationContainerAware>());
54 public void addConfigurationContainerAware(
55 IConfigurationContainerAware configurationAware) {
56 if (!this.configurationAwareList.contains(configurationAware)) {
57 this.configurationAwareList.add(configurationAware);
61 public int getConfigurationAwareListSize() {
62 return this.configurationAwareList.size();
65 public void removeConfigurationContainerAware(
66 IConfigurationContainerAware configurationAware) {
67 this.configurationAwareList.remove(configurationAware);
70 public void setClusterServices(IClusterContainerServices i) {
71 this.clusterServices = i;
72 logger.debug("IClusterServices set");
75 public void unsetClusterServices(IClusterContainerServices i) {
76 if (this.clusterServices == i) {
77 this.clusterServices = null;
78 logger.debug("IClusterServices Unset");
90 public void destroy() {
92 this.configurationAwareList.clear();
96 public Status saveConfiguration() {
97 boolean success = true;
98 for (IConfigurationContainerAware configurationAware : configurationAwareList) {
99 logger.trace("Save Config triggered for {}", configurationAware.getClass().getSimpleName());
101 Status status = configurationAware.saveConfiguration();
102 if (!status.isSuccess()) {
104 logger.warn("Failed to save config for {}", configurationAware.getClass().getSimpleName());
108 return new Status(StatusCode.SUCCESS);
110 return new Status(StatusCode.INTERNALERROR, "Failed to Save All Configurations");
115 public Status saveConfigurations() {
116 containerConfigEvent.put(ConfigurationEvent.SAVE, "");
117 return saveConfiguration();
121 public void entryCreated(ConfigurationEvent key, String cacheName,
122 boolean originLocal) {
129 public void entryUpdated(ConfigurationEvent key, String new_value,
130 String cacheName, boolean originLocal) {
134 logger.debug("Processing {} event", key);
135 if (key == ConfigurationEvent.SAVE) {
141 public void entryDeleted(ConfigurationEvent key, String cacheName,
142 boolean originLocal) {
148 private void allocateCache() {
149 if (this.clusterServices == null) {
150 logger.error("uninitialized clusterServices, can't create cache");
154 this.clusterServices.createCache(CONTAINER_SAVE_EVENT_CACHE,
155 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
156 } catch (CacheConfigException cce) {
157 logger.debug("Error creating ContainerConfigurationService cache ", cce);
158 } catch (CacheExistException cce) {
159 logger.debug("ConfigurationService Cache already exists, destroy and recreate ", cce);
163 @SuppressWarnings({ "unchecked" })
164 private void retrieveCache() {
165 if (this.clusterServices == null) {
166 logger.error("uninitialized clusterServices, can't retrieve cache");
169 containerConfigEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache(CONTAINER_SAVE_EVENT_CACHE);
170 if (containerConfigEvent == null) {
171 logger.error("Failed to retrieve configuration Cache");