private static final String FMT =
"%s id=%d, name=%s, version=%s, origin=%s, description=%s, " +
- "features=%s, featuresRepo=%s, permissions=%s";
+ "features=%s, featuresRepo=%s, apps=%s, permissions=%s";
private static final String SHORT_FMT =
"%s %3d %-32s %-8s %s";
app.id().id(), app.id().name(), app.version(), app.origin(),
app.description(), app.features(),
app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "",
- app.permissions());
+ app.requiredApps(), app.permissions());
}
}
}
* @return application features
*/
List<String> features();
+
+ /**
+ * Returns list of required application names.
+ *
+ * @return list of application names
+ */
+ List<String> requiredApps();
}
void remove(ApplicationId appId);
/**
- * Mark the application as actived.
+ * Mark the application as active.
*
* @param appId application identifier
*/
private final Set<Permission> permissions;
private final Optional<URI> featuresRepo;
private final List<String> features;
+ private final List<String> requiredApps;
/**
* Creates a new application descriptor using the supplied data.
* @param permissions requested permissions
* @param featuresRepo optional features repo URI
* @param features application features
+ * @param requiredApps list of required application names
*/
public DefaultApplicationDescription(String name, Version version,
String description, String origin,
ApplicationRole role, Set<Permission> permissions,
- URI featuresRepo, List<String> features) {
+ URI featuresRepo, List<String> features,
+ List<String> requiredApps) {
this.name = checkNotNull(name, "Name cannot be null");
this.version = checkNotNull(version, "Version cannot be null");
this.description = checkNotNull(description, "Description cannot be null");
this.permissions = checkNotNull(permissions, "Permissions cannot be null");
this.featuresRepo = Optional.ofNullable(featuresRepo);
this.features = checkNotNull(features, "Features cannot be null");
+ this.requiredApps = checkNotNull(requiredApps, "Required apps cannot be null");
checkArgument(!features.isEmpty(), "There must be at least one feature");
}
return features;
}
+ @Override
+ public List<String> requiredApps() {
+ return requiredApps;
+ }
+
@Override
public String toString() {
return toStringHelper(this)
.add("permissions", permissions)
.add("featuresRepo", featuresRepo)
.add("features", features)
+ .add("requiredApps", requiredApps)
.toString();
}
}
* @return application features
*/
List<String> features();
+
+ /**
+ * Returns list of required application names.
+ *
+ * @return list of application names
+ */
+ List<String> requiredApps();
}
private final Set<Permission> permissions;
private final Optional<URI> featuresRepo;
private final List<String> features;
+ private final List<String> requiredApps;
/**
* Creates a new application descriptor using the supplied data.
* @param permissions requested permissions
* @param featuresRepo optional features repo URI
* @param features application features
+ * @param requiredApps list of required application names
*/
public DefaultApplication(ApplicationId appId, Version version,
String description, String origin,
ApplicationRole role, Set<Permission> permissions,
- Optional<URI> featuresRepo, List<String> features) {
+ Optional<URI> featuresRepo, List<String> features,
+ List<String> requiredApps) {
this.appId = checkNotNull(appId, "ID cannot be null");
this.version = checkNotNull(version, "Version cannot be null");
this.description = checkNotNull(description, "Description cannot be null");
this.permissions = checkNotNull(permissions, "Permissions cannot be null");
this.featuresRepo = checkNotNull(featuresRepo, "Features repo cannot be null");
this.features = checkNotNull(features, "Features cannot be null");
+ this.requiredApps = checkNotNull(requiredApps, "Required apps cannot be null");
checkArgument(!features.isEmpty(), "There must be at least one feature");
}
return features;
}
+ @Override
+ public List<String> requiredApps() {
+ return requiredApps;
+ }
+
@Override
public int hashCode() {
return Objects.hash(appId, version, description, origin, role, permissions,
- featuresRepo, features);
+ featuresRepo, features, requiredApps);
}
@Override
Objects.equals(this.role, other.role) &&
Objects.equals(this.permissions, other.permissions) &&
Objects.equals(this.featuresRepo, other.featuresRepo) &&
- Objects.equals(this.features, other.features);
+ Objects.equals(this.features, other.features) &&
+ Objects.equals(this.requiredApps, other.requiredApps);
}
@Override
.add("permissions", permissions)
.add("featuresRepo", featuresRepo)
.add("features", features)
+ .add("requiredApps", requiredApps)
.toString();
}
}
private Application createApp() {
return new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE,
- PERMS, Optional.of(FURL), FEATURES);
+ PERMS, Optional.of(FURL), FEATURES, APPS);
}
@Test
new Permission(AppPermission.class.getName(), "FLOWRULE_READ"));
public static final URI FURL = URI.create("mvn:org.foo-features/1.2a/xml/features");
public static final List<String> FEATURES = ImmutableList.of("foo", "bar");
+ public static final List<String> APPS = ImmutableList.of("fifi");
@Test
public void basics() {
ApplicationDescription app =
new DefaultApplicationDescription(APP_NAME, VER, DESC, ORIGIN,
- ROLE, PERMS, FURL, FEATURES);
+ ROLE, PERMS, FURL, FEATURES, APPS);
assertEquals("incorrect id", APP_NAME, app.name());
assertEquals("incorrect version", VER, app.version());
assertEquals("incorrect description", DESC, app.description());
assertEquals("incorrect permissions", PERMS, app.permissions());
assertEquals("incorrect features repo", FURL, app.featuresRepo().get());
assertEquals("incorrect features", FEATURES, app.features());
+ assertEquals("incorrect apps", APPS, app.requiredApps());
assertTrue("incorrect toString", app.toString().contains(APP_NAME));
}
@Test
public void basics() {
Application app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE,
- PERMS, Optional.of(FURL), FEATURES);
+ PERMS, Optional.of(FURL), FEATURES, APPS);
assertEquals("incorrect id", APP_ID, app.id());
assertEquals("incorrect version", VER, app.version());
assertEquals("incorrect description", DESC, app.description());
assertEquals("incorrect permissions", PERMS, app.permissions());
assertEquals("incorrect features repo", FURL, app.featuresRepo().get());
assertEquals("incorrect features", FEATURES, app.features());
+ assertEquals("incorrect apps", APPS, app.requiredApps());
assertTrue("incorrect toString", app.toString().contains(APP_NAME));
}
@Test
public void testEquality() {
Application a1 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE,
- PERMS, Optional.of(FURL), FEATURES);
+ PERMS, Optional.of(FURL), FEATURES, APPS);
Application a2 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE,
- PERMS, Optional.of(FURL), FEATURES);
+ PERMS, Optional.of(FURL), FEATURES, APPS);
Application a3 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE,
- PERMS, Optional.empty(), FEATURES);
+ PERMS, Optional.empty(), FEATURES, APPS);
Application a4 = new DefaultApplication(APP_ID, VER, DESC, ORIGIN + "asd", ROLE,
- PERMS, Optional.of(FURL), FEATURES);
+ PERMS, Optional.of(FURL), FEATURES, APPS);
new EqualsTester().addEqualityGroup(a1, a2)
.addEqualityGroup(a3).addEqualityGroup(a4).testEquals();
}
public ObjectNode encode(Application app, CodecContext context) {
checkNotNull(app, "Application cannot be null");
ApplicationService service = context.getService(ApplicationService.class);
- ObjectNode result = context.mapper().createObjectNode()
+ return context.mapper().createObjectNode()
.put("name", app.id().name())
.put("id", app.id().id())
.put("version", app.version().toString())
.put("description", app.description())
.put("origin", app.origin())
- .put("permissions", app.permissions().toString())
+ .put("permissions", app.permissions().toString()) // FIXME: change to an array
.put("featuresRepo", app.featuresRepo().isPresent() ?
app.featuresRepo().get().toString() : "")
- .put("features", app.features().toString())
+ .put("features", app.features().toString()) // FIXME: change to an array
+ .put("requiredApps", app.requiredApps().toString()) // FIXME: change to an array
.put("state", service.getState(app.id()).toString());
- return result;
}
}
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import org.apache.commons.configuration.ConfigurationException;
import org.onosproject.security.AppPermission;
import org.onosproject.security.Permission;
import org.onosproject.store.AbstractStore;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.NoSuchFileException;
-import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
private static final String VERSION = "[@version]";
private static final String FEATURES_REPO = "[@featuresRepo]";
private static final String FEATURES = "[@features]";
+ private static final String APPS = "[@apps]";
private static final String DESCRIPTION = "description";
private static final String ROLE = "security.role";
URI featuresRepo = featRepo != null ? URI.create(featRepo) : null;
List<String> features = ImmutableList.copyOf(cfg.getString(FEATURES).split(","));
+ String apps = cfg.getString(APPS, "");
+ List<String> requiredApps = apps.isEmpty() ?
+ ImmutableList.of() : ImmutableList.copyOf(apps.split(","));
+
return new DefaultApplicationDescription(name, version, desc, origin, role,
- perms, featuresRepo, features);
+ perms, featuresRepo, features,
+ requiredApps);
}
// Expands the specified ZIP stream into app-specific directory.
// Returns the set of Permissions specified in the app.xml file
private ImmutableSet<Permission> getPermissions(XMLConfiguration cfg) {
- List<Permission> permissionList = new ArrayList();
+ List<Permission> permissionList = Lists.newArrayList();
for (Object o : cfg.getList(APP_PERMISSIONS)) {
String name = (String) o;
new DefaultApplication(appId, appDesc.version(),
appDesc.description(), appDesc.origin(),
appDesc.role(), appDesc.permissions(),
- appDesc.featuresRepo(), appDesc.features());
+ appDesc.featuresRepo(), appDesc.features(),
+ appDesc.requiredApps());
apps.put(appId, app);
states.put(appId, isActive(name) ? INSTALLED : ACTIVE);
// load app permissions
DefaultApplication app =
new DefaultApplication(appId, appDesc.version(), appDesc.description(),
appDesc.origin(), appDesc.role(), appDesc.permissions(),
- appDesc.featuresRepo(), appDesc.features());
+ appDesc.featuresRepo(), appDesc.features(),
+ appDesc.requiredApps());
apps.put(appId, app);
states.put(appId, INSTALLED);
delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
// The following methods are fully synchronized to guard against remote vs.
// locally induced feature service interactions.
+ // Installs all feature repositories required by the specified app.
private synchronized boolean installAppArtifacts(Application app) throws Exception {
if (app.featuresRepo().isPresent() &&
featuresService.getRepository(app.featuresRepo().get()) == null) {
return false;
}
+ // Uninstalls all the feature repositories required by the specified app.
private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
if (app.featuresRepo().isPresent() &&
featuresService.getRepository(app.featuresRepo().get()) != null) {
return false;
}
+ // Installs all features that define the specified app.
private synchronized boolean installAppFeatures(Application app) throws Exception {
boolean changed = false;
for (String name : app.features()) {
return changed;
}
+ // Uninstalls all features that define the specified app.
private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
boolean changed = false;
invokeHook(deactivateHooks.get(app.id().name()), app.id());
*/
package org.onosproject.app.impl;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.After;
import org.junit.Before;
@Override
public Application create(InputStream appDescStream) {
app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE, PERMS,
- Optional.of(FURL), FEATURES);
+ Optional.of(FURL), FEATURES, ImmutableList.of());
state = INSTALLED;
delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
return app;
state = INSTALLED;
delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
}
+
+ @Override
+ public ApplicationId getId(String name) {
+ return new DefaultApplicationId(0, name);
+ }
}
private class TestFeaturesService extends FeaturesServiceAdapter {
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
-
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.onosproject.core.Application;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.ApplicationIdStore;
+import org.onosproject.core.CoreService;
import org.onosproject.core.DefaultApplication;
import org.onosproject.security.Permission;
import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
+import static com.google.common.collect.Multimaps.newSetMultimap;
+import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
import static com.google.common.io.ByteStreams.toByteArray;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.onlab.util.Tools.groupedThreads;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ApplicationIdStore idStore;
+ // Multimap to track which apps are required by others apps
+ // app -> { required-by, ... }
+ // Apps explicitly activated will be required by the CORE app
+ private final Multimap<ApplicationId, ApplicationId> requiredBy =
+ synchronizedSetMultimap(newSetMultimap(Maps.newHashMap(), Sets::newHashSet));
+
+ private ApplicationId coreAppId;
+
@Activate
public void activate() {
KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
groupedThreads("onos/store/app", "message-handler"));
clusterCommunicator.<String, byte[]>addSubscriber(APP_BITS_REQUEST,
- bytes -> new String(bytes, Charsets.UTF_8),
- name -> {
- try {
- return toByteArray(getApplicationInputStream(name));
- } catch (IOException e) {
- throw new StorageException(e);
- }
- },
- Function.identity(),
- messageHandlingExecutor);
+ bytes -> new String(bytes, Charsets.UTF_8),
+ name -> {
+ try {
+ return toByteArray(getApplicationInputStream(name));
+ } catch (IOException e) {
+ throw new StorageException(e);
+ }
+ },
+ Function.identity(),
+ messageHandlingExecutor);
// FIXME: Consider consolidating into a single map.
.withTimestampProvider((k, v) -> clockService.getTimestamp())
.build();
+ coreAppId = getId(CoreService.CORE_APP_NAME);
log.info("Started");
}
try {
Application app = create(getApplicationDescription(name), false);
if (app != null && isActive(app.id().name())) {
+ requiredBy.put(app.id(), coreAppId);
activate(app.id(), false);
// load app permissions
}
public void setDelegate(ApplicationStoreDelegate delegate) {
super.setDelegate(delegate);
loadFromDisk();
-// executor.schedule(this::pruneUninstalledApps, LOAD_TIMEOUT_MS, MILLISECONDS);
}
@Override
@Override
public Application create(InputStream appDescStream) {
ApplicationDescription appDesc = saveApplication(appDescStream);
- return create(appDesc, true);
+ if (hasPrerequisites(appDesc)) {
+ return create(appDesc, true);
+ }
+ throw new ApplicationException("Missing dependencies for app " + appDesc.name());
+ }
+
+ private boolean hasPrerequisites(ApplicationDescription app) {
+ return !app.requiredApps().stream().map(n -> getId(n))
+ .anyMatch(id -> id == null || getApplication(id) == null);
}
private Application create(ApplicationDescription appDesc, boolean updateTime) {
public void remove(ApplicationId appId) {
Application app = apps.get(appId);
if (app != null) {
+ uninstallDependentApps(app);
apps.remove(appId);
states.remove(app);
permissions.remove(app);
}
}
+ // Uninstalls all apps that depend on the given app.
+ private void uninstallDependentApps(Application app) {
+ getApplications().stream()
+ .filter(a -> a.requiredApps().contains(app.id().name()))
+ .forEach(a -> remove(a.id()));
+ }
+
@Override
public void activate(ApplicationId appId) {
+ activate(appId, coreAppId);
+ }
+
+ private void activate(ApplicationId appId, ApplicationId forAppId) {
+ requiredBy.put(appId, forAppId);
activate(appId, true);
}
+
private void activate(ApplicationId appId, boolean updateTime) {
Application app = apps.get(appId);
if (app != null) {
if (updateTime) {
updateTime(appId.name());
}
+ activateRequiredApps(app);
states.put(app, ACTIVATED);
}
}
+ // Activates all apps required by this application.
+ private void activateRequiredApps(Application app) {
+ app.requiredApps().stream().map(this::getId).forEach(id -> activate(id, app.id()));
+ }
+
@Override
public void deactivate(ApplicationId appId) {
- Application app = apps.get(appId);
- if (app != null) {
- updateTime(appId.name());
- states.put(app, DEACTIVATED);
+ deactivateDependentApps(getApplication(appId));
+ deactivate(appId, coreAppId);
+ }
+
+ private void deactivate(ApplicationId appId, ApplicationId forAppId) {
+ requiredBy.remove(appId, forAppId);
+ if (requiredBy.get(appId).isEmpty()) {
+ Application app = apps.get(appId);
+ if (app != null) {
+ updateTime(appId.name());
+ states.put(app, DEACTIVATED);
+ deactivateRequiredApps(app);
+ }
}
}
+ // Deactivates all apps that require this application.
+ private void deactivateDependentApps(Application app) {
+ getApplications().stream()
+ .filter(a -> states.get(a) == ACTIVATED)
+ .filter(a -> a.requiredApps().contains(app.id().name()))
+ .forEach(a -> deactivate(a.id()));
+ }
+
+ // Deactivates all apps required by this application.
+ private void deactivateRequiredApps(Application app) {
+ app.requiredApps().stream().map(this::getId).map(this::getApplication)
+ .filter(a -> states.get(a) == ACTIVATED)
+ .forEach(a -> deactivate(a.id(), app.id()));
+ }
+
@Override
public Set<Permission> getPermissions(ApplicationId appId) {
Application app = apps.get(appId);
ApplicationId appId = idStore.registerApplication(appDesc.name());
return new DefaultApplication(appId, appDesc.version(), appDesc.description(),
appDesc.origin(), appDesc.role(), appDesc.permissions(),
- appDesc.featuresRepo(), appDesc.features());
+ appDesc.featuresRepo(), appDesc.features(),
+ appDesc.requiredApps());
}
}
private static final String ONOS_APP_NAME = "onos.app.name";
private static final String ONOS_APP_ORIGIN = "onos.app.origin";
+ private static final String ONOS_APP_REQUIRES = "onos.app.requires";
private static final String JAR = "jar";
private static final String XML = "xml";
private String name;
private String origin;
+ private String requiredApps;
private String version = DEFAULT_VERSION;
private String featuresRepo = DEFAULT_FEATURES_REPO;
private List<String> artifacts;
origin = (String) project.getProperties().get(ONOS_APP_ORIGIN);
origin = origin != null ? origin : DEFAULT_ORIGIN;
+ requiredApps = (String) project.getProperties().get(ONOS_APP_REQUIRES);
+ requiredApps = requiredApps == null ? "" : requiredApps;
+
if (appFile.exists()) {
loadAppFile(appFile);
} else {
return string == null ? null :
string.replaceAll("\\$\\{onos.app.name\\}", name)
.replaceAll("\\$\\{onos.app.origin\\}", origin)
+ .replaceAll("\\$\\{onos.app.requires\\}", requiredApps)
.replaceAll("\\$\\{project.groupId\\}", projectGroupId)
.replaceAll("\\$\\{project.artifactId\\}", projectArtifactId)
.replaceAll("\\$\\{project.version\\}", projectVersion)
-->
<app name="${onos.app.name}" origin="${onos.app.origin}" version="${project.version}"
featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"
- features="${project.artifactId}">
+ features="${project.artifactId}" apps="${onos.app.requires}">
<description>${project.description}</description>
<artifact>mvn:${project.groupId}/${project.artifactId}/${project.version}</artifact>
</app>
--- /dev/null
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.util;
+
+import java.util.HashMap;
+
+/**
+ * HashMap that returns a default value for unmapped keys.
+ */
+public class DefaultHashMap<K, V> extends HashMap<K, V> {
+
+ /** Default value to return when no key binding exists. */
+ protected V defaultValue;
+
+ /**
+ * Constructs an empty map with the given default value.
+ *
+ * @param defaultValue the default value
+ */
+ public DefaultHashMap(V defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+
+ @Override
+ public V get(Object k) {
+ return containsKey(k) ? super.get(k) : defaultValue;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onlab.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link DefaultHashMap}.
+ */
+public class DefaultHashMapTest {
+
+ private static final String ONE = "one";
+ private static final String TWO = "two";
+ private static final String THREE = "three";
+ private static final String FOUR = "four";
+
+ private static final String ALPHA = "Alpha";
+ private static final String BETA = "Beta";
+ private static final String OMEGA = "Omega";
+
+ private DefaultHashMap<String, Integer> map;
+ private DefaultHashMap<String, String> chartis;
+
+ private void loadMap() {
+ map.put(ONE, 1);
+ map.put(TWO, 2);
+ }
+
+ private void fortioCharti() {
+ chartis.put(ONE, ALPHA);
+ chartis.put(TWO, BETA);
+ }
+
+ @Test
+ public void nullDefaultIsAllowed() {
+ // but makes this class behave no different than HashMap
+ map = new DefaultHashMap<>(null);
+ loadMap();
+ assertEquals("missing 1", 1, (int) map.get(ONE));
+ assertEquals("missing 2", 2, (int) map.get(TWO));
+ assertEquals("three?", null, map.get(THREE));
+ assertEquals("four?", null, map.get(FOUR));
+ }
+
+ @Test
+ public void defaultToFive() {
+ map = new DefaultHashMap<>(5);
+ loadMap();
+ assertEquals("missing 1", 1, (int) map.get(ONE));
+ assertEquals("missing 2", 2, (int) map.get(TWO));
+ assertEquals("three?", 5, (int) map.get(THREE));
+ assertEquals("four?", 5, (int) map.get(FOUR));
+ }
+
+ @Test
+ public void defaultToOmega() {
+ chartis = new DefaultHashMap<>(OMEGA);
+ fortioCharti();
+ assertEquals("missing 1", ALPHA, chartis.get(ONE));
+ assertEquals("missing 2", BETA, chartis.get(TWO));
+ assertEquals("three?", OMEGA, chartis.get(THREE));
+ assertEquals("four?", OMEGA, chartis.get(FOUR));
+ }
+
+}
private Application app1 =
new DefaultApplication(id1, VER,
"app1", "origin1", ApplicationRole.ADMIN, ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"));
+ ImmutableList.of("My Feature"), ImmutableList.of());
private Application app2 =
new DefaultApplication(id2, VER,
"app2", "origin2", ApplicationRole.ADMIN, ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"));
+ ImmutableList.of("My Feature"), ImmutableList.of());
private Application app3 =
new DefaultApplication(id3, VER,
"app3", "origin3", ApplicationRole.ADMIN, ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"));
+ ImmutableList.of("My Feature"), ImmutableList.of());
private Application app4 =
new DefaultApplication(id4, VER,
"app4", "origin4", ApplicationRole.ADMIN, ImmutableSet.of(), Optional.of(FURL),
- ImmutableList.of("My Feature"));
+ ImmutableList.of("My Feature"), ImmutableList.of());
/**
* Hamcrest matcher to check that an application representation in JSON matches
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.osgi.ServiceDirectory;
import org.onlab.packet.IpAddress;
+import org.onlab.util.DefaultHashMap;
import org.onosproject.cluster.ClusterEvent;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
-import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
-import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_REMOVED;
import static org.onosproject.cluster.ControllerNode.State.ACTIVE;
import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
import static org.onosproject.net.PortNumber.portNumber;
-import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
-import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
-import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
-import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
-import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
-import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
import static org.onosproject.ui.topo.TopoConstants.CoreButtons;
import static org.onosproject.ui.topo.TopoConstants.Properties;
import static org.onosproject.ui.topo.TopoUtils.compactLinkString;
*/
public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
+ // default to an "add" event...
+ private static final DefaultHashMap<ClusterEvent.Type, String> CLUSTER_EVENT =
+ new DefaultHashMap<>("addInstance");
+
+ // default to an "update" event...
+ private static final DefaultHashMap<DeviceEvent.Type, String> DEVICE_EVENT =
+ new DefaultHashMap<>("updateDevice");
+ private static final DefaultHashMap<LinkEvent.Type, String> LINK_EVENT =
+ new DefaultHashMap<>("updateLink");
+ private static final DefaultHashMap<HostEvent.Type, String> HOST_EVENT =
+ new DefaultHashMap<>("updateHost");
+
+ // but call out specific events that we care to differentiate...
+ static {
+ CLUSTER_EVENT.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
+
+ DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
+ DEVICE_EVENT.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
+
+ LINK_EVENT.put(LinkEvent.Type.LINK_ADDED, "addLink");
+ LINK_EVENT.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
+
+ HOST_EVENT.put(HostEvent.Type.HOST_ADDED, "addHost");
+ HOST_EVENT.put(HostEvent.Type.HOST_REMOVED, "removeHost");
+ HOST_EVENT.put(HostEvent.Type.HOST_MOVED, "moveHost");
+ }
+
protected static final Logger log =
LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class);
}
// Produces a cluster instance message to the client.
- protected ObjectNode instanceMessage(ClusterEvent event, String messageType) {
+ protected ObjectNode instanceMessage(ClusterEvent event, String msgType) {
ControllerNode node = event.subject();
int switchCount = mastershipService.getDevicesOf(node.id()).size();
ObjectNode payload = objectNode()
payload.set("labels", labels);
addMetaUi(node.id().toString(), payload);
- String type = messageType != null ? messageType :
- ((event.type() == INSTANCE_ADDED) ? "addInstance" :
- ((event.type() == INSTANCE_REMOVED ? "removeInstance" :
- "addInstance")));
+ String type = msgType != null ? msgType : CLUSTER_EVENT.get(event.type());
return JsonUtils.envelope(type, 0, payload);
}
addGeoLocation(device, payload);
addMetaUi(device.id().toString(), payload);
- String type = (event.type() == DEVICE_ADDED) ? "addDevice" :
- ((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice");
+ String type = DEVICE_EVENT.get(event.type());
return JsonUtils.envelope(type, 0, payload);
}
.put("srcPort", link.src().port().toString())
.put("dst", link.dst().deviceId().toString())
.put("dstPort", link.dst().port().toString());
- String type = (event.type() == LINK_ADDED) ? "addLink" :
- ((event.type() == LINK_REMOVED) ? "removeLink" : "updateLink");
+ String type = LINK_EVENT.get(event.type());
return JsonUtils.envelope(type, 0, payload);
}
protected ObjectNode hostMessage(HostEvent event) {
Host host = event.subject();
String hostType = host.annotations().value(AnnotationKeys.TYPE);
+ HostLocation prevLoc = event.prevLocation();
+
ObjectNode payload = objectNode()
.put("id", host.id().toString())
.put("type", isNullOrEmpty(hostType) ? "endstation" : hostType)
.put("ingress", compactLinkString(edgeLink(host, true)))
.put("egress", compactLinkString(edgeLink(host, false)));
payload.set("cp", hostConnect(host.location()));
+ if (prevLoc != null) {
+ payload.set("prevCp", hostConnect(event.prevLocation()));
+ }
payload.set("labels", labels(ip(host.ipAddresses()),
host.mac().toString()));
payload.set("props", props(host.annotations()));
addGeoLocation(host, payload);
addMetaUi(host.id().toString(), payload);
- String type = (event.type() == HOST_ADDED) ? "addHost" :
- ((event.type() == HOST_REMOVED) ? "removeHost" : "updateHost");
+ String type = HOST_EVENT.get(event.type());
return JsonUtils.envelope(type, 0, payload);
}
removeDevice: tfs,
addHost: tfs,
updateHost: tfs,
+ moveHost: tfs,
removeHost: tfs,
addLink: tfs,
updateLink: tfs,
}
}
+ function moveHost(data) {
+ var id = data.id,
+ d = lu[id],
+ lnk;
+ if (d) {
+ // first remove the old host link
+ removeLinkElement(d.linkData);
+
+ // merge new data
+ angular.extend(d, data);
+ if (tms.positionNode(d, true)) {
+ sendUpdateMeta(d);
+ }
+
+ // now create a new host link
+ lnk = tms.createHostLink(data);
+ if (lnk) {
+ d.linkData = lnk;
+ network.links.push(lnk);
+ lu[d.ingress] = lnk;
+ lu[d.egress] = lnk;
+ }
+
+ updateNodes();
+ updateLinks();
+ fResume();
+ }
+ }
+
function removeHost(data) {
var id = data.id,
d = lu[id];
removeDevice: removeDevice,
addHost: addHost,
updateHost: updateHost,
+ moveHost: moveHost,
removeHost: removeHost,
addLink: addLink,
updateLink: updateLink,