Fixed inappropriate uses of log level INFO
[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.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 ConfigurationService related tasks.
35  *
36  */
37
38 public class ConfigurationService implements IConfigurationService, ICacheUpdateAware<ConfigurationEvent, String> {
39     private static final Logger logger = LoggerFactory
40             .getLogger(ConfigurationService.class);
41     public static final String SAVE_EVENT_CACHE = "config.event.save";
42     private IClusterGlobalServices clusterServices;
43     private ConcurrentMap <ConfigurationEvent, String> configEvent;
44     private Set<IConfigurationAware> configurationAwareList = Collections
45             .synchronizedSet(new HashSet<IConfigurationAware>());
46
47
48     public int getConfigurationAwareListSize() {
49         return this.configurationAwareList.size();
50     }
51
52     public void addConfigurationAware(IConfigurationAware configurationAware) {
53         if (!this.configurationAwareList.contains(configurationAware)) {
54             this.configurationAwareList.add(configurationAware);
55         }
56     }
57
58     public void removeConfigurationAware(IConfigurationAware configurationAware) {
59         this.configurationAwareList.remove(configurationAware);
60     }
61
62     public void setClusterServices(IClusterGlobalServices i) {
63         this.clusterServices = i;
64         logger.debug("IClusterServices set");
65     }
66
67     public void unsetClusterServices(IClusterGlobalServices i) {
68         if (this.clusterServices == i) {
69             this.clusterServices = null;
70             logger.debug("IClusterServices Unset");
71         }
72     }
73
74     public void init() {
75         logger.info("ConfigurationService Manager init");
76     }
77
78     public void start() {
79         allocateCache();
80         retrieveCache();
81     }
82
83     public void destroy() {
84         // Clear local states
85         this.configurationAwareList.clear();
86     }
87
88     @Override
89     public Status saveConfigurations() {
90         if (configEvent != null) {
91             configEvent.put(ConfigurationEvent.SAVE, "");
92         }
93         return saveConfigurationsInternal();
94     }
95
96     private Status saveConfigurationsInternal() {
97         boolean success = true;
98         for (IConfigurationAware configurationAware : configurationAwareList) {
99             Status status = configurationAware.saveConfiguration();
100             if (!status.isSuccess()) {
101                 success = false;
102                 logger.warn("Failed to save config for {}",
103                         configurationAware.getClass().getName());
104             }
105         }
106         if (success) {
107             return new Status(StatusCode.SUCCESS);
108         } else {
109             return new Status(StatusCode.INTERNALERROR,
110                     "Failed to Save All Configurations");
111         }
112     }
113
114     @Override
115     public void entryCreated(ConfigurationEvent key, String cacheName,
116             boolean originLocal) {
117         if (originLocal) {
118             return;
119         }
120     }
121
122     @Override
123     public void entryUpdated(ConfigurationEvent key, String new_value,
124             String cacheName, boolean originLocal) {
125         if (originLocal) {
126             return;
127         }
128         if (key == ConfigurationEvent.SAVE) {
129             saveConfigurationsInternal();
130         }
131     }
132
133     @Override
134     public void entryDeleted(ConfigurationEvent key, String cacheName,
135             boolean originLocal) {
136         if (originLocal) {
137             return;
138         }
139     }
140
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(SAVE_EVENT_CACHE,
148                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
149         } catch (CacheConfigException cce) {
150             logger.debug("Error creating ConfigurationService cache ", cce);
151         } catch (CacheExistException cce) {
152             logger.debug("ConfigurationService Cache already exists, destroy and recreate ", cce);
153         }
154     }
155
156     @SuppressWarnings({ "unchecked" })
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(SAVE_EVENT_CACHE);
163         if (configEvent == null) {
164             logger.error("Failed to retrieve configuration Cache");
165         }
166     }
167 }