entity.relations().add(DockerContainer.RUNNING.getInverseRelationshipType(), this);
private static void populateBrooklynObjectMementoBuilder(BrooklynObject instance, AbstractMemento.Builder<?> builder) { if (Proxy.isProxyClass(instance.getClass())) { throw new IllegalStateException("Attempt to create memento from proxy "+instance+" (would fail with wrong type)"); } OsgiClassPrefixer prefixer = new OsgiClassPrefixer(); Optional<String> typePrefix = prefixer.getPrefix(instance.getClass()); builder.id = instance.getId(); builder.displayName = instance.getDisplayName(); builder.catalogItemId = instance.getCatalogItemId(); builder.searchPath = instance.getCatalogItemIdSearchPath(); builder.type = (typePrefix.isPresent() ? typePrefix.get() : "") + instance.getClass().getName(); builder.typeClass = instance.getClass(); if (instance instanceof EntityAdjunct) { builder.uniqueTag = ((EntityAdjunct)instance).getUniqueTag(); } for (Object tag : instance.tags().getTags()) { builder.tags.add(tag); } // CatalogItems return empty support, so this is safe even through they don't support relations for (RelationshipType<?,? extends BrooklynObject> relationship: instance.relations().getRelationshipTypes()) { @SuppressWarnings({ "unchecked", "rawtypes" }) Set relations = instance.relations().getRelations((RelationshipType)relationship); Set<String> relationIds = Sets.newLinkedHashSet(); for (Object r: relations) relationIds.add( ((BrooklynObject)r).getId() ); builder.relations.put(relationship.getRelationshipTypeName(), relationIds); } }
@Override public void connect(DockerContainer container, VirtualNetwork network) { synchronized (network) { MutableSet<Entity> connected = MutableSet.copyOf(network.sensors().get(VirtualNetwork.CONNECTED_CONTAINERS)); connected.add(container); network.sensors().set(VirtualNetwork.CONNECTED_CONTAINERS, connected.asImmutableCopy()); } network.relations().add(VirtualNetwork.ATTACHED, container); container.relations().add(VirtualNetwork.CONNECTED, network); }
@Override public void disconnect(DockerContainer container, VirtualNetwork network) { synchronized (network) { MutableSet<Entity> connected = MutableSet.copyOf(network.sensors().get(VirtualNetwork.CONNECTED_CONTAINERS)); connected.remove(container); network.sensors().set(VirtualNetwork.CONNECTED_CONTAINERS, connected.asImmutableCopy()); } network.relations().remove(VirtualNetwork.CONNECTED, container); container.relations().remove(VirtualNetwork.CONNECTED, network); }
/** * As {@link RelationSupport#getRelations(RelationshipType)} for the given object. Callers can use either method. * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T extends BrooklynObject,U extends BrooklynObject> Set<U> getRelations(RelationshipType<? super T,U> relationship, T source) { return ((BrooklynObjectInternal)source).relations().getLocalBackingStore().getRelations((RelationshipType)relationship); }
/** * As {@link RelationSupport#getRelationshipTypes()} for the given object. Callers can use either method. * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T extends BrooklynObject> Set<RelationshipType<? super T,? extends BrooklynObject>> getRelationshipTypes(T source) { return (Set) ((BrooklynObjectInternal)source).relations().getLocalBackingStore().getRelationshipTypes(); }
/** * As {@link RelationSupport#add(RelationshipType, BrooklynObject)} for the given object. Callers can use either method. * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T extends BrooklynObject,U extends BrooklynObject> void add(T source, RelationshipType<? super T,? super U> relationship, U target) { ((BrooklynObjectInternal)source).relations().getLocalBackingStore().add((RelationshipType)relationship, target); if (relationship.getInverseRelationshipType()!=null) ((BrooklynObjectInternal)target).relations().getLocalBackingStore().add((RelationshipType)relationship.getInverseRelationshipType(), source); }
/** * As {@link RelationSupport#remove(RelationshipType, BrooklynObject)} for the given object. Callers can use either method. * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T extends BrooklynObject,U extends BrooklynObject> void remove(T source, RelationshipType<? super T,? super U> relationship, U target) { ((BrooklynObjectInternal)source).relations().getLocalBackingStore().remove((RelationshipType)relationship, target); if (relationship.getInverseRelationshipType()!=null) ((BrooklynObjectInternal)target).relations().getLocalBackingStore().remove((RelationshipType)relationship.getInverseRelationshipType(), source); }
public void testCustomEntityRelation() throws Exception { TestEntity origT1 = origApp.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity origT2 = origApp.createAndManageChild(EntitySpec.create(TestEntity.class)); origT1.relations().add(EntityRelations.HAS_TARGET, origT2); TestApplication newApp = rebind(); Iterator<Entity> ci = newApp.getChildren().iterator(); Entity t1 = ci.next(); Entity t2 = ci.next(); Assert.assertEquals(t1.relations().getRelations(EntityRelations.HAS_TARGET), Collections.singleton(t2)); Assert.assertEquals(t2.relations().getRelations(EntityRelations.HAS_TARGET), Collections.emptySet()); Assert.assertEquals(t2.relations().getRelations(EntityRelations.TARGETTED_BY), Collections.singleton(t1)); }