Merge "Fixed bug when global RPCs we're not forwarded"
[controller.git] / opendaylight / configuration / implementation / src / main / java / org / opendaylight / controller / configuration / internal / ContainerConfigurationService.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.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;
31
32 /**
33  * @file   ConfigurationImpl.java
34  *
35  * @brief  Backend functionality for all ConfigurationService related tasks.
36  *
37  *
38  */
39
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;
46     /*
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
50      */
51     private Set<IConfigurationContainerAware> configurationAwareList = Collections
52             .synchronizedSet(new HashSet<IConfigurationContainerAware>());
53
54     public void addConfigurationContainerAware(
55             IConfigurationContainerAware configurationAware) {
56         if (!this.configurationAwareList.contains(configurationAware)) {
57             this.configurationAwareList.add(configurationAware);
58         }
59     }
60
61     public int getConfigurationAwareListSize() {
62         return this.configurationAwareList.size();
63     }
64
65     public void removeConfigurationContainerAware(
66             IConfigurationContainerAware configurationAware) {
67         this.configurationAwareList.remove(configurationAware);
68     }
69
70     public void setClusterServices(IClusterContainerServices i) {
71         this.clusterServices = i;
72         logger.debug("IClusterServices set");
73     }
74
75     public void unsetClusterServices(IClusterContainerServices i) {
76         if (this.clusterServices == i) {
77             this.clusterServices = null;
78             logger.debug("IClusterServices Unset");
79         }
80     }
81
82     public void init() {
83     }
84
85     public void start() {
86         allocateCache();
87         retrieveCache();
88     }
89
90     public void destroy() {
91         // Clear local states
92         this.configurationAwareList.clear();
93     }
94
95     @Override
96     public Status saveConfiguration() {
97         boolean success = true;
98         for (IConfigurationContainerAware configurationAware : configurationAwareList) {
99             logger.trace("Save Config triggered for {}", configurationAware.getClass().getSimpleName());
100
101             Status status = configurationAware.saveConfiguration();
102             if (!status.isSuccess()) {
103                 success = false;
104                 logger.warn("Failed to save config for {}", configurationAware.getClass().getSimpleName());
105             }
106         }
107         if (success) {
108             return new Status(StatusCode.SUCCESS);
109         } else {
110             return new Status(StatusCode.INTERNALERROR, "Failed to Save All Configurations");
111         }
112     }
113
114     @Override
115     public Status saveConfigurations() {
116         containerConfigEvent.put(ConfigurationEvent.SAVE, "");
117         return saveConfiguration();
118     }
119
120     @Override
121     public void entryCreated(ConfigurationEvent key, String cacheName,
122             boolean originLocal) {
123         if (originLocal) {
124             return;
125         }
126     }
127
128     @Override
129     public void entryUpdated(ConfigurationEvent key, String new_value,
130             String cacheName, boolean originLocal) {
131         if (originLocal) {
132             return;
133         }
134         logger.debug("Processing {} event", key);
135         if (key == ConfigurationEvent.SAVE) {
136             saveConfiguration();
137         }
138     }
139
140     @Override
141     public void entryDeleted(ConfigurationEvent key, String cacheName,
142             boolean originLocal) {
143         if (originLocal) {
144             return;
145         }
146     }
147
148     private void allocateCache() {
149         if (this.clusterServices == null) {
150             logger.error("uninitialized clusterServices, can't create cache");
151             return;
152         }
153         try {
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);
160         }
161     }
162
163     @SuppressWarnings({ "unchecked" })
164     private void retrieveCache() {
165         if (this.clusterServices == null) {
166             logger.error("uninitialized clusterServices, can't retrieve cache");
167             return;
168         }
169         containerConfigEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache(CONTAINER_SAVE_EVENT_CACHE);
170         if (containerConfigEvent == null) {
171             logger.error("Failed to retrieve configuration Cache");
172         }
173     }
174 }