// add custom validation form module attributes here.
}
+ @SuppressWarnings("resource")
@Override
public java.lang.AutoCloseable createInstance() {
final OpflexConnectionService connectionService = new OpflexConnectionService();
int ch = b.getByte(b.readerIndex()) & 0xFF;
if (!(ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) {
return;
- } else {
- b.readByte(); //move the read index
}
+ b.readByte(); //move the read index
}
}
private static class CallContext {
private String method;
- private RpcMessage request;
private SettableFuture<Object> future;
- public CallContext(RpcMessage request, String method, SettableFuture<Object> future) {
+ public CallContext(String method, SettableFuture<Object> future) {
this.method = method;
- this.request = request;
this.future = future;
}
return method;
}
- public RpcMessage getRequest() {
- return request;
- }
-
public SettableFuture<Object> getFuture() {
return future;
}
logger.trace("invoke: {}", s);
SettableFuture<Object> sf = SettableFuture.create();
- methodContext.put(message.getId(), new CallContext(message, message.getName(), sf));
+ methodContext.put(message.getId(), new CallContext(message.getName(), sf));
nettyChannel.writeAndFlush(s);
*/
public void processResult(JsonNode response) throws NoSuchMethodException {
- logger.trace("Response : {}", response.toString());
+ logger.warn("Response : {}", response.toString());
CallContext returnCtxt = methodContext.get(response.get("id").asText());
if (returnCtxt == null) return;
RpcMessage message = messageMap.get(returnCtxt.getMethod());
return this.channel;
}
- private void handleNewConnection(String identifier, Channel channel)
+ void handleNewConnection(String identifier, Channel newChannel)
throws InterruptedException, ExecutionException {
ObjectMapper objectMapper = new ObjectMapper();
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonRpcEndpoint endpoint = new JsonRpcEndpoint(identifier, connectionService,
- objectMapper, channel, messageMap, broker);
+ objectMapper, newChannel, messageMap, broker);
endpoint.setContext(context);
JsonRpcServiceBinderHandler binderHandler =
new JsonRpcServiceBinderHandler(endpoint);
- channel.pipeline().addLast(binderHandler);
+ newChannel.pipeline().addLast(binderHandler);
connectionService.addConnection(endpoint);
- ChannelFuture closeFuture = channel.closeFuture();
+ ChannelFuture closeFuture = newChannel.closeFuture();
closeFuture.addListener(endpoint);
}
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
- public void initChannel(SocketChannel channel)
+ public void initChannel(SocketChannel ch)
throws Exception {
logger.debug("New Passive channel created : "
- + channel.toString());
- InetAddress address = channel.remoteAddress()
+ + ch.toString());
+ InetAddress address = ch.remoteAddress()
.getAddress();
- int port = channel.remoteAddress().getPort();
+ int port = ch.remoteAddress().getPort();
String identifier = address.getHostAddress() + ":"
+ port;
- channel.pipeline().addLast(
+ ch.pipeline().addLast(
new LoggingHandler(LogLevel.INFO),
new JsonRpcDecoder(100000),
new StringEncoder(CharsetUtil.UTF_8));
- handleNewConnection(identifier, channel);
+ handleNewConnection(identifier, ch);
logger.warn("Connected Node : " + identifier);
}
});
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.groupbasedpolicy.jsonrpc.ConnectionService;
import org.opendaylight.groupbasedpolicy.renderer.opflex.messages.IdentityRequest;
import org.opendaylight.groupbasedpolicy.renderer.opflex.messages.IdentityResponse;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.Domains;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.DomainsBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.domains.Domain;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.domains.DomainKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.domains.domain.DiscoveryDefinitions;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.domains.domain.discovery.definitions.EndpointRegistry;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.domains.domain.discovery.definitions.Observer;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.domains.domain.discovery.definitions.PolicyRepository;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public static final InstanceIdentifier<Domains> DOMAINS_IID =
InstanceIdentifier.builder(Domains.class).build();
- public InstanceIdentifier<Domain> domainIid(DomainKey domainKey) {
- return InstanceIdentifier.builder(Domains.class).child(Domain.class, domainKey)
- .build();
- }
-
public OpflexConnectionService() {
int numCPU = Runtime.getRuntime().availableProcessors();
executor = Executors.newScheduledThreadPool(numCPU * 2);
start();
}
- private List<OpflexRpcServer> setDefaultIdentities(OpflexDomain domain) {
+ private List<OpflexRpcServer> setDefaultIdentities(OpflexDomain od) {
/*
* Create a single server, filling all roles
roles.add(Role.ENDPOINT_REGISTRY);
roles.add(Role.OBSERVER);
- OpflexDomain od = new OpflexDomain();
- od.setDomain(OPFLEX_DOMAIN);
OpflexRpcServer srv = new OpflexRpcServer(od, identity, roles);
srv.setConnectionService(this);
srv.setRpcBroker(this);
return null;
}
- private void initializeConfig() {
- // XXX - This is a hack to avoid a bug in the data broker
- // API where you have to write all the parents before you can write
- // a child
- WriteTransaction t = dataProvider.newWriteOnlyTransaction();
- t.put(LogicalDatastoreType.CONFIGURATION, DOMAINS_IID, new DomainsBuilder().build());
- ListenableFuture<RpcResult<TransactionStatus>> f = t.commit();
- Futures.addCallback(f, new FutureCallback<RpcResult<TransactionStatus>>() {
-
- @Override
- public void onSuccess(RpcResult<TransactionStatus> result) {
-
- }
-
- @Override
- public void onFailure(Throwable t) {
- logger.error("Could not write domain base container", t);
- }
- });
- }
-
private void initializeServers() {
OpflexDomain od;
- //initializeConfig();
readConfig();
/*
* Get the configured identities, if any. If lists are empty,
}
else {
// TODO: should also write into config store?
- logger.warn("Setting default identities");
+ logger.info("Setting default identities");
od = new OpflexDomain();
od.setDomain(OPFLEX_DOMAIN);
od.addServers(setDefaultIdentities(od));
}
+ /**
+ * Start the {@link OpflexConnectionService}
+ */
public void start() {
opflexDomains = new ConcurrentHashMap<String, OpflexDomain>();
brokerMap = new ConcurrentHashMap<String, List<RpcCallback>>();
private void deleteDomain(String domain) {
-
OpflexDomain od = opflexDomains.remove(domain);
if (od != null) {
od.cleanup();
}
}
- /**
- * Close the connection service. Implemented from the
- * AutoCloseable interface.
- */
- @Override
- public void close() throws ExecutionException, InterruptedException {
-
- executor.shutdownNow();
-
- if (dataProvider != null) {
- WriteTransaction t = dataProvider.newWriteOnlyTransaction();
- t.delete(LogicalDatastoreType.CONFIGURATION, DOMAINS_IID);
- t.commit().get();
- }
- }
-
private void readConfig() {
ListenableFuture<Optional<Domains>> dao =
dataProvider.newReadOnlyTransaction()
@Override
public void onSuccess(final Optional<Domains> result) {
if (!result.isPresent()) {
- logger.warn("No result!!!");
return;
}
getNewConfig(result);
}, executor);
}
- private void getNewConfig(final Optional<Domains> result) {
+ void getNewConfig(final Optional<Domains> result) {
List<String> currentDomains = new ArrayList<String>(opflexDomains.keySet());
List<String> newDomains = new ArrayList<String>();
- List<String> addList = new ArrayList<String>();
- List <String> dropList = new ArrayList<String>();
- List <String> updateList = new ArrayList<String>();
- if (result.get() instanceof Domains) {
- /*
- * Get the new list of domains from the
- * configuration store, and convert to a
- * list of the actual domain names for list
- * manipulation
- */
- Domains domains = (Domains)result.get();
+ /*
+ * Get the new list of domains from the
+ * configuration store, and convert to a
+ * list of the actual domain names for list
+ * manipulation
+ */
+ Domains domains = result.get();
- domainList = domains.getDomain();
- for (Domain domainObj : domainList) {
- newDomains.add(domainObj.getId());
- }
+ domainList = domains.getDomain();
+ for (Domain domainObj : domainList) {
+ newDomains.add(domainObj.getId());
+ }
- logger.warn("Current domains {}", currentDomains);
- /*
- * Find out what's changed at the domain level.
- * Classify as additions, deletions, and updates
- */
- addList = new ArrayList<String>(newDomains);
- dropList = new ArrayList<String>(currentDomains);
- updateList = new ArrayList<String>(newDomains);
- addList.removeAll(currentDomains);
- dropList.removeAll(newDomains);
- updateList.removeAll(addList);
-
- /*
- * Drop domains that were removed, along with all
- * of their servers and connections
- */
- for (String d : dropList) {
- deleteDomain(d);
- }
+ /*
+ * Find out what's changed at the domain level.
+ * Classify as additions, deletions, and updates
+ */
+ List<String> addList = new ArrayList<String>(newDomains);
+ List <String> dropList = new ArrayList<String>(currentDomains);
+ List <String> updateList = new ArrayList<String>(newDomains);
+ addList.removeAll(currentDomains);
+ dropList.removeAll(newDomains);
+ updateList.removeAll(addList);
+
+ /*
+ * Drop domains that were removed, along with all
+ * of their servers and connections
+ */
+ for (String d : dropList) {
+ deleteDomain(d);
+ }
- /*
- * These are entirely new domains -- get the
- * information for each new domain and configure
- */
- for (String d : addList) {
- OpflexDomain od = new OpflexDomain();
- od.setDomain(d);
- opflexDomains.put(od.getDomain(), od );
-
- /* Spawn the servers for this domain */
- for (Domain dl : domainList) {
- if (dl.getId().equals(d)) {
- od.addServers(createServerList(od, dl));
- break;
- }
+ /*
+ * These are entirely new domains -- get the
+ * information for each new domain and configure
+ */
+ for (String d : addList) {
+ OpflexDomain od = new OpflexDomain();
+ od.setDomain(d);
+ opflexDomains.put(od.getDomain(), od );
+
+ /* Spawn the servers for this domain */
+ for (Domain dl : domainList) {
+ if (dl.getId().equals(d)) {
+ od.addServers(createServerList(od, dl));
+ break;
}
}
+ }
- /*
- * These are domains with updates
- */
- for (String d : updateList) {
- OpflexDomain od = opflexDomains.get(d);
- for (Domain domainObj : domainList) {
- if (domainObj.getId().equals(d)) {
- logger.warn("updateServers");
- od.updateServers(createServerList(od, domainObj));
- break;
- }
+ /*
+ * These are domains with updates
+ */
+ for (String d : updateList) {
+ OpflexDomain od = opflexDomains.get(d);
+ for (Domain domainObj : domainList) {
+ if (domainObj.getId().equals(d)) {
+ od.updateServers(createServerList(od, domainObj));
+ break;
}
}
}
}
- @Override
+ @Override
public void onDataChanged( final AsyncDataChangeEvent<InstanceIdentifier<?>,
DataObject>change) {
readConfig();
}
+
+ /**
+ * Close the connection service. Implemented from the
+ * AutoCloseable interface.
+ */
+ @Override
+ public void close() throws ExecutionException, InterruptedException {
+
+ executor.shutdownNow();
+
+ if (dataProvider != null) {
+ WriteTransaction t = dataProvider.newWriteOnlyTransaction();
+ t.delete(LogicalDatastoreType.CONFIGURATION, DOMAINS_IID);
+ t.commit().get();
+ }
+ }
+
@Override
public void subscribe(RpcMessage message, RpcCallback callback) {
}
}
+ /**
+ * This notification handles the OpFlex Identity request messages.
+ *
+ * TODO: implement Identity Response messages
+ */
@Override
public void callback(JsonRpcEndpoint endpoint, RpcMessage message) {
}
}
+ /**
+ * This is the notification when a new endpoint
+ * has been created. Since the endpoint is new,
+ * we don't have a OpflexConnection for it yet. We
+ * create the OpflexConnection, then retrieve the
+ * OpflexRpcServer that created this connections
+ * to inherit some of the fields we need (domain, server).
+ */
@Override
public void addConnection(JsonRpcEndpoint endpoint) {
/*
- * When the connection is added, we don't have a context
- * other than the JsonRpcEndpoint. We use the context
- * field to store the server object that created this
+ * When the connection is added, we only have the
+ * JsonRpcEndpoint. We use the JsonRpcEndpoint's
+ * context field to store the server object that created this
* connection, and can look up things like the domain,
* etc. to create the containing connection object.
*/
OpflexRpcServer server = (OpflexRpcServer)endpoint.getContext();
OpflexDomain domain = server.getDomain();
-
- /*
- * This is the notification when a new endpoint
- * has been created. Since the endpoint is new,
- * we don't have a OpflexConnection for it yet. We
- * create the OpflexConnection, then get the
- * OpflexRpcServer to set some of the fields
- * we need (domain, server).
- */
- OpflexAgent oc = new OpflexAgent();
- oc.setEndpoint(endpoint);
- oc.setIdentity(endpoint.getIdentifier());
- oc.setDomain(domain.getDomain());
- oc.setOpflexServer(server);
- oc.setRoles(server.getRoles());
-
/*
- * The OpFlex domain is determined by the server socket
+ * The OpFlex domain is the same as the server
* that the agent connected to. Look up the OpFlex RPC
* server using the server socket.
*
* condition). Treat that as a failure, closing the
* connection.
*/
- logger.warn("Adding agent {}", endpoint.getIdentifier());
+ OpflexAgent oc = new OpflexAgent();
+ oc.setEndpoint(endpoint);
+ oc.setIdentity(endpoint.getIdentifier());
+ oc.setDomain(domain.getDomain());
+ oc.setOpflexServer(server);
+ oc.setRoles(server.getRoles());
+
+ logger.info("Adding agent {}", endpoint.getIdentifier());
domain.addOpflexAgent(oc);
}
+ /**
+ * This is the notification we receive when a connection
+ * is closed. Retrieve the domain from the {@link JsonRpcEndpoint}'s
+ * context field to get the {@link OpflexRpcServer}, which contains
+ * the OpFlex domain for this connection, then use the identity from
+ * the {@link JsonRpcEndpoint} and domain to remove the {@link OpflexAgent}
+ * from the domain
+ */
@Override
public void channelClosed(JsonRpcEndpoint endpoint) throws Exception {
logger.info("Connection to Node : {} closed", endpoint.getIdentifier());
private String address;
private int port;
- private void parseAndSetIdentity(String identity) {
- if (identity.split(":").length == 2) {
- this.identity = identity;
- this.address = identity.split(":")[0];
- this.port = Integer.parseInt(identity.split(":")[1]);
+ private void parseAndSetIdentity(String id) {
+ if (id.split(":").length == 2) {
+ this.identity = id;
+ this.address = id.split(":")[0];
+ this.port = Integer.parseInt(id.split(":")[1]);
}
}
new Thread() {
private RpcServer server;
- public Thread initializeServerParams(RpcServer server) {
- this.server = server;
+ public Thread initializeServerParams(RpcServer srv) {
+ this.server = srv;
return this;
}
@Override
--- /dev/null
+/*
+ * Copyright (C) 2014 Cisco Systems, Inc.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Authors : Thomas Bachman
+ */
+package org.opendaylight.groupbasedpolicy.renderer.opflex.messages;
+
+import java.util.List;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+@JsonSerialize
+@JsonDeserialize
+public class ManagedObject {
+
+ public static class Properties {
+ private String name;
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getData() {
+ return data;
+ }
+ public void setData(String data) {
+ this.data = data;
+ }
+ private String data;
+ }
+
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public List<Properties> getProperties() {
+ return properties;
+ }
+ public void setProperties(List<Properties> properties) {
+ this.properties = properties;
+ }
+ public List<ManagedObject> getChildren() {
+ return children;
+ }
+ public void setChildren(List<ManagedObject> children) {
+ this.children = children;
+ }
+ public List<ManagedObject> getStatistics() {
+ return statistics;
+ }
+ public void setStatistics(List<ManagedObject> statistics) {
+ this.statistics = statistics;
+ }
+ public List<ManagedObject> getFrom_relations() {
+ return from_relations;
+ }
+ public void setFrom_relations(List<ManagedObject> from_relations) {
+ this.from_relations = from_relations;
+ }
+ public List<ManagedObject> getTo_relations() {
+ return to_relations;
+ }
+ public void setTo_relations(List<ManagedObject> to_relations) {
+ this.to_relations = to_relations;
+ }
+ public List<ManagedObject> getFaults() {
+ return faults;
+ }
+ public void setFaults(List<ManagedObject> faults) {
+ this.faults = faults;
+ }
+ public List<ManagedObject> getHealth() {
+ return health;
+ }
+ public void setHealth(List<ManagedObject> health) {
+ this.health = health;
+ }
+ private String name;
+ private List<Properties> properties;
+ private List<ManagedObject> children;
+ private List<ManagedObject> statistics;
+ private List<ManagedObject> from_relations;
+ private List<ManagedObject> to_relations;
+ private List<ManagedObject> faults;
+ private List<ManagedObject> health;
+
+}
public static final String POLICY_MESSAGE_RESPONSE = "resolve_policy_response";
static public class Result {
- private List<String> policy; // TODO: replace with MOs
+ private List<ManagedObject> policy;
private int prr;
- public List<String> getPolicy() {
+ public List<ManagedObject> getPolicy() {
return policy;
}
- public void setPolicy(List<String> policy) {
+ public void setPolicy(List<ManagedObject> policy) {
this.policy = policy;
}
public int getPrr() {
static public class Params {
private String context;
- private List<String> subtree;
+ private List<ManagedObject> subtree;
private int prr;
public String getContext() {
return context;
public void setContext(String context) {
this.context = context;
}
- public List<String> getSubtree() {
+ public List<ManagedObject> getSubtree() {
return subtree;
}
- public void setSubtree(List<String> subtree) {
+ public void setSubtree(List<ManagedObject> subtree) {
this.subtree = subtree;
}
public int getPrr() {
static public class Params {
private String subject;
private String context;
- private String object; // TODO: change to MOs
- private List<String> fault;
- private List<String> event;
- private List<String> statistics;
- private List<String> health;
+ private ManagedObject object;
+ private List<ManagedObject> fault;
+ private List<ManagedObject> event;
+ private List<ManagedObject> statistics;
+ private List<ManagedObject> health;
public String getSubject() {
return subject;
}
public void setContext(String context) {
this.context = context;
}
- public String getObject() {
+ public ManagedObject getObject() {
return object;
}
- public void setObject(String object) {
+ public void setObject(ManagedObject object) {
this.object = object;
}
- public List<String> getFault() {
+ public List<ManagedObject> getFault() {
return fault;
}
- public void setFault(List<String> fault) {
+ public void setFault(List<ManagedObject> fault) {
this.fault = fault;
}
- public List<String> getEvent() {
+ public List<ManagedObject> getEvent() {
return event;
}
- public void setEvent(List<String> event) {
+ public void setEvent(List<ManagedObject> event) {
this.event = event;
}
- public List<String> getStatistics() {
+ public List<ManagedObject> getStatistics() {
return statistics;
}
- public void setStatistics(List<String> statistics) {
+ public void setStatistics(List<ManagedObject> statistics) {
this.statistics = statistics;
}
- public List<String> getHealth() {
+ public List<ManagedObject> getHealth() {
return health;
}
- public void setHealth(List<String> health) {
+ public void setHealth(List<ManagedObject> health) {
this.health = health;
}
}
}
@Override
- public void publish(JsonRpcEndpoint endpoint, RpcMessage message) {
+ public void publish(JsonRpcEndpoint ep, RpcMessage message) {
testTriggerFlag = true;
- callback(endpoint, message);
+ callback(ep, message);
}
@JsonDeserialize
}
@Override
- public void callback(JsonRpcEndpoint endpoint, RpcMessage message) {
+ public void callback(JsonRpcEndpoint ep, RpcMessage message) {
if (message != null && message instanceof JsonRpcEndpointTest.OpflexTest) {
JsonRpcEndpointTest.OpflexTest msg = (JsonRpcEndpointTest.OpflexTest)message;
if ( msg.getParams() == null) {
return;
}
- List<String> roles = msg.params.get(0).getMy_role();
}
}
private static final String TEST_IP = "127.0.0.1";
private static final int TEST_PORT = 53670;
- private static boolean newConnection = false;
- private static boolean serverClosed = false;
+
@Override
public void addConnection(JsonRpcEndpoint endpoint) {
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil;
+import java.io.IOException;
+import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
+import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
static private final String TEST_EP_UUID = "85d53c32-47af-4eaf-82fd-ced653ff74da";
static public final String TEST_IP = "127.0.0.1";
- static public final String TEST_PORT = "57563";
static private final String ID_UUID = "2da9e3d7-0bbe-4099-b343-12783777452f";
static private final String SEND_IDENTITY = "send_identity";
private Optional<Domains> mockDao;
@Mock
private Domains mockDomains;
- @Mock
- private Domain mockDomain;
+ @Mock Domain mockDomain;
@Mock
private OpflexDomain mockOpflexDomain;
@Mock
private OpflexRpcServer mockOpflexServer;
@Mock
private OpflexAgent mockAgent;
+
+ private ServerSocket create(int[] ports) throws IOException {
+ for (int port : ports) {
+ try {
+ return new ServerSocket(port);
+ } catch (IOException ex) {
+ continue; // try next port
+ }
+ }
+
+ // if the program gets here, no port in the range was found
+ throw new IOException("no free port found");
+ }
+
+ private int getAvailableServerPort() {
+ try {
+ int freePort;
+ ServerSocket s = create(new int[]
+ { 6670, 6671, 6672, 6673, 6674, 6675, 6676, 6677, 6678 });
+ freePort = s.getLocalPort();
+ s.close();
+ return freePort;
+ } catch (IOException ex) {
+ return 0;
+ }
+ }
@Before
public void setUp() throws Exception {
when(mockOption.get()).thenReturn(mockDao);
when(mockDao.get()).thenReturn(mockDomains);
when(mockDomains.getDomain())
- .thenReturn(new ArrayList<Domain>() {{ add(mockDomain); }});
+ .thenReturn(new ArrayList<Domain>() {
+ private static final long serialVersionUID = 6302503537798173568L;
+ { add(mockDomain); }});
+
when(mockDomain.getDiscoveryDefinitions()).thenReturn(dummyDefinitions);
/*
prBuilder = new PolicyRepositoryBuilder();
oBuilder = new ObserverBuilder();
-
- // TODO: needs deterministic way of finding available socket
- System.setProperty(OpflexConnectionService.OPFLEX_LISTENPORT, TEST_PORT);
+ int testPort = getAvailableServerPort();
+ if ( testPort == 0) {
+ assertTrue(1==0);
+ }
+ System.setProperty(OpflexConnectionService.OPFLEX_LISTENPORT, Integer.toString(testPort));
System.setProperty(OpflexConnectionService.OPFLEX_LISTENIP, TEST_IP);
}
- //@Test
+ @Test
public void testNoDefinitions() throws Exception {
opflexService = new OpflexConnectionService();
verify(mockDataBroker).newReadOnlyTransaction();
}
- //@Test
+ @Test
public void testInitialSet() throws Exception {
registries = new ArrayList<EndpointRegistry>();
repositories = new ArrayList<PolicyRepository>();
observers = new ArrayList<Observer>();
+ int serverPort = getAvailableServerPort();
EndpointRegistry epr = eprBuilder.setId(TEST_IP)
- .setPort(Integer.valueOf(TEST_PORT)).build();
+ .setPort(serverPort).build();
PolicyRepository pr = prBuilder.setId(TEST_IP)
- .setPort(Integer.valueOf(TEST_PORT)).build();
+ .setPort(serverPort).build();
Observer o = oBuilder.setId(TEST_IP)
- .setPort(Integer.valueOf(TEST_PORT)).build();
+ .setPort(serverPort).build();
registries.add(epr);
repositories.add(pr);
observers.add(o);
}
- //@Test
+ @Test
public void testAddConnection() throws Exception {
when(mockEp.getIdentifier()).thenReturn(TEST_EP_UUID);
when(mockEp.getContext()).thenReturn(mockOpflexServer);
verify(mockOpflexDomain, Mockito.times(1)).addOpflexAgent((OpflexAgent)anyObject());
}
- //@Test
+ @Test
public void testChannelClosed() throws Exception {
when(mockEp.getIdentifier()).thenReturn(TEST_EP_UUID);
when(mockEp.getContext()).thenReturn(mockOpflexServer);
verify(mockAgent).getIdentity();
}
- //@Test
+ @Test
public void testPublishSubscribeCallback() throws Exception {
List<Role> testRoles = new ArrayList<Role>();
public static final String SUBJECT = "webContract";
public static final String CONTEXT = "353786fd-7327-41dd-b7de-5d672e303730";
public static final String POLICY_NAME = "webFarmEpg";
+ public static final String PROP_NAME = "subject";
+ public static final String PROP_DATA = "http";
+ public static final String MO_NAME = "webFarmContract";
public static final String URI = "ef130684-ac17-4118-ad36-8dea0babc7b2";
public static final String DATA = "condition:notAuthorized";
public static final String PRR = "100";
" \"data\": \"" + DATA + "\"" +
" }] }";
+ private static final String emptyMo =
+ "{ \"name\": \"" + MO_NAME + "\"," +
+ " \"properties\": [ {\"name\": \"" + PROP_NAME + "\", " +
+ " \"data\": \"" + PROP_DATA + "\" }]," +
+ " \"children\": []," +
+ " \"statistics\": []," +
+ " \"from_relations\": []," +
+ " \"to_relations\": []," +
+ " \"faults\": []," +
+ " \"health\": [] }";
+
+ private static final String managedObject =
+ "{ \"name\": \"" + MO_NAME + "\", " +
+ " \"properties\": [ { \"name\": \"" + PROP_NAME + "\", " +
+ " \"data\": \"" + PROP_DATA + "\" }]," +
+ " \"children\": [ " + emptyMo + " ], " +
+ " \"statistics\": [ " + emptyMo + " ], " +
+ " \"from_relations\": [ " + emptyMo + " ], " +
+ " \"to_relations\": [ " + emptyMo + " ], " +
+ " \"faults\": [ " + emptyMo + " ], " +
+ " \"health\": [ " + emptyMo + " ]}";
+
private static final String opflexPolicyResponse =
"{ \"id\": \"" + ID_UUID + "\"," +
" \"error\": {}," +
" \"result\": {" +
- " \"policy\": [ \"" + POLICY_NAME + "\"]," +
+ " \"policy\": [ " + managedObject + "], " +
" \"prr\": \"" + PRR + "\"" +
" }}";
" \"method\": \"" + POLICY_REQUEST + "\"," +
" \"params\": [ {" +
" \"context\": \"" + CONTEXT + "\"," +
- " \"subtree\": [\"" + POLICY_NAME + "\"]," +
+ " \"subtree\": [" + managedObject + "]," +
" \"prr\": \"" + PRR + "\"" +
" }] }";
" \"result\": {} }";
private static final String STATE_REQUEST = "report_state";
- private static final String OBJECT = "ep101";
- private static final String FAULT = "ep102";
- private static final String EVENT = "infected";
- private static final String STATISTICS = "rxPackets: 20";
- private static final String HEALTH = ".98";
private static final String opflexStateRequest =
"{ \"id\": \"" + ID_UUID + "\"," +
" \"method\": \"" + STATE_REQUEST + "\"," +
" \"params\": [ {" +
" \"subject\": \"" + SUBJECT + "\"," +
" \"context\": \"" + CONTEXT + "\"," +
- " \"object\": \"" + OBJECT + "\"," +
- " \"fault\": [\"" + FAULT + "\"]," +
- " \"event\": [\"" + EVENT + "\"]," +
- " \"statistics\": [\"" + STATISTICS + "\"]," +
- " \"health\": [\"" + HEALTH + "\"]" +
+ " \"object\": " + managedObject + "," +
+ " \"fault\": [" + managedObject + "]," +
+ " \"event\": [" + managedObject + "]," +
+ " \"statistics\": [" + managedObject + "]," +
+ " \"health\": [" + managedObject + "]" +
" }] }";
PolicyResolutionResponse opflexResponse = (PolicyResolutionResponse)rpcMsg;
assertTrue(opflexResponse.getId().equals(ID_UUID));
assertTrue(opflexResponse.getResult()
- .getPolicy().get(0).equals(POLICY_NAME));
+ .getPolicy().get(0).getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getResult()
+ .getPolicy().get(0).getTo_relations().get(0).getName().equals(MO_NAME));
assertTrue(opflexResponse.getResult()
.getPrr() == Integer.parseInt(PRR));
}
assertTrue(rpcMsg instanceof PolicyUpdateRequest);
PolicyUpdateRequest opflexResponse = (PolicyUpdateRequest)rpcMsg;
assertTrue(opflexResponse.getId().equals(ID_UUID));
- assertTrue(opflexResponse.getParams()
- .get(0).getSubtree().get(0).equals(POLICY_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getSubtree().get(0).getTo_relations().get(0).getName().equals(MO_NAME));
assertTrue(opflexResponse.getParams()
.get(0).getPrr() == Integer.parseInt(PRR));
}
assertTrue(rpcMsg instanceof PolicyUpdateResponse);
PolicyUpdateResponse opflexResponse = (PolicyUpdateResponse)rpcMsg;
assertTrue(opflexResponse.getId().equals(ID_UUID));
- logger.warn("Result is {}", opflexResponse.getResult().toString());
+
}
@Test
assertTrue(opflexResponse.getParams()
.get(0).getContext().equals(CONTEXT));
assertTrue(opflexResponse.getParams()
- .get(0).getObject().equals(OBJECT));
- assertTrue(opflexResponse.getParams()
- .get(0).getFault().get(0).equals(FAULT));
- assertTrue(opflexResponse.getParams()
- .get(0).getEvent().get(0).equals(EVENT));
- assertTrue(opflexResponse.getParams()
- .get(0).getStatistics().get(0).equals(STATISTICS));
- assertTrue(opflexResponse.getParams()
- .get(0).getHealth().get(0).equals(HEALTH));
+ .get(0).getObject().getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getObject().getTo_relations().get(0).getName().equals(MO_NAME));
+
+ assertTrue(opflexResponse.getParams()
+ .get(0).getFault().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getFault().get(0).getTo_relations().get(0).getName().equals(MO_NAME));
+
+ assertTrue(opflexResponse.getParams()
+ .get(0).getEvent().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getEvent().get(0).getTo_relations().get(0).getName().equals(MO_NAME));
+
+
+ assertTrue(opflexResponse.getParams()
+ .get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getStatistics().get(0).getTo_relations().get(0).getName().equals(MO_NAME));
+
+ assertTrue(opflexResponse.getParams()
+ .get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getProperties().get(0).getName().equals(PROP_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getProperties().get(0).getData().equals(PROP_DATA));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getFaults().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getFrom_relations().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getHealth().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getStatistics().get(0).getName().equals(MO_NAME));
+ assertTrue(opflexResponse.getParams().get(0)
+ .getHealth().get(0).getTo_relations().get(0).getName().equals(MO_NAME));
}
@Test
public void testStateResponse() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
RpcMessage rpcMsg = objectMapper.
- readValue(opflexEpPolicyUpdateResponse, StateReportResponse.class);
+ readValue(opflexStateResponse, StateReportResponse.class);
assertTrue(rpcMsg instanceof StateReportResponse);
StateReportResponse opflexResponse = (StateReportResponse)rpcMsg;
assertTrue(opflexResponse.getId().equals(ID_UUID));