use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class SimpleIntManager method configDevice.
protected boolean configDevice(DeviceId deviceId) {
// Returns true if config was successful, false if not and a clean up is
// needed.
final Device device = deviceService.getDevice(deviceId);
if (device == null || !device.is(IntProgrammable.class)) {
return true;
}
if (isNotIntConfigured()) {
log.warn("Missing INT config, aborting programming of INT device {}", deviceId);
return true;
}
final boolean isEdge = !hostService.getConnectedHosts(deviceId).isEmpty();
final IntDeviceRole intDeviceRole = isEdge ? IntDeviceRole.SOURCE_SINK : IntDeviceRole.TRANSIT;
log.info("Started programming of INT device {} with role {}...", deviceId, intDeviceRole);
final IntProgrammable intProg = device.as(IntProgrammable.class);
if (!isIntStarted()) {
// Leave device with no INT configuration.
return true;
}
if (!intProg.init()) {
log.warn("Unable to init INT pipeline on {}", deviceId);
return false;
}
boolean supportSource = intProg.supportsFunctionality(IntProgrammable.IntFunctionality.SOURCE);
boolean supportSink = intProg.supportsFunctionality(IntProgrammable.IntFunctionality.SINK);
boolean supportPostcard = intProg.supportsFunctionality(IntProgrammable.IntFunctionality.POSTCARD);
if (intDeviceRole != IntDeviceRole.SOURCE_SINK && !supportPostcard) {
// Stop here, no more configuration needed for transit devices unless it support postcard.
return true;
}
if (supportSink || supportPostcard) {
if (!intProg.setupIntConfig(intConfig.get())) {
log.warn("Unable to apply INT report config on {}", deviceId);
return false;
}
}
// Port configuration.
final Set<PortNumber> hostPorts = deviceService.getPorts(deviceId).stream().map(port -> new ConnectPoint(deviceId, port.number())).filter(cp -> !hostService.getConnectedHosts(cp).isEmpty()).map(ConnectPoint::port).collect(Collectors.toSet());
for (PortNumber port : hostPorts) {
if (supportSource) {
log.info("Setting port {}/{} as INT source port...", deviceId, port);
if (!intProg.setSourcePort(port)) {
log.warn("Unable to set INT source port {} on {}", port, deviceId);
return false;
}
}
if (supportSink) {
log.info("Setting port {}/{} as INT sink port...", deviceId, port);
if (!intProg.setSinkPort(port)) {
log.warn("Unable to set INT sink port {} on {}", port, deviceId);
return false;
}
}
}
if (!supportSource && !supportPostcard) {
// it supports postcard mode.
return true;
}
// Apply intents.
// This is a trivial implementation where we simply get the
// corresponding INT objective from an intent and we apply to all
// device which support reporting.
int appliedCount = 0;
for (Versioned<IntIntent> versionedIntent : intentMap.values()) {
IntIntent intent = versionedIntent.value();
IntObjective intObjective = getIntObjective(intent);
if (intent.telemetryMode() == IntIntent.TelemetryMode.INBAND_TELEMETRY && supportSource) {
intProg.addIntObjective(intObjective);
appliedCount++;
} else if (intent.telemetryMode() == IntIntent.TelemetryMode.POSTCARD && supportPostcard) {
intProg.addIntObjective(intObjective);
appliedCount++;
} else {
log.warn("Device {} does not support intent {}.", deviceId, intent);
}
}
log.info("Completed programming of {}, applied {} INT objectives of {} total", deviceId, appliedCount, intentMap.size());
return true;
}
use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class DistributedDynamicConfigStore method deleteInner.
private void deleteInner(String spath) {
CompletableFuture<Map<String, Versioned<DataNode.Type>>> ret = keystore.getChildren(DocumentPath.from(spath));
Map<String, Versioned<DataNode.Type>> entries = null;
entries = complete(ret);
if ((entries != null) && (!entries.isEmpty())) {
entries.forEach((k, v) -> {
String[] names = k.split(ResourceIdParser.NM_CHK);
String name = names[0];
String nmSpc = ResourceIdParser.getNamespace(names[1]);
String keyVal = ResourceIdParser.getKeyVal(names[1]);
DataNode.Type type = v.value();
String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
removeLeaf(tempPath);
} else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
removeLeaf(mlpath);
} else if (type == DataNode.Type.SINGLE_INSTANCE_NODE) {
deleteInner(tempPath);
} else if (type == DataNode.Type.MULTI_INSTANCE_NODE) {
tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
deleteInner(tempPath);
} else {
throw new FailedException("Invalid node type");
}
});
}
log.trace(" keystore.removeNode({})", spath);
keystore.removeNode(DocumentPath.from(spath));
}
use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class DistributedContextEventMapTreeStore method getChildren.
@Override
public Map<String, Versioned<String>> getChildren(String path) throws WorkflowException {
DocumentPath dpath = DocumentPath.from(path);
Map<String, Versioned<String>> entries = complete(eventMapTree.getChildren(dpath));
return entries;
}
use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class DistributedContextEventMapTreeStore method asJsonTree.
@Override
public ObjectNode asJsonTree() throws WorkflowException {
DocumentPath rootPath = DocumentPath.from(Lists.newArrayList("root"));
Map<String, Versioned<String>> eventmap = complete(eventMapTree.getChildren(rootPath));
ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
for (Map.Entry<String, Versioned<String>> eventTypeEntry : eventmap.entrySet()) {
String eventType = eventTypeEntry.getKey();
ObjectNode eventTypeNode = JsonNodeFactory.instance.objectNode();
rootNode.put(eventType, eventTypeNode);
DocumentPath eventTypePath = DocumentPath.from(Lists.newArrayList("root", eventType));
Map<String, Versioned<String>> hintmap = complete(eventMapTree.getChildren(eventTypePath));
for (Map.Entry<String, Versioned<String>> hintEntry : hintmap.entrySet()) {
String hint = hintEntry.getKey();
ObjectNode hintNode = JsonNodeFactory.instance.objectNode();
eventTypeNode.put(hint, hintNode);
DocumentPath hintPath = DocumentPath.from(Lists.newArrayList("root", eventType, hint));
Map<String, Versioned<String>> contextmap = complete(eventMapTree.getChildren(hintPath));
for (Map.Entry<String, Versioned<String>> ctxtEntry : contextmap.entrySet()) {
hintNode.put(ctxtEntry.getKey(), ctxtEntry.getValue().value());
}
}
}
return rootNode;
}
use of org.onosproject.store.service.Versioned in project onos by opennetworkinglab.
the class DefaultConsistentMapTest method testBehavior.
/**
* Tests the behavior of public APIs of the default consistent map
* implmentation.
*/
@Test
public void testBehavior() {
Map<String, String> baseMap = new HashMap<>();
AsyncConsistentMapMock<String, String> asyncMap = new AsyncConsistentMapMock<>(baseMap);
ConsistentMap<String, String> newMap = new DefaultConsistentMap<>(asyncMap, 11);
assertThat(newMap.size(), is(0));
assertThat(newMap.isEmpty(), is(true));
newMap.put(KEY1, VALUE1);
assertThat(newMap.size(), is(1));
assertThat(newMap.get(KEY1).value(), is(VALUE1));
assertThat(newMap.containsKey(KEY1), is(true));
assertThat(newMap.containsKey(VALUE1), is(false));
assertThat(newMap.containsValue(VALUE1), is(true));
assertThat(newMap.containsValue(KEY1), is(false));
assertThat(newMap.keySet(), hasSize(1));
assertThat(newMap.keySet(), hasItem(KEY1));
assertThat(newMap.values(), hasSize(1));
assertThat(newMap.values(), hasItem(new Versioned<>(VALUE1, 0, 0)));
assertThat(newMap.entrySet(), hasSize(1));
Map.Entry<String, Versioned<String>> entry = newMap.entrySet().iterator().next();
assertThat(entry.getKey(), is(KEY1));
assertThat(entry.getValue().value(), is(VALUE1));
newMap.putIfAbsent(KEY2, VALUE2);
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.get(KEY2).value(), is(VALUE2));
newMap.putIfAbsent(KEY2, VALUE1);
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.get(KEY2).value(), is(VALUE2));
newMap.putAndGet(KEY3, VALUE3);
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.get(KEY3).value(), is(VALUE3));
newMap.putIfAbsent(KEY3, VALUE1);
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.get(KEY3).value(), is(VALUE3));
assertThat(newMap.computeIfAbsent(KEY4, this::computeFunction).value(), is(VALUE4));
assertThat(computeFunctionCalls, is(1));
assertThat(newMap.entrySet(), hasSize(4));
assertThat(newMap.computeIfAbsent(KEY4, this::computeFunction).value(), is(VALUE4));
assertThat(computeFunctionCalls, is(1));
Map javaMap = newMap.asJavaMap();
assertThat(javaMap.size(), is(newMap.size()));
assertThat(javaMap.get(KEY1), is(VALUE1));
assertThat(newMap.toString(), containsString(KEY4 + "=" + VALUE4));
assertThat(newMap.remove(KEY4).value(), is(VALUE4));
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.remove(KEY4).value(), nullValue());
assertThat(newMap.entrySet(), hasSize(3));
assertThat(newMap.remove(KEY3, DEFAULT_VERSION), is(true));
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.remove(KEY3, DEFAULT_VERSION), is(false));
assertThat(newMap.entrySet(), hasSize(2));
assertThat(newMap.remove(KEY2, VALUE2), is(true));
assertThat(newMap.entrySet(), hasSize(1));
assertThat(newMap.remove(KEY2, VALUE2), is(false));
assertThat(newMap.entrySet(), hasSize(1));
assertThat(newMap.replace(KEY1, VALUE4).value(), is(VALUE1));
assertThat(newMap.get(KEY1).value(), is(VALUE4));
assertThat(newMap.replace(KEY1, VALUE4, VALUE2), is(true));
assertThat(newMap.get(KEY1).value(), is(VALUE2));
assertThat(newMap.replace(KEY1, DEFAULT_VERSION, VALUE1), is(true));
assertThat(newMap.get(KEY1).value(), is(VALUE1));
newMap.clear();
assertThat(newMap.size(), is(0));
newMap.compute(KEY1, (a, b) -> VALUE1);
assertThat(newMap.get(KEY1).value(), is(VALUE1));
newMap.computeIfPresent(KEY1, (a, b) -> VALUE2);
assertThat(newMap.get(KEY1).value(), is(VALUE2));
Listener listener1 = new Listener(1);
newMap.addListener(listener1, null);
assertThat(asyncMap.listeners, hasSize(1));
assertThat(asyncMap.listeners, hasItem(listener1));
newMap.removeListener(listener1);
assertThat(asyncMap.listeners, hasSize(0));
Consumer<DistributedPrimitive.Status> consumer = status -> {
};
newMap.addStatusChangeListener(consumer);
assertThat(newMap.statusChangeListeners(), hasSize(1));
assertThat(newMap.statusChangeListeners(), hasItem(consumer));
newMap.removeStatusChangeListener(consumer);
assertThat(newMap.statusChangeListeners(), hasSize(0));
assertThat(newMap.statusChangeListeners(), not(hasItem(consumer)));
}
Aggregations