comparison OrthancServer/ServerJobs/StorageCommitmentScpJob.cpp @ 3786:3801435e34a1 SylvainRouquette/fix-issue169-95b752c

integration Orthanc-1.6.0->SylvainRouquette
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Mar 2020 11:48:30 +0100
parents 5ff5d5a0fd28
children 138d0dde41b5
comparison
equal deleted inserted replaced
3785:763533d6dd67 3786:3801435e34a1
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeadersServer.h"
35 #include "StorageCommitmentScpJob.h"
36
37 #include "../../Core/DicomNetworking/DicomUserConnection.h"
38 #include "../../Core/Logging.h"
39 #include "../../Core/OrthancException.h"
40 #include "../../Core/SerializationToolbox.h"
41 #include "../OrthancConfiguration.h"
42 #include "../ServerContext.h"
43
44
45 static const char* ANSWER = "Answer";
46 static const char* CALLED_AET = "CalledAet";
47 static const char* INDEX = "Index";
48 static const char* LOOKUP = "Lookup";
49 static const char* REMOTE_MODALITY = "RemoteModality";
50 static const char* SETUP = "Setup";
51 static const char* SOP_CLASS_UIDS = "SopClassUids";
52 static const char* SOP_INSTANCE_UIDS = "SopInstanceUids";
53 static const char* TRANSACTION_UID = "TransactionUid";
54 static const char* TYPE = "Type";
55
56
57
58 namespace Orthanc
59 {
60 class StorageCommitmentScpJob::StorageCommitmentCommand : public SetOfCommandsJob::ICommand
61 {
62 public:
63 virtual CommandType GetType() const = 0;
64 };
65
66
67 class StorageCommitmentScpJob::SetupCommand : public StorageCommitmentCommand
68 {
69 private:
70 StorageCommitmentScpJob& that_;
71
72 public:
73 SetupCommand(StorageCommitmentScpJob& that) :
74 that_(that)
75 {
76 }
77
78 virtual CommandType GetType() const ORTHANC_OVERRIDE
79 {
80 return CommandType_Setup;
81 }
82
83 virtual bool Execute(const std::string& jobId) ORTHANC_OVERRIDE
84 {
85 that_.Setup(jobId);
86 return true;
87 }
88
89 virtual void Serialize(Json::Value& target) const ORTHANC_OVERRIDE
90 {
91 target = Json::objectValue;
92 target[TYPE] = SETUP;
93 }
94 };
95
96
97 class StorageCommitmentScpJob::LookupCommand : public StorageCommitmentCommand
98 {
99 private:
100 StorageCommitmentScpJob& that_;
101 size_t index_;
102 bool hasFailureReason_;
103 StorageCommitmentFailureReason failureReason_;
104
105 public:
106 LookupCommand(StorageCommitmentScpJob& that,
107 size_t index) :
108 that_(that),
109 index_(index),
110 hasFailureReason_(false)
111 {
112 }
113
114 virtual CommandType GetType() const ORTHANC_OVERRIDE
115 {
116 return CommandType_Lookup;
117 }
118
119 virtual bool Execute(const std::string& jobId) ORTHANC_OVERRIDE
120 {
121 failureReason_ = that_.Lookup(index_);
122 hasFailureReason_ = true;
123 return true;
124 }
125
126 size_t GetIndex() const
127 {
128 return index_;
129 }
130
131 StorageCommitmentFailureReason GetFailureReason() const
132 {
133 if (hasFailureReason_)
134 {
135 return failureReason_;
136 }
137 else
138 {
139 throw OrthancException(ErrorCode_BadSequenceOfCalls);
140 }
141 }
142
143 virtual void Serialize(Json::Value& target) const ORTHANC_OVERRIDE
144 {
145 target = Json::objectValue;
146 target[TYPE] = LOOKUP;
147 target[INDEX] = static_cast<unsigned int>(index_);
148 }
149 };
150
151
152 class StorageCommitmentScpJob::AnswerCommand : public StorageCommitmentCommand
153 {
154 private:
155 StorageCommitmentScpJob& that_;
156
157 public:
158 AnswerCommand(StorageCommitmentScpJob& that) :
159 that_(that)
160 {
161 if (that_.ready_)
162 {
163 throw OrthancException(ErrorCode_BadSequenceOfCalls);
164 }
165 else
166 {
167 that_.ready_ = true;
168 }
169 }
170
171 virtual CommandType GetType() const ORTHANC_OVERRIDE
172 {
173 return CommandType_Answer;
174 }
175
176 virtual bool Execute(const std::string& jobId) ORTHANC_OVERRIDE
177 {
178 that_.Answer();
179 return true;
180 }
181
182 virtual void Serialize(Json::Value& target) const ORTHANC_OVERRIDE
183 {
184 target = Json::objectValue;
185 target[TYPE] = ANSWER;
186 }
187 };
188
189
190 class StorageCommitmentScpJob::Unserializer : public SetOfCommandsJob::ICommandUnserializer
191 {
192 private:
193 StorageCommitmentScpJob& that_;
194
195 public:
196 Unserializer(StorageCommitmentScpJob& that) :
197 that_(that)
198 {
199 that_.ready_ = false;
200 }
201
202 virtual ICommand* Unserialize(const Json::Value& source) const
203 {
204 const std::string type = SerializationToolbox::ReadString(source, TYPE);
205
206 if (type == SETUP)
207 {
208 return new SetupCommand(that_);
209 }
210 else if (type == LOOKUP)
211 {
212 return new LookupCommand(that_, SerializationToolbox::ReadUnsignedInteger(source, INDEX));
213 }
214 else if (type == ANSWER)
215 {
216 return new AnswerCommand(that_);
217 }
218 else
219 {
220 throw OrthancException(ErrorCode_BadFileFormat);
221 }
222 }
223 };
224
225
226 void StorageCommitmentScpJob::CheckInvariants()
227 {
228 const size_t n = GetCommandsCount();
229
230 if (n <= 1)
231 {
232 throw OrthancException(ErrorCode_InternalError);
233 }
234
235 for (size_t i = 0; i < n; i++)
236 {
237 const CommandType type = dynamic_cast<const StorageCommitmentCommand&>(GetCommand(i)).GetType();
238
239 if ((i == 0 && type != CommandType_Setup) ||
240 (i >= 1 && i < n - 1 && type != CommandType_Lookup) ||
241 (i == n - 1 && type != CommandType_Answer))
242 {
243 throw OrthancException(ErrorCode_InternalError);
244 }
245
246 if (type == CommandType_Lookup)
247 {
248 const LookupCommand& lookup = dynamic_cast<const LookupCommand&>(GetCommand(i));
249 if (lookup.GetIndex() != i - 1)
250 {
251 throw OrthancException(ErrorCode_InternalError);
252 }
253 }
254 }
255 }
256
257
258 void StorageCommitmentScpJob::Setup(const std::string& jobId)
259 {
260 CheckInvariants();
261
262 const std::string& remoteAet = remoteModality_.GetApplicationEntityTitle();
263 lookupHandler_.reset(context_.CreateStorageCommitment(jobId, transactionUid_, sopClassUids_,
264 sopInstanceUids_, remoteAet, calledAet_));
265 }
266
267
268 StorageCommitmentFailureReason StorageCommitmentScpJob::Lookup(size_t index)
269 {
270 #ifndef NDEBUG
271 CheckInvariants();
272 #endif
273
274 if (index >= sopClassUids_.size())
275 {
276 throw OrthancException(ErrorCode_InternalError);
277 }
278 else if (lookupHandler_.get() != NULL)
279 {
280 return lookupHandler_->Lookup(sopClassUids_[index], sopInstanceUids_[index]);
281 }
282 else
283 {
284 // This is the default implementation of Orthanc (if no storage
285 // commitment plugin is installed)
286 bool success = false;
287 StorageCommitmentFailureReason reason =
288 StorageCommitmentFailureReason_NoSuchObjectInstance /* 0x0112 == 274 */;
289
290 try
291 {
292 std::vector<std::string> orthancId;
293 context_.GetIndex().LookupIdentifierExact(orthancId, ResourceType_Instance, DICOM_TAG_SOP_INSTANCE_UID, sopInstanceUids_[index]);
294
295 if (orthancId.size() == 1)
296 {
297 std::string a, b;
298
299 // Make sure that the DICOM file can be re-read by DCMTK
300 // from the file storage, and that the actual SOP
301 // class/instance UIDs do match
302 ServerContext::DicomCacheLocker locker(context_, orthancId[0]);
303 if (locker.GetDicom().GetTagValue(a, DICOM_TAG_SOP_CLASS_UID) &&
304 locker.GetDicom().GetTagValue(b, DICOM_TAG_SOP_INSTANCE_UID) &&
305 b == sopInstanceUids_[index])
306 {
307 if (a == sopClassUids_[index])
308 {
309 success = true;
310 reason = StorageCommitmentFailureReason_Success;
311 }
312 else
313 {
314 // Mismatch in the SOP class UID
315 reason = StorageCommitmentFailureReason_ClassInstanceConflict /* 0x0119 */;
316 }
317 }
318 }
319 }
320 catch (OrthancException&)
321 {
322 }
323
324 LOG(INFO) << " Storage commitment SCP job: " << (success ? "Success" : "Failure")
325 << " while looking for " << sopClassUids_[index] << " / " << sopInstanceUids_[index];
326
327 return reason;
328 }
329 }
330
331
332 void StorageCommitmentScpJob::Answer()
333 {
334 CheckInvariants();
335 LOG(INFO) << " Storage commitment SCP job: Sending answer";
336
337 std::vector<StorageCommitmentFailureReason> failureReasons;
338 failureReasons.reserve(sopClassUids_.size());
339
340 for (size_t i = 1; i < GetCommandsCount() - 1; i++)
341 {
342 const LookupCommand& lookup = dynamic_cast<const LookupCommand&>(GetCommand(i));
343 failureReasons.push_back(lookup.GetFailureReason());
344 }
345
346 if (failureReasons.size() != sopClassUids_.size())
347 {
348 throw OrthancException(ErrorCode_InternalError);
349 }
350
351 DicomUserConnection scu(calledAet_, remoteModality_);
352 scu.ReportStorageCommitment(transactionUid_, sopClassUids_, sopInstanceUids_, failureReasons);
353 }
354
355
356 StorageCommitmentScpJob::StorageCommitmentScpJob(ServerContext& context,
357 const std::string& transactionUid,
358 const std::string& remoteAet,
359 const std::string& calledAet) :
360 context_(context),
361 ready_(false),
362 transactionUid_(transactionUid),
363 calledAet_(calledAet)
364 {
365 {
366 OrthancConfiguration::ReaderLock lock;
367 if (!lock.GetConfiguration().LookupDicomModalityUsingAETitle(remoteModality_, remoteAet))
368 {
369 throw OrthancException(ErrorCode_InexistentItem,
370 "Unknown remote modality for storage commitment SCP: " + remoteAet);
371 }
372 }
373
374 AddCommand(new SetupCommand(*this));
375 }
376
377
378 void StorageCommitmentScpJob::Reserve(size_t size)
379 {
380 if (ready_)
381 {
382 throw OrthancException(ErrorCode_BadSequenceOfCalls);
383 }
384 else
385 {
386 sopClassUids_.reserve(size);
387 sopInstanceUids_.reserve(size);
388 }
389 }
390
391
392 void StorageCommitmentScpJob::AddInstance(const std::string& sopClassUid,
393 const std::string& sopInstanceUid)
394 {
395 if (ready_)
396 {
397 throw OrthancException(ErrorCode_BadSequenceOfCalls);
398 }
399 else
400 {
401 assert(sopClassUids_.size() == sopInstanceUids_.size());
402 AddCommand(new LookupCommand(*this, sopClassUids_.size()));
403 sopClassUids_.push_back(sopClassUid);
404 sopInstanceUids_.push_back(sopInstanceUid);
405 }
406 }
407
408
409 void StorageCommitmentScpJob::MarkAsReady()
410 {
411 AddCommand(new AnswerCommand(*this));
412 }
413
414
415 void StorageCommitmentScpJob::GetPublicContent(Json::Value& value)
416 {
417 SetOfCommandsJob::GetPublicContent(value);
418
419 value["CalledAet"] = calledAet_;
420 value["RemoteAet"] = remoteModality_.GetApplicationEntityTitle();
421 value["TransactionUid"] = transactionUid_;
422 }
423
424
425 StorageCommitmentScpJob::StorageCommitmentScpJob(ServerContext& context,
426 const Json::Value& serialized) :
427 SetOfCommandsJob(new Unserializer(*this), serialized),
428 context_(context)
429 {
430 transactionUid_ = SerializationToolbox::ReadString(serialized, TRANSACTION_UID);
431 remoteModality_ = RemoteModalityParameters(serialized[REMOTE_MODALITY]);
432 calledAet_ = SerializationToolbox::ReadString(serialized, CALLED_AET);
433 SerializationToolbox::ReadArrayOfStrings(sopClassUids_, serialized, SOP_CLASS_UIDS);
434 SerializationToolbox::ReadArrayOfStrings(sopInstanceUids_, serialized, SOP_INSTANCE_UIDS);
435 }
436
437
438 bool StorageCommitmentScpJob::Serialize(Json::Value& target)
439 {
440 if (!SetOfCommandsJob::Serialize(target))
441 {
442 return false;
443 }
444 else
445 {
446 target[TRANSACTION_UID] = transactionUid_;
447 remoteModality_.Serialize(target[REMOTE_MODALITY], true /* force advanced format */);
448 target[CALLED_AET] = calledAet_;
449 SerializationToolbox::WriteArrayOfStrings(target, sopClassUids_, SOP_CLASS_UIDS);
450 SerializationToolbox::WriteArrayOfStrings(target, sopInstanceUids_, SOP_INSTANCE_UIDS);
451 return true;
452 }
453 }
454 }