bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
Granule.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of cmr_module, A C++ MODULE that can be loaded in to
4// the OPeNDAP Back-End Server (BES) and is able to handle remote requests.
5
6// Copyright (c) 2015 OPeNDAP, Inc.
7// Author: Nathan Potter <ndp@opendap.org>
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Lesser General Public
11// License as published by the Free Software Foundation; either
12// version 2.1 of the License, or (at your option) any later version.
13//
14// This library is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17// Lesser General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public
20// License along with this library; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22//
23// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24
25/*
26 * Granule.h
27 *
28 * Created on: July, 13 2018
29 * Author: ndp
30 */
31#include "config.h"
32
33#include <cstdlib> /* atol */
34#include <sstream>
35
36#include "JsonUtils.h"
37#include "BESDebug.h"
38
39#include "CmrNames.h"
40#include "CmrInternalError.h"
41#include "CmrNotFoundError.h"
42#include "Granule.h"
43#include "CmrApi.h"
44
45
46using namespace std;
47
48#define prolog std::string("Granule::").append(__func__).append("() - ")
49
50
51namespace cmr {
145Granule::Granule(const nlohmann::json& granule_json)
146{
147 setId(granule_json);
148 setName(granule_json);
149 setSize(granule_json);
150 setDapServiceUrl(granule_json);
151 setDataGranuleUrl(granule_json);
152 setMetadataAccessUrl(granule_json);
153 setLastModifiedStr(granule_json);
154}
155
156
157void Granule::setName(const nlohmann::json& granule_json)
158{
159 JsonUtils json;
160 d_name = json.get_str_if_present(CMR_V2_TITLE_KEY, granule_json);
161}
162
163
164void Granule::setId(const nlohmann::json& granule_json)
165{
166 JsonUtils json;
167 d_id = json.get_str_if_present(CMR_GRANULE_ID_KEY, granule_json);
168}
169
170
171void Granule::setSize(const nlohmann::json& granule_json)
172{
173 JsonUtils json;
174 d_size_str = json.get_str_if_present(CMR_GRANULE_SIZE_KEY, granule_json);
175}
176
177
182void Granule::setLastModifiedStr(const nlohmann::json& granule_json)
183{
184 JsonUtils json;
185 d_last_modified_time = json.get_str_if_present(CMR_GRANULE_LMT_KEY, granule_json);
186}
187
188
192const nlohmann::json& Granule::get_links_array(const nlohmann::json& granule_json) const
193{
194 JsonUtils json;
195 return json.qc_get_array(CMR_GRANULE_LINKS_KEY, granule_json);
196}
197
198
199
203void Granule::setDataGranuleUrl(const nlohmann::json& granule_json)
204{
205 const auto& links = get_links_array(granule_json);
206 for(auto &link : links){
207 string rel = link[CMR_GRANULE_LINKS_REL].get<string>();
208 if(rel == CMR_GRANULE_LINKS_REL_DATA_ACCESS){
209 d_data_access_url = link[CMR_GRANULE_LINKS_HREF];
210 return;
211 }
212 }
213 stringstream msg;
214 msg << "ERROR: Failed to locate granule data access link (";
215 msg << CMR_GRANULE_LINKS_REL_DATA_ACCESS << "), :(";
216 throw CmrInternalError(msg.str(), __FILE__, __LINE__);
217}
218
222void Granule::setDapServiceUrl(const nlohmann::json& granule_json)
223{
224 JsonUtils json;
225 BESDEBUG(MODULE, prolog << "JSON: " << endl << granule_json.dump(4) << endl);
226 const auto& links = get_links_array(granule_json);
227 for(auto &link : links){
228 string rel = json.get_str_if_present(CMR_GRANULE_LINKS_REL,link);
229 if (rel == CMR_GRANULE_LINKS_REL_SERVICE) {
230 // Check the service link title to see itf it's an OPeNDAP thing
231 string title = json.get_str_if_present(CMR_V2_TITLE_KEY,link);
232 // change to upper case
233 transform(title.begin(), title.end(), title.begin(), ::toupper);
234 if (title.find("OPENDAP") != string::npos) {
235 // Ooh! It's an OPeNDAP service link.
236 d_dap_service_url = json.get_str_if_present(CMR_GRANULE_LINKS_HREF, link);
237 return;
238 }
239 }
240 }
241 stringstream msg;
242 msg << "Failed to locate DAP service link (";
243 msg << CMR_GRANULE_LINKS_REL_SERVICE << "), :(";
244 BESDEBUG(MODULE, prolog << msg.str() << endl);
245}
246
247
251void Granule::setMetadataAccessUrl(const nlohmann::json& granule_json)
252{
253 const auto &links = get_links_array(granule_json);
254 for(auto &link : links){
255 string rel = link[CMR_GRANULE_LINKS_REL].get<string>();
256 if(rel == CMR_GRANULE_LINKS_REL_METADATA_ACCESS){
257 d_metadata_access_url = link[CMR_GRANULE_LINKS_HREF].get<string>();
258 return;
259 }
260 }
261 stringstream msg;
262 msg << "ERROR: Failed to locate granule metadata access link (";
263 msg << CMR_GRANULE_LINKS_REL_METADATA_ACCESS << "), :(";
264 throw CmrInternalError(msg.str(), __FILE__, __LINE__);
265}
266
267size_t Granule::getSize() const {
268 double value = strtod(getSizeStr().c_str(), nullptr);
269 value *= 1024*1204;
270 return static_cast<size_t>(value);
271}
272
273
274
275bes::CatalogItem *Granule::getCatalogItem(const BESCatalogUtils *d_catalog_utils) const
276{
277 auto *item = new bes::CatalogItem();
278 item->set_type(bes::CatalogItem::leaf);
279 item->set_name(getName());
280 item->set_lmt(getLastModifiedStr());
281 item->set_size(getSize());
282 item->set_is_data(d_catalog_utils->is_data(item->get_name()));
283 if(!getDapServiceUrl().empty()) {
284 item->set_dap_service_url(getDapServiceUrl());
285 }
286
287 return item;
288}
289
290
291
292} //namespace cmr
bool is_data(const std::string &item) const
is there a handler that can process this
Granule(const nlohmann::json &granule_json)
Definition Granule.cc:145