2 * Copyright (C) 2014 Cisco Systems, Inc.
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 * Authors : Thomas Bachman
10 package org.opendaylight.groupbasedpolicy.renderer.opflex.lib;
12 import java.util.List;
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;
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
27 public class OpflexRpcServer {
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;
36 private String address;
39 private void parseAndSetIdentity(String id) {
40 if (id.split(":").length == 2) {
42 this.address = id.split(":")[0];
43 this.port = Integer.parseInt(id.split(":")[1]);
47 public OpflexRpcServer(String domain, String identity, List<Role> roles) {
50 parseAndSetIdentity(identity);
51 rpcServer = new RpcServer(address, port);
52 rpcServer.setContext(this);
55 public String getDomain() {
59 public String getId() {
63 public RpcServer getRpcServer() {
67 public ConnectionService getConnectionService() {
68 return connectionService;
71 public void setConnectionService(ConnectionService service) {
72 this.connectionService = service;
75 public String getAddress() {
79 public int getPort() {
83 public RpcBroker getRpcBroker() {
84 return this.rpcBroker;
87 public void setRpcBroker(RpcBroker rpcBroker) {
88 this.rpcBroker = rpcBroker;
91 public List<Role> getRoles() {
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?
103 public void start() {
104 rpcServer.setConnectionService(connectionService);
105 rpcServer.setRpcBroker(rpcBroker);
107 for (Role role : roles) {
108 rpcServer.addMessageList(role.getMessages());
111 * All servers get Discovery messages
113 rpcServer.addMessageList(Role.DISCOVERY.getMessages());
117 private RpcServer server;
119 public Thread initializeServerParams(RpcServer srv) {
128 } catch (Exception e) {
131 }.initializeServerParams(rpcServer).start();
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).
145 * @param srv The server to compare against
146 * @return true if they are equivalent
148 public boolean sameServer(OpflexRpcServer srv) {
153 if (!this.identity.equals(srv.identity))
155 if (this.domain == null || !this.domain.equals(srv.getDomain()))
157 if (this.roles == null && srv.roles == null)
159 if (this.roles == null || srv.roles == null)
161 if (this.roles.size() == srv.roles.size() && this.roles.containsAll(srv.roles))