Fix: Flows are deleted when another controller is started
[controller.git] / opendaylight / sal / connection / implementation / src / main / java / org / opendaylight / controller / sal / connection / implementation / internal / ConnectionService.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.connection.implementation.internal;
10
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.Collections;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17
18 import org.opendaylight.controller.sal.connection.ConnectionConstants;
19 import org.opendaylight.controller.sal.connection.IConnectionListener;
20 import org.opendaylight.controller.sal.connection.IConnectionService;
21 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
22 import org.opendaylight.controller.sal.connection.IPluginOutConnectionService;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.utils.GlobalConstants;
25 import org.opendaylight.controller.sal.utils.Status;
26 import org.opendaylight.controller.sal.utils.StatusCode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class ConnectionService implements IPluginOutConnectionService, IConnectionService {
31     protected static final Logger logger = LoggerFactory
32             .getLogger(ConnectionService.class);
33     private IConnectionListener connectionListener;
34     private ConcurrentMap<String, IPluginInConnectionService> pluginService =
35             new ConcurrentHashMap<String, IPluginInConnectionService>();
36
37     void setPluginService (Map props, IPluginInConnectionService s) {
38         String type = null;
39         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
40         if (value instanceof String) {
41             type = (String) value;
42         }
43         if (type == null) {
44             logger.error("Received a PluginInConnectionService without any "
45                     + "protocolPluginType provided");
46         } else {
47             this.pluginService.put(type, s);
48         }
49     }
50
51     void unsetPluginService(Map props, IPluginInConnectionService s) {
52         String type = null;
53
54         Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString());
55         if (value instanceof String) {
56             type = (String) value;
57         }
58         if (type == null) {
59             logger.error("Received a PluginInConnectionService without any "
60                     + "protocolPluginType provided");
61         } else if (this.pluginService.get(type).equals(s)) {
62             this.pluginService.remove(type);
63         }
64     }
65
66     void setListener(IConnectionListener s) {
67         this.connectionListener = s;
68     }
69
70     void unsetListener(IConnectionListener s) {
71         if (this.connectionListener == s) {
72             this.connectionListener = null;
73         }
74     }
75
76     /**
77      * Function called by the dependency manager when all the required
78      * dependencies are satisfied
79      *
80      */
81     void init() {
82     }
83
84     /**
85      * Function called by the dependency manager when at least one dependency
86      * become unsatisfied or when the component is shutting down because for
87      * example bundle is being stopped.
88      *
89      */
90     void destroy() {
91         connectionListener = null;
92         if (this.pluginService != null) {
93             this.pluginService.clear();
94         }
95     }
96
97     /**
98      * Method to test if a node is local to a controller.
99      *
100      * @return true if node is local to this controller. false otherwise.
101      */
102     public boolean isLocal(Node node) {
103         if (this.connectionListener == null) return false;
104         return connectionListener.isLocal(node);
105     }
106
107     @Override
108     public Node connect (String type, String connectionIdentifier, Map<ConnectionConstants, String> params) {
109         IPluginInConnectionService s = pluginService.get(type);
110         if (s != null) return s.connect(connectionIdentifier, params);
111         return null;
112     }
113
114     @Override
115     public Node connect (String connectionIdentifier, Map<ConnectionConstants, String> params) {
116         synchronized (this.pluginService) {
117             for (String pluginType : this.pluginService.keySet()) {
118                 IPluginInConnectionService s = pluginService.get(pluginType);
119                 Node node = s.connect(connectionIdentifier, params);
120                 if (node != null) return node;
121             }
122         }
123         return null;
124     }
125
126     @Override
127     public Status disconnect(Node node) {
128         IPluginInConnectionService s = pluginService.get(node.getType());
129         if (s != null) return s.disconnect(node);
130         return new Status(StatusCode.NOTFOUND);
131     }
132
133     /**
134      * View Change notification
135      */
136     @Override
137     public void notifyClusterViewChanged() {
138         for (String pluginType : this.pluginService.keySet()) {
139             IPluginInConnectionService s = pluginService.get(pluginType);
140             s.notifyClusterViewChanged();
141         }
142     }
143
144     /**
145      * Node Disconnected from the node's master controller.
146      */
147     @Override
148     public void notifyNodeDisconnectFromMaster(Node node) {
149         for (String pluginType : this.pluginService.keySet()) {
150             IPluginInConnectionService s = pluginService.get(pluginType);
151             s.notifyNodeDisconnectFromMaster(node);
152         }
153     }
154 }