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

COVERAGE SUMMARY FOR SOURCE FILE [VCloudDestroyNodeStrategy.java]

nameclass, %method, %block, %line, %
VCloudDestroyNodeStrategy.java0%   (0/1)0%   (0/6)0%   (0/215)0%   (0/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class VCloudDestroyNodeStrategy0%   (0/1)0%   (0/6)0%   (0/215)0%   (0/36)
VCloudDestroyNodeStrategy (Predicate, VCloudClient, GetNodeMetadataStrategy):... 0%   (0/1)0%   (0/15)0%   (0/6)
deleteVApp (VApp): void 0%   (0/1)0%   (0/32)0%   (0/4)
destroyNode (String): NodeMetadata 0%   (0/1)0%   (0/49)0%   (0/11)
undeployVAppIfDeployed (VApp): VApp 0%   (0/1)0%   (0/73)0%   (0/9)
waitForPendingTasksToComplete (VApp): void 0%   (0/1)0%   (0/17)0%   (0/3)
waitForTask (Task, VApp): void 0%   (0/1)0%   (0/29)0%   (0/3)

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.vcloud.compute.strategy;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.net.URI;
24 
25import javax.annotation.Resource;
26import javax.inject.Inject;
27import javax.inject.Named;
28import javax.inject.Singleton;
29 
30import org.jclouds.compute.domain.NodeMetadata;
31import org.jclouds.compute.reference.ComputeServiceConstants;
32import org.jclouds.compute.strategy.DestroyNodeStrategy;
33import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
34import org.jclouds.logging.Logger;
35import org.jclouds.rest.AuthorizationException;
36import org.jclouds.vcloud.VCloudClient;
37import org.jclouds.vcloud.domain.Status;
38import org.jclouds.vcloud.domain.Task;
39import org.jclouds.vcloud.domain.VApp;
40 
41import com.google.common.base.Predicate;
42 
43/**
44 * @author Adrian Cole
45 */
46@Singleton
47public class VCloudDestroyNodeStrategy implements DestroyNodeStrategy {
48   @Resource
49   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
50   protected Logger logger = Logger.NULL;
51 
52   protected final Predicate<URI> successTester;
53   protected final VCloudClient client;
54   protected final GetNodeMetadataStrategy getNode;
55 
56   @Inject
57   protected VCloudDestroyNodeStrategy(Predicate<URI> successTester, VCloudClient client,
58            GetNodeMetadataStrategy getNode) {
59      this.successTester = successTester;
60      this.client = client;
61      this.getNode = getNode;
62   }
63 
64   @Override
65   public NodeMetadata destroyNode(String id) {
66      URI vappId = URI.create(checkNotNull(id, "node.id"));
67      VApp vApp = client.getVAppClient().getVApp(vappId);
68      if (vApp == null)
69         return null;
70 
71      waitForPendingTasksToComplete(vApp);
72 
73      vApp = undeployVAppIfDeployed(vApp);
74      deleteVApp(vApp);
75      try {
76         return getNode.getNode(id);
77      } catch (AuthorizationException e) {
78         // vcloud bug will sometimes throw an exception getting the vapp right after deleting it.
79         logger.trace("authorization error getting %s after deletion: %s", id, e.getMessage());
80         return null;
81      }
82   }
83 
84   void waitForPendingTasksToComplete(VApp vApp) {
85      for (Task task : vApp.getTasks())
86         waitForTask(task, vApp);
87   }
88 
89   public void waitForTask(Task task, VApp vAppResponse) {
90      if (!successTester.apply(task.getHref())) {
91         throw new RuntimeException(String.format("failed to %s %s: %s", task.getName(), vAppResponse.getName(), task));
92      }
93   }
94 
95   void deleteVApp(VApp vApp) {
96      logger.debug(">> deleting vApp(%s)", vApp.getHref());
97      waitForTask(client.getVAppClient().deleteVApp(vApp.getHref()), vApp);
98      logger.debug("<< deleted vApp(%s)", vApp.getHref());
99   }
100 
101   VApp undeployVAppIfDeployed(VApp vApp) {
102      if (vApp.getStatus() != Status.OFF) {
103         logger.debug(">> undeploying vApp(%s), current status: %s", vApp.getName(), vApp.getStatus());
104         try {
105            waitForTask(client.getVAppClient().undeployVApp(vApp.getHref()), vApp);
106            vApp = client.getVAppClient().getVApp(vApp.getHref());
107            logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getName());
108         } catch (IllegalStateException e) {
109            logger.warn(e, "<< %s vApp(%s)", vApp.getStatus(), vApp.getName());
110         }
111      }
112      return vApp;
113   }
114}

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