All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclouds.cloudstack.compute.functions.OrphanedGroupsByZoneId Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.jclouds.cloudstack.compute.functions;

import static shaded.com.google.common.base.Preconditions.checkNotNull;
import static shaded.com.google.common.collect.Iterables.filter;
import static shaded.com.google.common.collect.Iterables.transform;
import static shaded.com.google.common.collect.Sets.filter;

import java.util.Set;

import javax.inject.Inject;

import org.jclouds.compute.ComputeService;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.predicates.NodePredicates;
import org.jclouds.domain.LocationScope;
import org.jclouds.cloudstack.compute.predicates.AllNodesInGroupTerminated;
import org.jclouds.cloudstack.domain.ZoneAndName;

import shaded.com.google.common.annotations.VisibleForTesting;
import shaded.com.google.common.base.Function;
import shaded.com.google.common.base.Predicate;
import shaded.com.google.common.collect.ImmutableSet;
import shaded.com.google.common.collect.Multimap;
import shaded.com.google.common.collect.Multimaps;

public class OrphanedGroupsByZoneId implements Function, Multimap> {
   private final Predicate allNodesInGroupTerminated;

   @Inject
   protected OrphanedGroupsByZoneId(ComputeService computeService) {
      this(new AllNodesInGroupTerminated(checkNotNull(computeService, "computeService")));
   }

   @VisibleForTesting
   OrphanedGroupsByZoneId(Predicate allNodesInGroupTerminated) {
      this.allNodesInGroupTerminated = checkNotNull(allNodesInGroupTerminated, "allNodesInGroupTerminated");
   }

   public Multimap apply(Set deadNodes) {
      Iterable nodesWithGroup = filter(deadNodes, NodePredicates.hasGroup());
      Set zoneAndGroupNames = ImmutableSet.copyOf(filter(transform(nodesWithGroup,
               new Function() {

                  @Override
                  public ZoneAndName apply(NodeMetadata input) {
                     String zoneId = input.getLocation().getScope() == LocationScope.HOST ? input.getLocation()
                              .getParent().getId() : input.getLocation().getId();
                     return ZoneAndName.fromZoneAndName(zoneId, input.getGroup());
                  }

               }), allNodesInGroupTerminated));
      Multimap zoneToZoneAndGroupNames = Multimaps.transformValues(Multimaps.index(zoneAndGroupNames,
               ZoneAndName.ZONE_FUNCTION), ZoneAndName.NAME_FUNCTION);
      return zoneToZoneAndGroupNames;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy