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.jsonrpc.ConnectionService;
15 import org.opendaylight.groupbasedpolicy.jsonrpc.RpcBroker;
16 import org.opendaylight.groupbasedpolicy.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
28 public class OpflexRpcServer {
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;
37 private String address;
40 private void parseAndSetIdentity(String id) {
41 if (id.split(":").length == 2) {
43 this.address = id.split(":")[0];
44 this.port = Integer.parseInt(id.split(":")[1]);
48 public OpflexRpcServer(String domain, String identity, List<Role> roles) {
51 parseAndSetIdentity(identity);
52 rpcServer = new RpcServer(address, port);
53 rpcServer.setContext(this);
56 public String getDomain() {
60 public String getId() {
64 public RpcServer getRpcServer() {
68 public ConnectionService getConnectionService() {
69 return connectionService;
72 public void setConnectionService(ConnectionService service) {
73 this.connectionService = service;
76 public String getAddress() {
80 public int getPort() {
84 public RpcBroker getRpcBroker() {
85 return this.rpcBroker;
88 public void setRpcBroker(RpcBroker rpcBroker) {
89 this.rpcBroker = rpcBroker;
92 public List<Role> getRoles() {
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.
103 * TODO: should use executor service instead?
105 public void start() {
106 rpcServer.setConnectionService(connectionService);
107 rpcServer.setRpcBroker(rpcBroker);
109 for ( Role role : roles ) {
110 rpcServer.addMessageList(role.getMessages());
113 * All servers get Discovery messages
115 rpcServer.addMessageList(Role.DISCOVERY.getMessages());
118 private RpcServer server;
120 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 ||
156 !this.domain.equals(srv.getDomain()))
158 if (this.roles == null && srv.roles == null)
160 if (this.roles == null || srv.roles == null)
162 if (this.roles.size() == srv.roles.size()
163 && this.roles.containsAll(srv.roles))