EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.deltacloud.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [DeltacloudComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
DeltacloudComputeServiceAdapter.java0%   (0/1)0%   (0/12)0%   (0/251)0%   (0/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DeltacloudComputeServiceAdapter0%   (0/1)0%   (0/12)0%   (0/251)0%   (0/45)
DeltacloudComputeServiceAdapter (DeltacloudClient): void 0%   (0/1)0%   (0/37)0%   (0/5)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/45)0%   (0/5)
destroyNode (String): void 0%   (0/1)0%   (0/72)0%   (0/13)
findChainTo (Instance$State, Instance$State, Multimap): Iterable 0%   (0/1)0%   (0/44)0%   (0/10)
getNode (String): Instance 0%   (0/1)0%   (0/9)0%   (0/1)
listHardwareProfiles (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
listImages (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
listLocations (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
listNodes (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
rebootNode (String): void 0%   (0/1)0%   (0/20)0%   (0/5)
resumeNode (String): void 0%   (0/1)0%   (0/4)0%   (0/1)
suspendNode (String): void 0%   (0/1)0%   (0/4)0%   (0/1)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.deltacloud.compute.strategy;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.net.URI;
24import java.util.Map;
25import java.util.concurrent.TimeUnit;
26 
27import javax.annotation.Resource;
28import javax.inject.Inject;
29import javax.inject.Named;
30import javax.inject.Singleton;
31 
32import org.jclouds.compute.ComputeService;
33import org.jclouds.compute.ComputeServiceAdapter;
34import org.jclouds.compute.domain.Template;
35import org.jclouds.compute.reference.ComputeServiceConstants;
36import org.jclouds.deltacloud.DeltacloudClient;
37import org.jclouds.deltacloud.domain.HardwareProfile;
38import org.jclouds.deltacloud.domain.Instance;
39import org.jclouds.deltacloud.domain.PasswordAuthentication;
40import org.jclouds.deltacloud.domain.Realm;
41import org.jclouds.deltacloud.domain.Transition;
42import org.jclouds.deltacloud.domain.TransitionOnAction;
43import org.jclouds.deltacloud.domain.Instance.State;
44import org.jclouds.deltacloud.options.CreateInstanceOptions;
45import org.jclouds.deltacloud.predicates.InstanceFinished;
46import org.jclouds.deltacloud.predicates.InstanceRunning;
47import org.jclouds.domain.Credentials;
48import org.jclouds.http.HttpRequest;
49import org.jclouds.logging.Logger;
50import org.jclouds.predicates.RetryablePredicate;
51 
52import com.google.common.base.Predicate;
53import com.google.common.collect.ImmutableMap;
54import com.google.common.collect.ImmutableSet;
55import com.google.common.collect.Iterables;
56import com.google.common.collect.Multimap;
57 
58/**
59 * defines the connection between the {@link DeltacloudClient} implementation and the jclouds
60 * {@link ComputeService}
61 * 
62 */
63@Singleton
64public class DeltacloudComputeServiceAdapter implements
65         ComputeServiceAdapter<Instance, HardwareProfile, org.jclouds.deltacloud.domain.Image, Realm> {
66   @Resource
67   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
68   protected Logger logger = Logger.NULL;
69 
70   private final org.jclouds.deltacloud.DeltacloudClient client;
71   private final ImmutableMap<State, Predicate<Instance>> stateChanges;
72 
73   @Inject
74   public DeltacloudComputeServiceAdapter(DeltacloudClient client) {
75      this.client = checkNotNull(client, "client");
76      // TODO: parameterize
77      stateChanges = ImmutableMap.<Instance.State, Predicate<Instance>> of(//
78               Instance.State.RUNNING, new RetryablePredicate<Instance>(new InstanceRunning(client), 600, 1,
79                        TimeUnit.SECONDS),//
80               Instance.State.FINISH, new RetryablePredicate<Instance>(new InstanceFinished(client), 30, 1,
81                        TimeUnit.SECONDS)//
82               );
83   }
84 
85   @Override
86   public Instance createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template,
87            Map<String, Credentials> credentialStore) {
88      Instance instance = client.createInstance(template.getImage().getProviderId(), CreateInstanceOptions.Builder
89               .named(name).hardwareProfile(template.getHardware().getId()).realm(template.getLocation().getId()));
90      if (instance.getAuthentication() != null && instance.getAuthentication() instanceof PasswordAuthentication) {
91         Credentials creds = PasswordAuthentication.class.cast(instance.getAuthentication()).getLoginCredentials();
92         // store the credentials so that later functions can use them
93         credentialStore.put(instance.getHref().toASCIIString(), creds);
94      }
95      return instance;
96   }
97 
98   @Override
99   public Iterable<HardwareProfile> listHardwareProfiles() {
100      return client.listHardwareProfiles();
101   }
102 
103   @Override
104   public Iterable<org.jclouds.deltacloud.domain.Image> listImages() {
105      return client.listImages();
106   }
107 
108   @Override
109   public Iterable<Instance> listNodes() {
110      return client.listInstances();
111   }
112 
113   @Override
114   public Iterable<Realm> listLocations() {
115      return client.listRealms();
116   }
117 
118   @Override
119   public org.jclouds.deltacloud.domain.Instance getNode(String id) {
120      return client.getInstance(URI.create(checkNotNull(id, "id")));
121   }
122 
123   @Override
124   public void destroyNode(String id) {
125      Instance instance = getNode(id);
126      for (Transition transition : findChainTo(Instance.State.FINISH, instance.getState(), client.getInstanceStates())) {
127         instance = getNode(id);
128         if (instance == null)
129            break;
130         if (transition instanceof TransitionOnAction) {
131            client.performAction(instance.getActions().get(TransitionOnAction.class.cast(transition).getAction()));
132         }
133         Predicate<Instance> stateTester = stateChanges.get(transition.getTo());
134         if (stateTester != null)
135            stateTester.apply(instance);
136         else
137            logger.debug(String.format("no state tester for: %s", transition));
138      }
139   }
140 
141   Iterable<Transition> findChainTo(Instance.State desired, Instance.State currentState,
142            Multimap<Instance.State, ? extends Transition> states) {
143      for (Transition transition : states.get(currentState)) {
144         if (currentState.ordinal() >= transition.getTo().ordinal())
145            continue;
146         if (transition.getTo() == desired)
147            return ImmutableSet.<Transition> of(transition);
148         Iterable<Transition> transitions = findChainTo(desired, transition.getTo(), states);
149         if (Iterables.size(transitions) > 0)
150            return Iterables.concat(ImmutableSet.of(transition), transitions);
151      }
152      return ImmutableSet.<Transition> of();
153   }
154 
155   @Override
156   public void rebootNode(String id) {
157      HttpRequest rebootUri = getNode(id).getActions().get(Instance.Action.REBOOT);
158      if (rebootUri != null) {
159         client.performAction(rebootUri);
160      } else {
161         throw new UnsupportedOperationException();
162      }
163   }
164 
165   @Override
166   public void resumeNode(String id) {
167      throw new UnsupportedOperationException();
168   }
169 
170   @Override
171   public void suspendNode(String id) {
172      throw new UnsupportedOperationException();
173   }
174}

[all classes][org.jclouds.deltacloud.compute.strategy]
EMMA 2.0.5312 (C) Vladimir Roubtsov