Search in sources :

Example 1 with Versioned

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;
}
Also used : IntIntent(org.onosproject.inbandtelemetry.api.IntIntent) ConsistentMap(org.onosproject.store.service.ConsistentMap) NetworkConfigService(org.onosproject.net.config.NetworkConfigService) NetworkConfigRegistry(org.onosproject.net.config.NetworkConfigRegistry) ScheduledFuture(java.util.concurrent.ScheduledFuture) CoreService(org.onosproject.core.CoreService) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) TimeoutException(java.util.concurrent.TimeoutException) SharedScheduledExecutors(org.onlab.util.SharedScheduledExecutors) ConnectPoint(org.onosproject.net.ConnectPoint) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) StorageService(org.onosproject.store.service.StorageService) SubjectFactories(org.onosproject.net.config.basics.SubjectFactories) Map(java.util.Map) ApplicationId(org.onosproject.core.ApplicationId) IntIntentId(org.onosproject.inbandtelemetry.api.IntIntentId) IntProgrammable(org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) MastershipService(org.onosproject.mastership.MastershipService) Serializer(org.onosproject.store.service.Serializer) Device(org.onosproject.net.Device) Deactivate(org.osgi.service.component.annotations.Deactivate) AtomicIdGenerator(org.onosproject.store.service.AtomicIdGenerator) Set(java.util.Set) Collectors(java.util.stream.Collectors) Versioned(org.onosproject.store.service.Versioned) ConfigFactory(org.onosproject.net.config.ConfigFactory) IntReportConfig(org.onosproject.net.behaviour.inbandtelemetry.IntReportConfig) IntIntentCodec(org.onosproject.inbandtelemetry.rest.IntIntentCodec) DeviceEvent(org.onosproject.net.device.DeviceEvent) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) AtomicValueEvent(org.onosproject.store.service.AtomicValueEvent) NetworkConfigEvent(org.onosproject.net.config.NetworkConfigEvent) KryoNamespace(org.onlab.util.KryoNamespace) HostListener(org.onosproject.net.host.HostListener) HostService(org.onosproject.net.host.HostService) MapEventListener(org.onosproject.store.service.MapEventListener) ConcurrentMap(java.util.concurrent.ConcurrentMap) AtomicValue(org.onosproject.store.service.AtomicValue) Component(org.osgi.service.component.annotations.Component) TrafficSelector(org.onosproject.net.flow.TrafficSelector) HostEvent(org.onosproject.net.host.HostEvent) Activate(org.osgi.service.component.annotations.Activate) IntService(org.onosproject.inbandtelemetry.api.IntService) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) ExecutorService(java.util.concurrent.ExecutorService) DeviceListener(org.onosproject.net.device.DeviceListener) Striped(com.google.common.util.concurrent.Striped) Logger(org.slf4j.Logger) MastershipRole(org.onosproject.net.MastershipRole) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IntDeviceConfig(org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig) Maps(com.google.common.collect.Maps) CodecService(org.onosproject.codec.CodecService) AtomicValueEventListener(org.onosproject.store.service.AtomicValueEventListener) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Lock(java.util.concurrent.locks.Lock) IntObjective(org.onosproject.net.behaviour.inbandtelemetry.IntObjective) MapEvent(org.onosproject.store.service.MapEvent) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Reference(org.osgi.service.component.annotations.Reference) NetworkConfigListener(org.onosproject.net.config.NetworkConfigListener) IntMetadataType(org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType) Device(org.onosproject.net.Device) IntIntent(org.onosproject.inbandtelemetry.api.IntIntent) IntObjective(org.onosproject.net.behaviour.inbandtelemetry.IntObjective) IntProgrammable(org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable) PortNumber(org.onosproject.net.PortNumber) ConnectPoint(org.onosproject.net.ConnectPoint) ConnectPoint(org.onosproject.net.ConnectPoint)

Example 2 with Versioned

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));
}
Also used : LeafType(org.onosproject.yang.model.LeafType) Versioned(org.onosproject.store.service.Versioned) DataNode(org.onosproject.yang.model.DataNode) FailedException(org.onosproject.config.FailedException) ConsistentMap(org.onosproject.store.service.ConsistentMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Versioned

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;
}
Also used : Versioned(org.onosproject.store.service.Versioned) DocumentPath(org.onosproject.store.service.DocumentPath)

Example 4 with Versioned

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;
}
Also used : Versioned(org.onosproject.store.service.Versioned) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DocumentPath(org.onosproject.store.service.DocumentPath) Map(java.util.Map) EventuallyConsistentMap(org.onosproject.store.service.EventuallyConsistentMap)

Example 5 with Versioned

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)));
}
Also used : ConsistentMap(org.onosproject.store.service.ConsistentMap) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) BiFunction(java.util.function.BiFunction) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DistributedPrimitive(org.onosproject.store.service.DistributedPrimitive) ArrayList(java.util.ArrayList) MapEventListener(org.onosproject.store.service.MapEventListener) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Versioned(org.onosproject.store.service.Versioned) List(java.util.List) MapEvent(org.onosproject.store.service.MapEvent) AsyncConsistentMapAdapter(org.onosproject.store.service.AsyncConsistentMapAdapter) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) MapEventListener(org.onosproject.store.service.MapEventListener) Versioned(org.onosproject.store.service.Versioned) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ConsistentMap(org.onosproject.store.service.ConsistentMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Versioned (org.onosproject.store.service.Versioned)11 Map (java.util.Map)7 ConsistentMap (org.onosproject.store.service.ConsistentMap)5 DocumentPath (org.onosproject.store.service.DocumentPath)5 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 FailedException (org.onosproject.config.FailedException)3 DataNode (org.onosproject.yang.model.DataNode)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Executors.newSingleThreadScheduledExecutor (java.util.concurrent.Executors.newSingleThreadScheduledExecutor)2 TimeUnit (java.util.concurrent.TimeUnit)2 MapEvent (org.onosproject.store.service.MapEvent)2 MapEventListener (org.onosproject.store.service.MapEventListener)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1