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

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