Feature uses features-parent as parent
[groupbasedpolicy.git] / renderers / opflex / src / main / java / org / opendaylight / groupbasedpolicy / renderer / opflex / lib / OpflexRpcServer.java
1 /*
2  * Copyright (C) 2014 Cisco Systems, Inc.
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  * Authors : Thomas Bachman
9  */
10 package org.opendaylight.groupbasedpolicy.renderer.opflex.lib;
11
12 import java.util.List;
13
14 import org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc.ConnectionService;
15 import org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc.RpcBroker;
16 import org.opendaylight.groupbasedpolicy.renderer.opflex.jsonrpc.RpcServer;
17
18 /**
19  * The {@link OpflexRpcServer}s respond to OpFlex clients
20  * which create {@link OpflexAgent} objects when they
21  * are established. The servers don't own the connections,
22  * which allows the clients to continue operation even if
23  * the server is closed
24  *
25  * @author tbachman
26  */
27 public class OpflexRpcServer {
28
29     private String identity;
30     private final String domain;
31     private final List<Role> roles;
32     private final RpcServer rpcServer;
33     private ConnectionService connectionService;
34     private RpcBroker rpcBroker;
35
36     private String address;
37     private int port;
38
39     private void parseAndSetIdentity(String id) {
40         if (id.split(":").length == 2) {
41             this.identity = id;
42             this.address = id.split(":")[0];
43             this.port = Integer.parseInt(id.split(":")[1]);
44         }
45     }
46
47     public OpflexRpcServer(String domain, String identity, List<Role> roles) {
48         this.domain = domain;
49         this.roles = roles;
50         parseAndSetIdentity(identity);
51         rpcServer = new RpcServer(address, port);
52         rpcServer.setContext(this);
53     }
54
55     public String getDomain() {
56         return domain;
57     }
58
59     public String getId() {
60         return this.identity;
61     }
62
63     public RpcServer getRpcServer() {
64         return rpcServer;
65     }
66
67     public ConnectionService getConnectionService() {
68         return connectionService;
69     }
70
71     public void setConnectionService(ConnectionService service) {
72         this.connectionService = service;
73     }
74
75     public String getAddress() {
76         return address;
77     }
78
79     public int getPort() {
80         return port;
81     }
82
83     public RpcBroker getRpcBroker() {
84         return this.rpcBroker;
85     }
86
87     public void setRpcBroker(RpcBroker rpcBroker) {
88         this.rpcBroker = rpcBroker;
89     }
90
91     public List<Role> getRoles() {
92         return this.roles;
93     }
94
95     /**
96      * Start the {@link OpflexRpcServer}. This adds the supported
97      * messages to the server, based on the roles that were
98      * configured. It creates an {@link RpcServer} object,
99      * passes it the context owned by the {@link OpflexRpcServer},
100      * and starts the server in its own thread.
101      * TODO: should use executor service instead?
102      */
103     public void start() {
104         rpcServer.setConnectionService(connectionService);
105         rpcServer.setRpcBroker(rpcBroker);
106
107         for (Role role : roles) {
108             rpcServer.addMessageList(role.getMessages());
109         }
110         /*
111          * All servers get Discovery messages
112          */
113         rpcServer.addMessageList(Role.DISCOVERY.getMessages());
114
115         new Thread() {
116
117             private RpcServer server;
118
119             public Thread initializeServerParams(RpcServer srv) {
120                 this.server = srv;
121                 return this;
122             }
123
124             @Override
125             public void run() {
126                 try {
127                     server.start();
128                 } catch (Exception e) {
129                 }
130             }
131         }.initializeServerParams(rpcServer).start();
132
133     }
134
135     /**
136      * Check to see if two servers are the same. They
137      * need to be in the same Opflex Domain, have the same
138      * identity, and the same roles, or they can be
139      * identical objects. Note that it purposely does
140      * not compare the RpcServer, as the purpose for
141      * this method is to see if there is already a server
142      * fulfilling this configuration (which is the reason
143      * it's a new method, instead of overriding toString).
144      *
145      * @param srv The server to compare against
146      * @return true if they are equivalent
147      */
148     public boolean sameServer(OpflexRpcServer srv) {
149         if (this == srv)
150             return true;
151         if (srv == null)
152             return false;
153         if (!this.identity.equals(srv.identity))
154             return false;
155         if (this.domain == null || !this.domain.equals(srv.getDomain()))
156             return false;
157         if (this.roles == null && srv.roles == null)
158             return true;
159         if (this.roles == null || srv.roles == null)
160             return false;
161         if (this.roles.size() == srv.roles.size() && this.roles.containsAll(srv.roles))
162             return true;
163         return false;
164     }
165 }