forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpNfcLeaseMO.java
More file actions
executable file
·177 lines (148 loc) · 5.97 KB
/
HttpNfcLeaseMO.java
File metadata and controls
executable file
·177 lines (148 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.hypervisor.vmware.mo;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import com.vmware.vim25.HttpNfcLeaseInfo;
import com.vmware.vim25.HttpNfcLeaseManifestEntry;
import com.vmware.vim25.HttpNfcLeaseState;
import com.vmware.vim25.LocalizedMethodFault;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.OvfCreateImportSpecResult;
import com.vmware.vim25.OvfFileItem;
import com.cloud.hypervisor.vmware.util.VmwareContext;
public class HttpNfcLeaseMO extends BaseMO {
private static final Logger s_logger = Logger.getLogger(HttpNfcLeaseMO.class);
public HttpNfcLeaseMO(VmwareContext context, ManagedObjectReference morHttpNfcLease) {
super(context, morHttpNfcLease);
}
public HttpNfcLeaseMO(VmwareContext context, String morType, String morValue) {
super(context, morType, morValue);
}
public HttpNfcLeaseState getState() throws Exception {
Object stateProp = _context.getVimClient().getDynamicProperty(_mor, "state");
// Due to some issue in JAX-WS De-serialization getting the information
// from the nodes
assert (stateProp.toString().contains("val: null"));
String stateVal = null;
Element stateElement = (Element)stateProp;
if (stateElement != null && stateElement.getFirstChild() != null) {
stateVal = stateElement.getFirstChild().getTextContent();
}
if (stateVal != null) {
return HttpNfcLeaseState.fromValue(stateVal);
}
return HttpNfcLeaseState.ERROR;
}
public HttpNfcLeaseState waitState(HttpNfcLeaseState[] states) throws Exception {
assert (states != null);
assert (states.length > 0);
HttpNfcLeaseState state;
while (true) {
state = getState();
if (state == HttpNfcLeaseState.READY || state == HttpNfcLeaseState.ERROR)
return state;
}
}
public HttpNfcLeaseInfo getLeaseInfo() throws Exception {
return (HttpNfcLeaseInfo)_context.getVimClient().getDynamicProperty(_mor, "info");
}
public LocalizedMethodFault getLeaseError() throws Exception {
return (LocalizedMethodFault)_context.getVimClient().getDynamicProperty(_mor, "error");
}
public List<HttpNfcLeaseManifestEntry> getLeaseManifest() throws Exception {
return _context.getService().httpNfcLeaseGetManifest(_mor);
}
public void completeLease() throws Exception {
_context.getService().httpNfcLeaseComplete(_mor);
}
public void abortLease() throws Exception {
_context.getService().httpNfcLeaseAbort(_mor, null);
}
public void updateLeaseProgress(int percent) throws Exception {
// make sure percentage is in right range
if (percent < 0)
percent = 0;
else if (percent > 100)
percent = 100;
_context.getService().httpNfcLeaseProgress(_mor, percent);
}
public ProgressReporter createProgressReporter() {
return new ProgressReporter();
}
public static long calcTotalBytes(OvfCreateImportSpecResult ovfImportResult) {
List<OvfFileItem> fileItemArr = ovfImportResult.getFileItem();
long totalBytes = 0;
if (fileItemArr != null) {
for (OvfFileItem fi : fileItemArr) {
totalBytes += fi.getSize();
}
}
return totalBytes;
}
public static String readOvfContent(String ovfFilePath) throws IOException {
StringBuffer strContent = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(ovfFilePath)));
String lineStr;
while ((lineStr = in.readLine()) != null) {
strContent.append(lineStr);
}
in.close();
return strContent.toString();
}
public class ProgressReporter extends Thread {
volatile int _percent;
volatile boolean _done;
public ProgressReporter() {
_percent = 0;
_done = false;
setDaemon(true);
start();
}
public void reportProgress(int percent) {
_percent = percent;
}
public void close() {
if (s_logger.isInfoEnabled())
s_logger.info("close ProgressReporter, interrupt reporter runner to let it quit");
_done = true;
interrupt();
}
@Override
public void run() {
while (!_done) {
try {
Thread.sleep(1000); // update progess every 1 second
updateLeaseProgress(_percent);
} catch (InterruptedException e) {
if (s_logger.isInfoEnabled())
s_logger.info("ProgressReporter is interrupted, quiting");
break;
} catch (Exception e) {
s_logger.warn("Unexpected exception ", e);
}
}
if (s_logger.isInfoEnabled())
s_logger.info("ProgressReporter stopped");
}
}
}