2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.switchmanager.internal;
10 import java.net.NetworkInterface;
11 import java.net.SocketException;
12 import java.util.EnumSet;
13 import java.util.Enumeration;
14 import java.util.HashMap;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
19 import org.apache.felix.dm.Component;
20 import org.opendaylight.controller.clustering.services.CacheConfigException;
21 import org.opendaylight.controller.clustering.services.CacheExistException;
22 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
23 import org.opendaylight.controller.clustering.services.IClusterServices;
24 import org.opendaylight.controller.sal.core.MacAddress;
25 import org.opendaylight.controller.sal.core.Property;
26 import org.opendaylight.controller.sal.utils.HexEncode;
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 * The class ControllerProperties
34 * provides implementation for the ControllerProperties API
36 public class ControllerProperties implements IControllerProperties {
38 private IClusterGlobalServices clusterService = null;
39 private final Logger log = LoggerFactory
40 .getLogger(ControllerProperties.class);
41 private static final String controllerGlobalPropertiesCacheName = "switchmanager.controllerGlobalProperties";
43 private ConcurrentMap<String, Property> controllerGlobalProperties;
45 private void allocateCaches() {
46 if (this.clusterService == null) {
47 this.nonClusterObjectCreate();
48 log.warn("un-initialized clusterService, can't create cache");
52 this.clusterService.createCache(controllerGlobalPropertiesCacheName,
53 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
54 } catch (CacheConfigException cce) {
55 log.error("\nCache configuration invalid - check cache mode");
56 } catch (CacheExistException ce) {
57 log.error("\nCache already exits - destroy and recreate if needed");
61 private void nonClusterObjectCreate() {
62 controllerGlobalProperties = new ConcurrentHashMap<String, Property>();
65 @SuppressWarnings({ "unchecked" })
66 private void retrieveCaches() {
67 if (this.clusterService == null) {
68 log.warn("un-initialized clusterService, can't create cache");
71 controllerGlobalProperties = (ConcurrentMap<String, Property>) this.clusterService
72 .getCache(controllerGlobalPropertiesCacheName);
73 if (controllerGlobalProperties == null) {
74 log.error("\nFailed to get cache for controllerGlobalProperties");
79 * Function called by the dependency manager when all the required
80 * dependencies are satisfied
84 void init(Component c) {
85 // Instantiate cluster synced variables
90 private void initializeProperties() {
91 byte controllerMac[] = getHardwareMAC();
92 if (controllerMac != null) {
93 Property existing = controllerGlobalProperties.putIfAbsent(MacAddress.name, new MacAddress(controllerMac));
94 if (existing == null && log.isTraceEnabled()) {
95 log.trace("Setting controller MAC address in the cluster: {}", HexEncode.bytesToHexStringFormat(controllerMac));
100 private byte[] getHardwareMAC() {
101 Enumeration<NetworkInterface> nis;
102 byte[] macAddress = null;
105 nis = NetworkInterface.getNetworkInterfaces();
106 } catch (SocketException e) {
107 log.error("Failed to acquire controller MAC: ", e);
111 while (nis.hasMoreElements()) {
112 NetworkInterface ni = nis.nextElement();
114 macAddress = ni.getHardwareAddress();
115 } catch (SocketException e) {
116 log.error("Failed to acquire controller MAC: ", e);
118 if (macAddress != null && macAddress.length != 0) {
122 if (macAddress == null) {
123 log.warn("Failed to acquire controller MAC: No physical interface found");
124 // This happens when running controller on windows VM, for example
125 // TODO: Try parsing the OS command output
126 // For now provide a quick fix for the release
127 macAddress = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x0c, (byte) 0x60, (byte) 0x0D, (byte) 0x10 };
128 log.debug("Assigning custom MAC address to controller");
134 * Function called by dependency manager after "init ()" is called and after
135 * the services provided by the class are registered in the service registry
139 // initialize required properties if first node in the cluster
140 if(this.clusterService.amICoordinator()) {
141 initializeProperties();
146 * Function called after registered the service in OSGi service registry.
153 * Function called before services of the component are removed
154 * from OSGi service registry.
161 * Function called by the dependency manager before the services exported by
162 * the component are unregistered, this will be followed by a "destroy ()"
171 * Function called by the dependency manager when at least one dependency
172 * become unsatisfied or when the component is shutting down because for
173 * example bundle is being stopped.
184 public Map<String, Property> getControllerProperties() {
185 return new HashMap<String, Property>(this.controllerGlobalProperties);
192 public Property getControllerProperty(String propertyName) {
193 if (propertyName != null) {
194 return this.controllerGlobalProperties.get(propertyName);
203 public Status setControllerProperty(Property property) {
204 if (property != null) {
205 this.controllerGlobalProperties.put(property.getName(), property);
206 return new Status(StatusCode.SUCCESS);
208 return new Status(StatusCode.BADREQUEST, "Invalid property provided when setting property");
215 public Status removeControllerProperty(String propertyName) {
216 if (propertyName != null) {
217 this.controllerGlobalProperties.remove(propertyName);
218 return new Status(StatusCode.SUCCESS);
220 return new Status(StatusCode.BADREQUEST, "Invalid property provided when removing property");
227 public void setClusterService(IClusterGlobalServices s) {
228 this.clusterService = s;
232 * unsetClusterServices
235 public void unsetClusterServices(IClusterGlobalServices s) {
236 if (this.clusterService == s) {
237 this.clusterService = null;