fa81ee026a39ac2766456958636515fffee4e6f5
[controller.git] / opendaylight / configuration / implementation / src / main / java / org / opendaylight / controller / configuration / internal / ConfigurationImpl.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.Set;
16 import java.util.concurrent.ConcurrentMap;
17
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;
30
31 /**
32  * @file   ConfigurationImpl.java
33  *
34  * @brief  Backend functionality for all Configuration related tasks.
35  *
36  *
37  */
38
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;
44     /*
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
48      */
49     private Set<IConfigurationAware> configurationAwareList = (Set<IConfigurationAware>) Collections
50             .synchronizedSet(new HashSet<IConfigurationAware>());
51
52
53     public int getConfigurationAwareListSize() {
54         return this.configurationAwareList.size();
55     }
56
57     public void addConfigurationAware(IConfigurationAware configurationAware) {
58         if (!this.configurationAwareList.contains(configurationAware)) {
59             this.configurationAwareList.add(configurationAware);
60         }
61     }
62
63     public void removeConfigurationAware(IConfigurationAware configurationAware) {
64         this.configurationAwareList.remove(configurationAware);
65     }
66
67     public void setClusterServices(IClusterGlobalServices i) {
68         this.clusterServices = i;
69         logger.debug("IClusterServices set");
70     }
71
72     public void unsetClusterServices(IClusterGlobalServices i) {
73         if (this.clusterServices == i) {
74             this.clusterServices = null;
75             logger.debug("IClusterServices Unset");
76         }
77     }
78
79     public void init() {
80         logger.info("ContainerManager startup....");
81     }
82
83     public void start() {
84         allocateCache();
85         retrieveCache();
86     }
87
88     public void destroy() {
89         // Clear local states
90         this.configurationAwareList.clear();
91     }
92
93     @Override
94     public Status saveConfigurations() {
95         if (configEvent != null) {
96             configEvent.put(ConfigurationEvent.SAVE, "");
97         }
98         return saveConfigurationsInternal();
99     }
100
101     private Status saveConfigurationsInternal() {
102         boolean success = true;
103         for (IConfigurationAware configurationAware : configurationAwareList) {
104             Status status = configurationAware.saveConfiguration();
105             if (!status.isSuccess()) {
106                 success = false;
107                 logger.info("Failed to save config for {}",
108                         configurationAware.getClass().getName());
109             }
110         }
111         if (success) {
112             return new Status(StatusCode.SUCCESS);
113         } else {
114             return new Status(StatusCode.INTERNALERROR,
115                     "Failed to Save All Configurations");
116         }
117     }
118
119     @Override
120     public void entryCreated(ConfigurationEvent key, String cacheName,
121             boolean originLocal) {
122         if (originLocal) return;
123     }
124
125     @Override
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();
131         }
132     }
133
134     @Override
135     public void entryDeleted(ConfigurationEvent key, String cacheName,
136             boolean originLocal) {
137         if (originLocal) return;
138     }
139
140     @SuppressWarnings("deprecation")
141     private void allocateCache() {
142         if (this.clusterServices == null) {
143             logger.error("uninitialized clusterServices, can't create cache");
144             return;
145         }
146         try {
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);
153         }
154     }
155
156     @SuppressWarnings({ "unchecked", "deprecation" })
157     private void retrieveCache() {
158         if (this.clusterServices == null) {
159             logger.error("uninitialized clusterServices, can't retrieve cache");
160             return;
161         }
162         configEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache("config.event.save");
163         if (configEvent == null) {
164             logger.error("Failed to retrieve configuration Cache");
165         }
166     }
167 }