Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / groupbasedpolicy / 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.jsonrpc.ConnectionService;
15 import org.opendaylight.groupbasedpolicy.jsonrpc.RpcBroker;
16 import org.opendaylight.groupbasedpolicy.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  */
28 public class OpflexRpcServer {
29
30     private String identity;
31     private String domain;
32     private List<Role> roles;
33     private RpcServer rpcServer;
34     private ConnectionService connectionService;
35     private RpcBroker rpcBroker;
36
37     private String address;
38     private int port;
39
40     private void parseAndSetIdentity(String id) {
41         if (id.split(":").length == 2) {
42             this.identity = id;
43             this.address = id.split(":")[0];
44             this.port =  Integer.parseInt(id.split(":")[1]);
45         }
46     }
47
48     public OpflexRpcServer(String domain, String identity, List<Role> roles) {
49         this.domain = domain;
50         this.roles = roles;
51         parseAndSetIdentity(identity);
52         rpcServer = new RpcServer(address, port);
53         rpcServer.setContext(this);
54     }
55
56     public String getDomain() {
57         return domain;
58     }
59
60     public String getId() {
61         return this.identity;
62     }
63
64     public RpcServer getRpcServer() {
65         return rpcServer;
66     }
67
68     public ConnectionService getConnectionService() {
69         return connectionService;
70     }
71
72     public void setConnectionService(ConnectionService service) {
73         this.connectionService = service;
74     }
75
76     public String getAddress() {
77         return address;
78     }
79
80     public int getPort() {
81         return port;
82     }
83
84     public RpcBroker getRpcBroker() {
85         return this.rpcBroker;
86     }
87
88     public void setRpcBroker(RpcBroker rpcBroker) {
89         this.rpcBroker = rpcBroker;
90     }
91
92     public List<Role> getRoles() {
93         return this.roles;
94     }
95
96     /**
97      * Start the {@link OpflexRpcServer}. This adds the supported
98      * messages to the server, based on the roles that were
99      * configured. It creates an {@link RpcServer} object,
100      * passes it the context owned by the {@link OpflexRpcServer},
101      * and starts the server in its own thread.
102      *
103      * TODO: should use executor service instead?
104      */
105     public void start() {
106         rpcServer.setConnectionService(connectionService);
107         rpcServer.setRpcBroker(rpcBroker);
108
109         for ( Role role : roles ) {
110             rpcServer.addMessageList(role.getMessages());
111         }
112         /*
113          * All servers get Discovery messages
114          */
115         rpcServer.addMessageList(Role.DISCOVERY.getMessages());
116
117         new Thread() {
118             private RpcServer server;
119
120             public Thread initializeServerParams(RpcServer srv) {
121                 this.server = srv;
122                 return this;
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 ||
156                 !this.domain.equals(srv.getDomain()))
157             return false;
158         if (this.roles == null && srv.roles == null)
159             return true;
160         if (this.roles == null || srv.roles == null)
161             return false;
162         if (this.roles.size() == srv.roles.size()
163                 && this.roles.containsAll(srv.roles))
164             return true;
165         return false;
166     }
167 }