Java Code Examples for org.apache.brooklyn.api.location.Location#getParent()

The following examples show how to use org.apache.brooklyn.api.location.Location#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalLocationManager.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** management on creation */
@Override
public Location manage(Location loc) {
    if (isManaged(loc)) {
        // TODO put log.warn back in if/when manage(Location) becomes private; or could even have assert.
        // Can be stricter about contract.
        return loc;
    }
    
    Location parent = loc.getParent();
    if (parent != null && !managementContext.getLocationManager().isManaged(parent)) {
        log.warn("Parent location "+parent+" of "+loc+" is not managed; attempting to manage it (in future this may be disallowed)");
        return manage(parent);
    } else {
        return manageRecursive(loc, ManagementTransitionMode.guessing(BrooklynObjectManagementMode.NONEXISTENT, BrooklynObjectManagementMode.MANAGED_PRIMARY));
    }
}
 
Example 2
Source File: TreeUtils.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public static Collection<Location> findLocationsInHierarchy(Location root) {
    Set<Location> result = Sets.newLinkedHashSet();
    
    Deque<Location> tovisit = new ArrayDeque<Location>();
    tovisit.addFirst(root);
    
    while (tovisit.size() > 0) {
        Location current = tovisit.pop();
        result.add(current);
        for (Location child : current.getChildren()) {
            if (child != null) {
                tovisit.push(child);
            }
        }
    }

    Location parentLocation = root.getParent();
    while (parentLocation != null) {
        result.add(parentLocation);
        parentLocation = parentLocation.getParent();
    }

    return result;
}
 
Example 3
Source File: LocationPredicates.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/** @deprecated since 0.9.0 kept only to allow conversion of anonymous inner classes */
@SuppressWarnings("unused") @Deprecated 
private static <T> Predicate<Location> isDescendantOfOld(final Location ancestor) {
    // TODO PERSISTENCE WORKAROUND
    return new Predicate<Location>() {
        @Override
        public boolean apply(@Nullable Location input) {
            // assumes impossible to have cycles in location-hierarchy
            Location contenderAncestor = (input == null) ? input : input.getParent();
            while (contenderAncestor != null) {
                if (Objects.equal(contenderAncestor, ancestor)) {
                    return true;
                }
                contenderAncestor = contenderAncestor.getParent();
            }
            return false;
        }
    };
}
 
Example 4
Source File: RebindTestUtils.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
/**
 * Walks the contents of a ManagementContext, to create a corresponding memento.
 */
protected static BrooklynMemento newBrooklynMemento(ManagementContext managementContext) {
    BrooklynMementoImpl.Builder builder = BrooklynMementoImpl.builder();
            
    for (Application app : managementContext.getApplications()) {
        builder.applicationId(app.getId());
    }
    for (Entity entity : managementContext.getEntityManager().getEntities()) {
        builder.entity(((EntityInternal)entity).getRebindSupport().getMemento());
    }
    for (Location location : managementContext.getLocationManager().getLocations()) {
        builder.location(((LocationInternal)location).getRebindSupport().getMemento());
        if (location.getParent() == null) {
            builder.topLevelLocationId(location.getId());
        }
    }

    BrooklynMemento result = builder.build();
    MementoValidators.validateMemento(result);
    return result;
}
 
Example 5
Source File: FollowTheSunPolicy.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public Location apply(Entity e) {
    Collection<Location> locs = e.getLocations();
    if (locs.isEmpty()) return null;
    Location contender = Iterables.get(locs, 0);
    while (contender.getParent() != null && !(contender instanceof MachineProvisioningLocation)) {
        contender = contender.getParent();
    }
    return contender;
}
 
Example 6
Source File: MementosGenerators.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated since 0.7.0; use {@link #newBasicMemento(BrooklynObject)} instead
 */
@Deprecated
public static BasicLocationMemento.Builder newLocationMementoBuilder(Location location) {
    BasicLocationMemento.Builder builder = BasicLocationMemento.builder();
    populateBrooklynObjectMementoBuilder(location, builder);

    Set<String> nonPersistableFlagNames = MutableMap.<String,Object>builder()
            .putAll(FlagUtils.getFieldsWithFlagsWithModifiers(location, Modifier.TRANSIENT))
            .putAll(FlagUtils.getFieldsWithFlagsWithModifiers(location, Modifier.STATIC))
            .put("id", String.class)
            .filterValues(Predicates.not(Predicates.instanceOf(ConfigKey.class)))
            .build()
            .keySet();
    Map<String, Object> persistableFlags = MutableMap.<String, Object>builder()
            .putAll(FlagUtils.getFieldsWithFlagsExcludingModifiers(location, Modifier.STATIC ^ Modifier.TRANSIENT))
            .removeAll(nonPersistableFlagNames)
            .build();
    ConfigBag persistableConfig = new ConfigBag().copy( ((LocationInternal)location).config().getLocalBag() ).removeAll(nonPersistableFlagNames);

    builder.copyConfig(persistableConfig);
    builder.locationConfig.putAll(persistableFlags);

    Location parentLocation = location.getParent();
    builder.parent = (parentLocation != null) ? parentLocation.getId() : null;
    
    for (Location child : location.getChildren()) {
        builder.children.add(child.getId()); 
    }
    
    return builder;
}
 
Example 7
Source File: LocationPredicates.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public boolean apply(@Nullable Location input) {
    // assumes impossible to have cycles in location-hierarchy
    Location contenderAncestor = (input == null) ? input : input.getParent();
    while (contenderAncestor != null) {
        if (Objects.equal(contenderAncestor, ancestor)) {
            return true;
        }
        contenderAncestor = contenderAncestor.getParent();
    }
    return false;
}
 
Example 8
Source File: AbstractLocation.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsLocation(Location potentialDescendent) {
    Location loc = potentialDescendent;
    while (loc != null) {
        if (this == loc) return true;
        loc = loc.getParent();
    }
    return false;
}
 
Example 9
Source File: DynamicClusterImpl.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <strong>Note</strong> for sub-classes; this method can be called while synchronized on {@link #mutex}.
 */
@Override
public String replaceMember(String memberId) {
    Entity member = getEntityManager().getEntity(memberId);
    LOG.info("In {}, replacing member {} ({})", new Object[] {this, memberId, member});

    if (member == null) {
        throw new NoSuchElementException("In "+this+", entity "+memberId+" cannot be resolved, so not replacing");
    }

    synchronized (mutex) {
        if (!getMembers().contains(member)) {
            throw new NoSuchElementException("In "+this+", entity "+member+" is not a member so not replacing");
        }

        Location memberLoc = null;
        if (isAvailabilityZoneEnabled()) {
            // this member's location could be a machine provisioned by a sub-location, or the actual sub-location
            List<Location> subLocations = findSubLocations(getLocation(true));
            Collection<Location> actualMemberLocs = member.getLocations();
            boolean foundMatch = false;
            for (Iterator<Location> iter = actualMemberLocs.iterator(); !foundMatch && iter.hasNext();) {
                Location actualMemberLoc = iter.next();
                Location contenderMemberLoc = actualMemberLoc;
                do {
                    if (subLocations.contains(contenderMemberLoc)) {
                        memberLoc = contenderMemberLoc;
                        foundMatch = true;
                        LOG.debug("In {} replacing member {} ({}), inferred its sub-location is {}", new Object[] {this, memberId, member, memberLoc});
                    }
                    contenderMemberLoc = contenderMemberLoc.getParent();
                } while (!foundMatch && contenderMemberLoc != null);
            }
            if (!foundMatch) {
                if (actualMemberLocs.isEmpty()) {
                    memberLoc = subLocations.get(0);
                    LOG.warn("In {} replacing member {} ({}), has no locations; falling back to first availability zone: {}", new Object[] {this, memberId, member, memberLoc});
                } else {
                    memberLoc = Iterables.tryFind(actualMemberLocs, Predicates.instanceOf(MachineProvisioningLocation.class)).or(Iterables.getFirst(actualMemberLocs, null));
                    LOG.warn("In {} replacing member {} ({}), could not find matching sub-location; falling back to its actual location: {}", new Object[] {this, memberId, member, memberLoc});
                }
            } else if (memberLoc == null) {
                // impossible to get here, based on logic above!
                throw new IllegalStateException("Unexpected condition! cluster="+this+"; member="+member+"; actualMemberLocs="+actualMemberLocs);
            }
        } else {
            // Replacing member, so new member should be in the same location as that being replaced.
            // Expect this to agree with `getMemberSpec().getLocations()` (if set). If not, then 
            // presumably there was a reason this specific member was started somewhere else!
            memberLoc = getLocation(false);
        }

        Entity replacement = replaceMember(member, memberLoc, ImmutableMap.of());
        return replacement.getId();
    }
}
 
Example 10
Source File: HostGeoInfo.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
/** returns null if cannot be set */
public static HostGeoInfo fromLocation(Location l) {
    if (l==null) return null;
    
    Location la = l;
    HostGeoInfo resultFromLocation = null;
    while (la!=null) {
        if (la instanceof HasHostGeoInfo) {
            resultFromLocation = ((HasHostGeoInfo)l).getHostGeoInfo();
            if (resultFromLocation!=null) break;
        }
        la = la.getParent();
    }
    if (resultFromLocation!=null && l==la) {
        // from the location
        return resultFromLocation;
    }
    // resultFromLocation may be inherited, in which case we will copy it later
    
    InetAddress address = findIpAddress(l);
    Object latitude = l.getConfig(LocationConfigKeys.LATITUDE);
    Object longitude = l.getConfig(LocationConfigKeys.LONGITUDE);

    if (resultFromLocation!=null && (latitude == null || longitude == null)) {
        latitude = resultFromLocation.latitude;
        longitude = resultFromLocation.longitude;            
    }
    if (address!=null && (latitude == null || longitude == null)) {
        HostGeoInfo geo = fromIpAddress(address);
        if (geo==null) return null;
        latitude = geo.latitude;
        longitude = geo.longitude;
    }
    
    if (latitude==null || longitude==null)
        return null;
    
    Exception error=null;
    try {
        latitude = TypeCoercions.castPrimitive(latitude, Double.class);
        longitude = TypeCoercions.castPrimitive(longitude, Double.class);
    } catch (Exception e) {
        Exceptions.propagateIfFatal(e);
        error = e;
    }
    if (error!=null || !(latitude instanceof Double) || !(longitude instanceof Double))
        throw new IllegalArgumentException("Location "+l+" specifies invalid type of lat/long: " +
                "lat="+latitude+" (type "+(latitude==null ? null : latitude.getClass())+"); " +
                "lon="+longitude+" (type "+(longitude==null ? null : longitude.getClass())+")", error);
    
    HostGeoInfo result = new HostGeoInfo(address!=null ? address.getHostAddress() : null, l.getDisplayName(), (Double) latitude, (Double) longitude);
    if (l instanceof AbstractLocation) {
        ((AbstractLocation)l).setHostGeoInfo(result);
    }
    return result;
}
 
Example 11
Source File: LocationConfigMap.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
protected Location getParentOfContainer(Location container) {
    if (container==null) return null;
    return container.getParent();
}