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

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