EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.savvis.vpdc.compute.strategy]

COVERAGE SUMMARY FOR SOURCE FILE [VPDCComputeServiceAdapter.java]

nameclass, %method, %block, %line, %
VPDCComputeServiceAdapter.java0%   (0/2)0%   (0/13)0%   (0/422)0%   (0/59)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class VPDCComputeServiceAdapter0%   (0/1)0%   (0/11)0%   (0/411)0%   (0/58)
VPDCComputeServiceAdapter (VPDCClient, TaskSuccess): void 0%   (0/1)0%   (0/21)0%   (0/4)
createNodeWithGroupEncodedIntoNameThenStoreCredentials (String, String, Templ... 0%   (0/1)0%   (0/139)0%   (0/20)
destroyNode (String): void 0%   (0/1)0%   (0/15)0%   (0/2)
getNode (String): VM 0%   (0/1)0%   (0/16)0%   (0/1)
listHardwareProfiles (): Iterable 0%   (0/1)0%   (0/15)0%   (0/1)
listImages (): Iterable 0%   (0/1)0%   (0/4)0%   (0/1)
listLocations (): Iterable 0%   (0/1)0%   (0/82)0%   (0/12)
listNodes (): Iterable 0%   (0/1)0%   (0/82)0%   (0/10)
rebootNode (String): void 0%   (0/1)0%   (0/7)0%   (0/3)
resumeNode (String): void 0%   (0/1)0%   (0/15)0%   (0/2)
suspendNode (String): void 0%   (0/1)0%   (0/15)0%   (0/2)
     
class VPDCComputeServiceAdapter$10%   (0/1)0%   (0/2)0%   (0/11)0%   (0/2)
VPDCComputeServiceAdapter$1 (VPDCComputeServiceAdapter): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (Resource): boolean 0%   (0/1)0%   (0/5)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.savvis.vpdc.compute.strategy;
21 
22import static com.google.common.base.Preconditions.checkNotNull;
23import static org.jclouds.savvis.vpdc.options.GetVMOptions.Builder.withPowerState;
24import static org.jclouds.savvis.vpdc.reference.VPDCConstants.PROPERTY_VPDC_VDC_EMAIL;
25 
26import java.net.URI;
27import java.util.Map;
28import java.util.concurrent.TimeUnit;
29 
30import javax.inject.Named;
31import javax.inject.Singleton;
32 
33import org.jclouds.compute.ComputeService;
34import org.jclouds.compute.ComputeServiceAdapter;
35import org.jclouds.compute.domain.CIMOperatingSystem;
36import org.jclouds.compute.domain.Template;
37import org.jclouds.compute.domain.Volume;
38import org.jclouds.domain.Credentials;
39import org.jclouds.predicates.RetryablePredicate;
40import org.jclouds.savvis.vpdc.VPDCClient;
41import org.jclouds.savvis.vpdc.domain.Network;
42import org.jclouds.savvis.vpdc.domain.Org;
43import org.jclouds.savvis.vpdc.domain.Resource;
44import org.jclouds.savvis.vpdc.domain.Task;
45import org.jclouds.savvis.vpdc.domain.VDC;
46import org.jclouds.savvis.vpdc.domain.VM;
47import org.jclouds.savvis.vpdc.domain.VMSpec;
48import org.jclouds.savvis.vpdc.predicates.TaskSuccess;
49import org.jclouds.savvis.vpdc.reference.VCloudMediaType;
50 
51import com.google.common.base.Predicate;
52import com.google.common.collect.ImmutableSet;
53import com.google.common.collect.Iterables;
54import com.google.common.collect.ImmutableSet.Builder;
55import com.google.inject.Inject;
56 
57;
58 
59/**
60 * defines the connection between the {@link VPDCClient} implementation and the jclouds
61 * {@link ComputeService}
62 * 
63 */
64@Singleton
65public class VPDCComputeServiceAdapter implements ComputeServiceAdapter<VM, VMSpec, CIMOperatingSystem, Network> {
66   private final VPDCClient client;
67   private final RetryablePredicate<String> taskTester;
68   @Inject(optional = true)
69   @Named(PROPERTY_VPDC_VDC_EMAIL)
70   String email;
71 
72   @Inject
73   public VPDCComputeServiceAdapter(VPDCClient client, TaskSuccess taskSuccess) {
74      this.client = checkNotNull(client, "client");
75      // TODO: parameterize
76      this.taskTester = new RetryablePredicate<String>(checkNotNull(taskSuccess, "taskSuccess"), 650, 10,
77               TimeUnit.SECONDS);
78   }
79 
80   @Override
81   public VM createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template,
82            Map<String, Credentials> credentialStore) {
83      String networkTierName = template.getLocation().getId();
84      String vpdcId = template.getLocation().getParent().getId();
85      String billingSiteId = template.getLocation().getParent().getParent().getId();
86 
87      VMSpec.Builder specBuilder = VMSpec.builder();
88      specBuilder.name(name);
89      specBuilder.networkTierName(networkTierName);
90      specBuilder.operatingSystem(CIMOperatingSystem.class.cast(template.getImage().getOperatingSystem()));
91      specBuilder.processorCount(template.getHardware().getProcessors().size());
92      specBuilder.memoryInGig(template.getHardware().getRam() / 1024);
93 
94      for (Volume volume : template.getHardware().getVolumes()) {
95         if (volume.isBootDevice())
96            specBuilder.bootDeviceName(volume.getDevice()).bootDiskSize(volume.getSize().intValue());
97         else
98            specBuilder.addDataDrive(volume.getDevice(), volume.getSize().intValue());
99      }
100 
101      Task task = client.getVMClient().addVMIntoVDC(billingSiteId, vpdcId, specBuilder.build());
102      // make sure there's no error
103      if (task.getError() != null)
104         throw new RuntimeException("cloud not add vm: " + task.getError().toString());
105 
106      if (taskTester.apply(task.getId())) {
107         try {
108            return this.getNode(task.getResult().getHref().toASCIIString());
109         } finally {
110            // TODO: get the credentials relevant to the billingSiteId/Org
111            // credentialStore.put(id, new Credentials(orgId, orgUser));
112         }
113      } else {
114         throw new RuntimeException("task timed out: " + task);
115      }
116   }
117 
118   @Override
119   public Iterable<VMSpec> listHardwareProfiles() {
120      // TODO don't depend on OS
121      return ImmutableSet.of(VMSpec.builder().operatingSystem(Iterables.get(listImages(), 0)).memoryInGig(2)
122               .addDataDrive("/data01", 25).build());
123   }
124 
125   @Override
126   public Iterable<CIMOperatingSystem> listImages() {
127      return client.listPredefinedOperatingSystems();
128   }
129 
130   @Override
131   public Iterable<VM> listNodes() {
132      Builder<VM> builder = ImmutableSet.<VM> builder();
133      for (Resource org1 : client.listOrgs()) {
134         Org org = client.getBrowsingClient().getOrg(org1.getId());
135         for (Resource vdc : org.getVDCs()) {
136            VDC VDC = client.getBrowsingClient().getVDCInOrg(org.getId(), vdc.getId());
137            for (Resource vApp : Iterables.filter(VDC.getResourceEntities(), new Predicate<Resource>() {
138 
139               @Override
140               public boolean apply(Resource arg0) {
141                  return VCloudMediaType.VAPP_XML.equals(arg0.getType());
142               }
143 
144            })) {
145               builder.add(client.getBrowsingClient().getVMInVDC(org.getId(), vdc.getId(), vApp.getId(),
146                        withPowerState()));
147            }
148         }
149      }
150      return builder.build();
151   }
152 
153   @Override
154   public Iterable<Network> listLocations() {
155      Builder<Network> builder = ImmutableSet.<Network> builder();
156      for (Resource org1 : client.listOrgs()) {
157         Org org = client.getBrowsingClient().getOrg(org1.getId());
158         for (Resource vdc : org.getVDCs()) {
159            VDC VDC = client.getBrowsingClient().getVDCInOrg(org.getId(), vdc.getId());
160            // optionally constrain locations
161            if (email != null && VDC.getDescription().indexOf(email) != -1)
162               continue;
163            for (Resource network : VDC.getAvailableNetworks()) {
164               builder.add(client.getBrowsingClient().getNetworkInVDC(org.getId(), vdc.getId(), network.getId()));
165            }
166         }
167      }
168      return builder.build();
169   }
170 
171   @Override
172   public VM getNode(String id) {
173      return client.getBrowsingClient().getVM(URI.create(checkNotNull(id, "id")), withPowerState());
174   }
175 
176   @Override
177   public void destroyNode(String id) {
178      taskTester.apply(client.getVMClient().removeVM(URI.create(checkNotNull(id, "id"))).getId());
179   }
180 
181   @Override
182   public void rebootNode(String id) {
183      // there is no support for restart in savvis yet
184      suspendNode(id);
185      resumeNode(id);
186   }
187 
188   @Override
189   public void resumeNode(String id) {
190      taskTester.apply(client.getServiceManagementClient().powerOnVM(URI.create(checkNotNull(id, "id"))).getId());
191   }
192 
193   @Override
194   public void suspendNode(String id) {
195      taskTester.apply(client.getServiceManagementClient().powerOffVM(URI.create(checkNotNull(id, "id"))).getId());
196   }
197}

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