use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class VEProviderGraphContainerTest method testContainerWithHopProvider.
@Test
public void testContainerWithHopProvider() throws Exception {
VertexHopGraphProvider vertexHopGraphProvider = new VertexHopGraphProvider(m_graphProvider);
DefaultTopologyService topologyService = new DefaultTopologyService();
SimpleMetaTopologyProvider simpleMetaTopologyProvider = new SimpleMetaTopologyProvider(vertexHopGraphProvider);
topologyService.setServiceLocator(new SimpleServiceLocator(simpleMetaTopologyProvider));
// Wrap the test GraphProvider in a VertexHopGraphProvider
VEProviderGraphContainer graphContainer = new VEProviderGraphContainer();
graphContainer.setSemanticZoomLevel(0);
graphContainer.setTopologyService(topologyService);
graphContainer.setMetaTopologyId(simpleMetaTopologyProvider.getId());
graphContainer.setSelectedNamespace(vertexHopGraphProvider.getNamespace());
m_graphContainer = graphContainer;
// There should be zero vertices or edges if no focus vertices are set
Graph graph = m_graphContainer.getGraph();
assertEquals(0, graph.getDisplayVertices().size());
assertEquals(0, graph.getDisplayEdges().size());
// Add one focus vertex
VertexHopGraphProvider.DefaultVertexHopCriteria hopCriteria = new VertexHopGraphProvider.DefaultVertexHopCriteria(new DefaultVertexRef("nodes", "v1"));
m_graphContainer.addCriteria(hopCriteria);
// This needs to be 2 because there is a SemanticZoomLevelCriteria in there also
assertEquals(2, m_graphContainer.getCriteria().length);
// Verify that a single vertex is in the graph
graph = m_graphContainer.getGraph();
assertEquals(1, graph.getDisplayVertices().size());
assertEquals(0, graph.getDisplayEdges().size());
expectVertex("nodes", "v1", "vertex");
graph.visit(verifier());
verify();
verifyConnectedness(graph);
reset();
// Change SZL to 1
m_graphContainer.setSemanticZoomLevel(1);
assertEquals(2, m_graphContainer.getCriteria().length);
// Focus vertex
expectVertex("nodes", "v1", "vertex");
expectVertex("nodes", "v2", "vertex");
/*
This is a problem with the VEProviderGraphContainer... it wraps a delegate GraphProvider
in a MergingGraphProvider like so:
VEProviderGraphContainer { MergingGraphProvider { VertexHopGraphProvider } } }
But for the VertexHopProvider to calculate the SZL correctly, it needs to be aware of all
edges, including those provided by the MergingGraphProvider. So we should rearrange things
so that they are laid out like:
VEProviderGraphContainer { VertexHopGraphProvider { MergingGraphProvider } } }
We should decouple the MergingGraphProvider from the VEProviderGraphContainer and then just
inject them in the correct order. When this problem is fixed, uncomment all of the lines that
are commented out in this test.
*/
// expectVertex("nodes", "v3", "vertex");
expectVertex("nodes", "v4", "vertex");
expectEdge("nodes", "e1", "edge");
// expectEdge("nodes", "e2", "edge");
// expectEdge("nodes", "e3", "edge");
expectEdge("nodes", "e4", "edge");
graph = m_graphContainer.getGraph();
// assertEquals(4, graph.getDisplayVertices().size());
// assertEquals(5, graph.getDisplayEdges().size());
assertEquals(3, graph.getDisplayVertices().size());
assertEquals(2, graph.getDisplayEdges().size());
graph.visit(verifier());
verify();
verifyConnectedness(graph);
reset();
// Add a collapsed criteria to the container
Criteria collapsibleCriteria = new TestCriteria1();
m_graphContainer.addCriteria(collapsibleCriteria);
assertEquals(3, m_graphContainer.getCriteria().length);
// Make sure that the TestCollapsibleCriteria is mapping "v2" and "v4" to the collapsed "test" vertex
Map<VertexRef, Set<Vertex>> collapsed = VertexHopGraphProvider.getMapOfVerticesToCollapsedVertices(VertexHopGraphProvider.getCollapsedCriteria(m_graphContainer.getCriteria()));
assertTrue(collapsed.containsKey(new DefaultVertexRef("nodes", "v2")));
assertTrue(collapsed.containsKey(new DefaultVertexRef("nodes", "v4")));
assertTrue(collapsed.get(new DefaultVertexRef("nodes", "v2")).equals(Collections.singleton(new DefaultVertexRef("nodes", "test"))));
assertTrue(collapsed.get(new DefaultVertexRef("nodes", "v4")).equals(Collections.singleton(new DefaultVertexRef("nodes", "test"))));
assertEquals(m_graphContainer.getGraph().getDisplayVertices().toString(), 3, m_graphContainer.getGraph().getDisplayVertices().size());
assertEquals(m_graphContainer.getTopologyServiceClient().getGraphProviderBy("nodes").getVertices(new TestCriteria1()).toString(), 3, m_graphContainer.getTopologyServiceClient().getGraphProviderBy("nodes").getVertices(new TestCriteria1()).size());
expectVertex("nodes", "v1", "vertex");
expectVertex("nodes", "v3", "vertex");
// Collapsed vertex that contains v2 and v4
expectVertex("nodes", "test", "test");
expectEdge("nodes", "collapsedTarget-e1", "edge");
expectEdge("nodes", "collapsedSource-e2", "edge");
expectEdge("nodes", "collapsedTarget-e3", "edge");
expectEdge("nodes", "collapsedSource-e4", "edge");
graph = m_graphContainer.getGraph();
assertEquals(3, graph.getDisplayVertices().size());
assertEquals(4, graph.getDisplayEdges().size());
for (Edge edge : graph.getDisplayEdges()) {
if (edge.getId().equals("collapsedTarget-e1")) {
assertEquals("v1", edge.getSource().getVertex().getId());
assertEquals("test", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedSource-e2")) {
assertEquals("test", edge.getSource().getVertex().getId());
assertEquals("v3", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedTarget-e3")) {
assertEquals("v3", edge.getSource().getVertex().getId());
assertEquals("test", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedSource-e4")) {
assertEquals("test", edge.getSource().getVertex().getId());
assertEquals("v1", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("ncs1")) {
assertEquals("v1", edge.getSource().getVertex().getId());
assertEquals("v3", edge.getTarget().getVertex().getId());
} else {
fail("Unknown edge ID: " + edge.getId());
}
}
graph.visit(verifier());
verify();
verifyConnectedness(graph);
reset();
// Remove the collapsed criteria and make sure that the state reverts correctly
m_graphContainer.removeCriteria(collapsibleCriteria);
graph = m_graphContainer.getGraph();
// assertEquals(4, graph.getDisplayVertices().size());
// assertEquals(5, graph.getDisplayEdges().size());
assertEquals(3, graph.getDisplayVertices().size());
assertEquals(2, graph.getDisplayEdges().size());
collapsibleCriteria = new TestCriteria1();
m_graphContainer.addCriteria(collapsibleCriteria);
collapsibleCriteria = new TestCriteria2();
m_graphContainer.addCriteria(collapsibleCriteria);
assertEquals(4, m_graphContainer.getCriteria().length);
graph = m_graphContainer.getGraph();
assertEquals(3, m_graphContainer.getGraph().getDisplayVertices().size());
/*
* One edge is missing because of the VertexHopGraphProvider issue mentioned above.
assertEquals(
ArrayUtils.toString(m_graphContainer.getGraph().getDisplayEdges()),
5,
m_graphContainer.getGraph().getDisplayEdges().size()
);
*/
assertEquals(4, m_graphContainer.getGraph().getDisplayEdges().size());
for (Edge edge : graph.getDisplayEdges()) {
if (edge.getId().equals("collapsedTarget-e1")) {
assertEquals("v1", edge.getSource().getVertex().getId());
assertEquals("test", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsed-e2")) {
assertEquals("test", edge.getSource().getVertex().getId());
assertEquals("collapse-v3", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsed-e3")) {
assertEquals("collapse-v3", edge.getSource().getVertex().getId());
assertEquals("test", edge.getTarget().getVertex().getId());
} else if (edge.getId().equals("collapsedSource-e4")) {
assertEquals("test", edge.getSource().getVertex().getId());
assertEquals("v1", edge.getTarget().getVertex().getId());
/**
* This edge is not found because of the issue mentioned above.
* } else if (edge.getId().equals("collapsedTarget-ncs1")) {
* assertEquals("v1", edge.getSource().getVertex().getId());
* assertEquals("collapse-v3", edge.getTarget().getVertex().getId());
*/
} else {
fail("Unknown edge ID: " + edge.getId());
}
}
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class NCSEdgeProvider method getEdges.
/**
* This factory works by using {@link NCSServiceCriteria} to construct edges based on
* connecting all of the ServiceElements that make up a Service to each other.
*
* @param criteria An {@link NCSServiceCriteria} object
*/
@Override
public List<Edge> getEdges(Criteria... criteria) {
List<Edge> retval = new ArrayList<>();
for (Criteria criterium : criteria) {
try {
NCSServiceCriteria crit = (NCSServiceCriteria) criterium;
for (Long id : crit) {
NCSComponent service = m_dao.get(id);
if (service == null) {
LoggerFactory.getLogger(this.getClass()).warn("NCSComponent not found for ID {}", id);
} else {
// Check foreignsource of the subcomponents to make sure it matches the Service's foreignsource
NCSComponent[] subs = checkForeignSource(service.getForeignSource(), service.getSubcomponents());
// Connect all of the ServiceElements to one another
for (int i = 0; i < subs.length; i++) {
for (int j = i + 1; j < subs.length; j++) {
String foreignSource = null, foreignId = null;
OnmsNode sourceNode = null, targetNode = null;
NodeIdentification ident = subs[i].getNodeIdentification();
String sourceLabel = subs[i].getName();
if (ident != null) {
foreignSource = ident.getForeignSource();
foreignId = ident.getForeignId();
sourceNode = m_nodeDao.findByForeignId(foreignSource, foreignId);
if (sourceNode == null) {
continue;
}
if (sourceLabel == null) {
sourceLabel = sourceNode.getLabel();
}
}
ident = subs[j].getNodeIdentification();
String targetLabel = subs[j].getName();
if (ident != null) {
foreignSource = ident.getForeignSource();
foreignId = ident.getForeignId();
targetNode = m_nodeDao.findByForeignId(foreignSource, foreignId);
if (targetNode == null) {
continue;
}
if (targetLabel == null) {
targetLabel = targetNode.getLabel();
}
}
String sourceElementName = subs[i].getForeignSource() + "::" + subs[i].getForeignId();
String targetElementName = subs[j].getForeignSource() + "::" + subs[j].getForeignId();
retval.add(new NCSEdge(subs[i].getForeignId(), service.getName(), sourceElementName, targetElementName, new NCSVertex(String.valueOf(sourceNode.getId()), sourceLabel), new NCSVertex(String.valueOf(targetNode.getId()), targetLabel)));
}
}
}
}
} catch (ClassCastException e) {
}
}
return retval;
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class NCSEdgeStatusProvider method getNCSImpactedAlarms.
private Set<String> getNCSImpactedAlarms() {
org.opennms.core.criteria.Criteria criteria = new org.opennms.core.criteria.Criteria(OnmsAlarm.class);
criteria.addRestriction(new EqRestriction("uei", "uei.opennms.org/internal/ncs/componentImpacted"));
List<OnmsAlarm> alarms = getAlarmDao().findMatching(criteria);
Set<String> alarmsSet = new HashSet<>();
for (OnmsAlarm alarm : alarms) {
final Optional<String> foreignSource = alarm.findEventParameter("foreignSource").map(OnmsEventParameter::getValue);
final Optional<String> foreignId = alarm.findEventParameter("foreignId").map(OnmsEventParameter::getValue);
if (foreignSource.isPresent() && foreignId.isPresent()) {
alarmsSet.add(foreignSource.get() + "::" + foreignId.get());
}
}
return alarmsSet;
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class NCSSearchProvider method onFocusSearchResult.
@Override
public void onFocusSearchResult(SearchResult searchResult, OperationContext operationContext) {
Criteria criteria = NCSEdgeProvider.createCriteria(Collections.singletonList(Long.parseLong(searchResult.getId())));
if (m_serviceManager.isCriteriaRegistered(NAMESPACE, operationContext.getGraphContainer().getSessionId())) {
m_serviceManager.unregisterCriteria(NAMESPACE, operationContext.getGraphContainer().getSessionId());
}
m_serviceManager.registerCriteria(criteria, operationContext.getGraphContainer().getSessionId());
// Juniper specifically asked for the selection of NCS services
selectVerticesForEdge(m_ncsEdgeProvider, criteria, operationContext.getGraphContainer().getSelectionManager());
}
use of org.opennms.features.topology.api.topo.Criteria in project opennms by OpenNMS.
the class ShowNCSPathOperation method execute.
@Override
public void execute(List<VertexRef> targets, final OperationContext operationContext) {
// Get the current NCS criteria from here you can get the foreignIds foreignSource and deviceA and Z
for (Criteria criterium : operationContext.getGraphContainer().getCriteria()) {
try {
NCSServiceCriteria ncsCriterium = (NCSServiceCriteria) criterium;
if (ncsCriterium.getServiceCount() > 0) {
m_storedCriteria = ncsCriterium;
break;
}
} catch (ClassCastException e) {
}
}
final VertexRef defaultVertRef = targets.get(0);
final SelectionManager selectionManager = operationContext.getGraphContainer().getSelectionManager();
// selectionManager.getSelectedVertexRefs();
final Collection<VertexRef> vertexRefs = getVertexRefsForNCSService(m_storedCriteria);
final UI mainWindow = operationContext.getMainWindow();
final Window ncsPathPrompt = new Window("Show NCS Path");
ncsPathPrompt.setModal(true);
ncsPathPrompt.setResizable(false);
ncsPathPrompt.setWidth("300px");
ncsPathPrompt.setHeight("220px");
// Items used in form field
final PropertysetItem item = new PropertysetItem();
item.addItemProperty("Device A", new ObjectProperty<String>("", String.class));
item.addItemProperty("Device Z", new ObjectProperty<String>("", String.class));
FormFieldFactory fieldFactory = new FormFieldFactory() {
private static final long serialVersionUID = 1L;
@Override
public Field<?> createField(Item item, Object propertyId, Component uiContext) {
String pid = (String) propertyId;
ComboBox select = new ComboBox();
for (VertexRef vertRef : vertexRefs) {
select.addItem(vertRef.getId());
select.setItemCaption(vertRef.getId(), vertRef.getLabel());
}
select.setNewItemsAllowed(false);
select.setNullSelectionAllowed(false);
select.setImmediate(true);
select.setScrollToSelectedItem(true);
if ("Device A".equals(pid)) {
select.setCaption("Device A");
} else {
select.setCaption("Device Z");
}
return select;
}
};
final Form promptForm = new Form() {
@Override
public void commit() {
String deviceA = (String) getField("Device A").getValue();
String deviceZ = (String) getField("Device Z").getValue();
if (deviceA.equals(deviceZ)) {
Notification.show("Device A and Device Z cannot be the same", Notification.Type.WARNING_MESSAGE);
throw new Validator.InvalidValueException("Device A and Device Z cannot be the same");
}
OnmsNode nodeA = m_nodeDao.get(Integer.valueOf(deviceA));
String deviceANodeForeignId = nodeA.getForeignId();
// Use nodeA's foreignSource, deviceZ should have the same foreignSource. It's an assumption
// which might need to changed in the future. Didn't want to hard code it it "space" if they
// change it in the future
String nodeForeignSource = nodeA.getForeignSource();
String deviceZNodeForeignId = m_nodeDao.get(Integer.valueOf(deviceZ)).getForeignId();
NCSComponent ncsComponent = m_dao.get(m_storedCriteria.getServiceIds().get(0));
String foreignSource = ncsComponent.getForeignSource();
String foreignId = ncsComponent.getForeignId();
String serviceName = ncsComponent.getName();
try {
NCSServicePath path = getNcsPathProvider().getPath(foreignId, foreignSource, deviceANodeForeignId, deviceZNodeForeignId, nodeForeignSource, serviceName);
if (path.getStatusCode() == 200) {
NCSServicePathCriteria criteria = new NCSServicePathCriteria(path.getEdges());
m_serviceManager.registerCriteria(criteria, operationContext.getGraphContainer().getSessionId());
// Select only the vertices in the path
selectionManager.setSelectedVertexRefs(path.getVertices());
} else {
LoggerFactory.getLogger(this.getClass()).warn("An error occured while retrieving the NCS Path, Juniper NetworkAppsApi send error code: " + path.getStatusCode());
mainWindow.showNotification("An error occurred while retrieving the NCS Path\nStatus Code: " + path.getStatusCode(), Notification.TYPE_ERROR_MESSAGE);
}
} catch (Exception e) {
if (e.getCause() instanceof ConnectException) {
LoggerFactory.getLogger(this.getClass()).warn("Connection Exception Occurred while retreiving path {}", e);
Notification.show("Connection Refused when attempting to reach the NetworkAppsApi", Notification.Type.TRAY_NOTIFICATION);
} else if (e.getCause() instanceof HttpOperationFailedException) {
HttpOperationFailedException httpException = (HttpOperationFailedException) e.getCause();
if (httpException.getStatusCode() == 401) {
LoggerFactory.getLogger(this.getClass()).warn("Authentication error when connecting to NetworkAppsApi {}", httpException);
Notification.show("Authentication error when connecting to NetworkAppsApi, please check the username and password", Notification.Type.TRAY_NOTIFICATION);
} else {
LoggerFactory.getLogger(this.getClass()).warn("An error occured while retrieving the NCS Path {}", httpException);
Notification.show("An error occurred while retrieving the NCS Path\n" + httpException.getMessage(), Notification.Type.TRAY_NOTIFICATION);
}
} else if (e.getCause() instanceof Validator.InvalidValueException) {
Notification.show(e.getMessage(), Notification.Type.ERROR_MESSAGE);
} else {
LoggerFactory.getLogger(this.getClass()).warn("Exception Occurred while retreiving path {}", e);
Notification.show("An error occurred while calculating the path please check the karaf.log file for the exception: \n" + e.getMessage(), Notification.Type.TRAY_NOTIFICATION);
}
}
}
};
promptForm.setBuffered(true);
promptForm.setFormFieldFactory(fieldFactory);
promptForm.setItemDataSource(item);
Button ok = new Button("OK");
ok.addClickListener(new ClickListener() {
private static final long serialVersionUID = -2742886456007926688L;
@Override
public void buttonClick(ClickEvent event) {
promptForm.commit();
mainWindow.removeWindow(ncsPathPrompt);
}
});
promptForm.getFooter().addComponent(ok);
Button cancel = new Button("Cancel");
cancel.addClickListener(new ClickListener() {
private static final long serialVersionUID = -9026067481179449095L;
@Override
public void buttonClick(ClickEvent event) {
mainWindow.removeWindow(ncsPathPrompt);
}
});
promptForm.getFooter().addComponent(cancel);
ncsPathPrompt.setContent(promptForm);
mainWindow.addWindow(ncsPathPrompt);
promptForm.getField("Device A").setValue(defaultVertRef.getId());
}
Aggregations