• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

telefonicaid / fiware-orion / 45953572929

27 Nov 2026 06:42AM UTC coverage: 79.258% (-1.2%) from 80.495%
45953572929

Pull #4835

github

web-flow
Merge 4d18f97e2 into 710cc426d
Pull Request #4835: (JP) Added documentation about contextReferences (#4833)

21168 of 28251 branches covered (74.93%)

Branch coverage included in aggregate %.

17238 of 20206 relevant lines covered (85.31%)

36574.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.81
/src/lib/parse/CompoundValueNode.cpp
1
/*
2
*
3
* Copyright 2014 Telefonica Investigacion y Desarrollo, S.A.U
4
*
5
* This file is part of Orion Context Broker.
6
*
7
* Orion Context Broker is free software: you can redistribute it and/or
8
* modify it under the terms of the GNU Affero 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
* Orion Context Broker is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
15
* General Public License for more details.
16
*
17
* You should have received a copy of the GNU Affero General Public License
18
* along with Orion Context Broker. If not, see http://www.gnu.org/licenses/.
19
*
20
* For those usages not covered by this license please contact with
21
* iot_support at tid dot es
22
*
23
* Author: Ken Zangelin
24
*/
25
#include <string>
26

27
#include "logMsg/logMsg.h"
28
#include "logMsg/traceLevels.h"
29

30
#include "common/globals.h"
31
#include "common/string.h"
32
#include "common/JsonHelper.h"
33
#include "common/macroSubstitute.h"
34
#include "alarmMgr/alarmMgr.h"
35
#include "mongoDriver/safeMongo.h"
36
#include "parse/forbiddenChars.h"
37

38
#include "orionTypes/OrionValueType.h"
39
#include "parse/CompoundValueNode.h"
40

41

42

43
namespace orion
44
{
45
/* ****************************************************************************
46
*
47
* CompoundValueNode - constructor for toplevel 'node'
48
*/
49
CompoundValueNode::CompoundValueNode():
8,904 ✔
50
  name        ("Unset"),
17,808 ✔
51
  valueType   (orion::ValueTypeNotGiven),
8,904 ✔
52
  numberValue (0.0),
8,904 ✔
53
  boolValue   (false)
8,904 ✔
54
{
55
  LM_T(LmtCompoundValue, ("Created EMPTY compound node at %p", this));
8,904 ✔
56
}
8,904 ✔
57

58

59

60
/* ****************************************************************************
61
*
62
* CompoundValueNode - constructor for toplevel 'node'
63
*/
64
CompoundValueNode::CompoundValueNode(orion::ValueType _type):
11,520 ✔
65
  valueType   (_type),
11,520 ✔
66
  numberValue (0.0),
11,520 ✔
67
  boolValue   (false)
11,520 ✔
68
{
69
  LM_T(LmtCompoundValue, ("Created TOPLEVEL compound node (a %s) at %p", (valueType == orion::ValueTypeVector)? "Vector" : "Object", this));
11,520 ✔
70
}
11,520 ✔
71

72

73

74
/* ****************************************************************************
75
*
76
* CompoundValueNode - constructor for all nodes except toplevel (string)
77
*/
78
CompoundValueNode::CompoundValueNode
21,229 ✔
79
(
80
  const std::string&  _name,
81
  const std::string&  _value,
82
  orion::ValueType    _type
83
):
21,229 ✔
84
  name        (_name),
21,229 ✔
85
  valueType   (_type),
21,229 ✔
86
  stringValue (_value),
21,229 ✔
87
  numberValue (0.0),
21,229 ✔
88
  boolValue   (false)
21,229 ✔
89
{
90
  LM_T(LmtCompoundValue, ("Created compound node '%s', type %s at %p",
21,229 ✔
91
                          name.c_str(),
92
                          orion::valueTypeName(valueType),
93
                          this));
94
}
21,229 ✔
95

96

97

98
/* ****************************************************************************
99
*
100
* CompoundValueNode - constructor for all nodes except toplevel (char*)
101
*/
102
CompoundValueNode::CompoundValueNode
13 ✔
103
(
104
  const std::string&  _name,
105
  const char*         _value,
106
  orion::ValueType    _type
107
):
13 ✔
108
  name        (_name),
13 ✔
109
  valueType   (_type),
13 ✔
110
  stringValue (std::string(_value)),
26 ✔
111
  numberValue (0.0),
13 ✔
112
  boolValue   (false)
13 ✔
113
{
114
  LM_T(LmtCompoundValue, ("Created compound node '%s', type %s at %p",
13 ✔
115
                          name.c_str(),
116
                          orion::valueTypeName(valueType),
117
                          this));
118
}
13 ✔
119

120

121

122
/* ****************************************************************************
123
*
124
* CompoundValueNode - constructor for all nodes except toplevel (number)
125
*/
126
CompoundValueNode::CompoundValueNode
8,142 ✔
127
(
128
  const std::string&  _name,
129
  double              _value,
130
  orion::ValueType    _type
131
):
8,142 ✔
132
  name        (_name),
8,142 ✔
133
  valueType   (_type),
8,142 ✔
134
  stringValue (),
8,142 ✔
135
  numberValue (_value),
8,142 ✔
136
  boolValue   (false)
8,142 ✔
137
{
138
  LM_T(LmtCompoundValue, ("Created compound node '%s', type %s at %p",
8,142 ✔
139
                          name.c_str(),
140
                          orion::valueTypeName(valueType),
141
                          this));
142
}
8,142 ✔
143

144

145

146
/* ****************************************************************************
147
*
148
* CompoundValueNode - constructor for all nodes except toplevel (bool)
149
*/
150
CompoundValueNode::CompoundValueNode
2,113 ✔
151
(
152
  const std::string&  _name,
153
  bool                _value,
154
  orion::ValueType    _type
155
):
2,113 ✔
156
  name        (_name),
2,113 ✔
157
  valueType   (_type),
2,113 ✔
158
  stringValue (""),
4,226 ✔
159
  numberValue (0.0),
2,113 ✔
160
  boolValue   (_value)
2,113 ✔
161
{
162
  LM_T(LmtCompoundValue, ("Created compound node '%s', type %s at %p",
2,113 ✔
163
                          name.c_str(),
164
                          orion::valueTypeName(valueType),
165
                          this));
166
}
2,113 ✔
167

168

169

170
/* ****************************************************************************
171
*
172
* CompoundValueNode - destructor for all nodes except toplevel
173
*/
174
CompoundValueNode::~CompoundValueNode()
51,921 ✔
175
{
176
  LM_T(LmtCompoundValue, ("Destroying node %p: name: '%s' at %p (with %d children)", this, name.c_str(), this, childV.size()));
51,921 ✔
177

178
  for (uint64_t ix = 0; ix < childV.size(); ++ix)
94,821 ✔
179
  {
180
    if (childV[ix] != NULL)
42,900 !
181
    {
182
      LM_T(LmtCompoundValue, ("Deleting child %d, at %p", ix, childV[ix]));
42,900 ✔
183
      delete childV[ix];
42,900 !
184
      childV[ix] = NULL;
42,900 ✔
185
    }
186
  }
187

188
  while (childV.size() != 0)
94,821 ✔
189
  {
190
    childV.erase(childV.begin());
42,900 ✔
191
  }
192

193
  childV.clear();
51,921 ✔
194
}
51,921 ✔
195

196

197

198
/* ****************************************************************************
199
*
200
* finish -
201
*
202
*/
203
std::string CompoundValueNode::finish(void)
4 ✔
204
{
205
  LM_T(LmtCompoundValue, ("Finishing a compound: s (%s)", name.c_str(), toJson().c_str()));
4 ✔
206

207
  return check("");
8 ✔
208
}
209

210

211

212
/* ****************************************************************************
213
*
214
* add -
215
*/
216
CompoundValueNode* CompoundValueNode::add(CompoundValueNode* node)
35,768 ✔
217
{
218
  if (node->valueType == orion::ValueTypeString)
35,768 ✔
219
  {
220
    LM_T(LmtCompoundValueAdd, ("Adding String '%s', with value '%s'",
13,480 ✔
221
                               node->name.c_str(),
222
                               node->stringValue.c_str()));
223
  }
224
  else
225
  {
226
    LM_T(LmtCompoundValueAdd, ("Adding %s '%s')", orion::valueTypeName(node->valueType), node->name.c_str()));
22,288 ✔
227
  }
228

229
  childV.push_back(node);
35,768 ✔
230
  return node;
35,768 ✔
231
}
232

233

234

235
/* ****************************************************************************
236
*
237
* add - (string)
238
*/
239
CompoundValueNode* CompoundValueNode::add
12 ✔
240
(
241
  const orion::ValueType  _type,
242
  const std::string&      _name,
243
  const std::string&      _value
244
)
245
{
246
  CompoundValueNode* node = new CompoundValueNode(_name, _value, _type);
12 !
247

248
  return add(node);
12 ✔
249
}
250

251

252

253
/* ****************************************************************************
254
*
255
* add - (char*)
256
*/
257
CompoundValueNode* CompoundValueNode::add
3 ✔
258
(
259
  const orion::ValueType  _type,
260
  const std::string&      _name,
261
  const char*             _value
262
)
263
{
264
  CompoundValueNode* node = new CompoundValueNode(_name, _value, _type);
3 !
265

266
  return add(node);
3 ✔
267
}
268

269

270

271
/* ****************************************************************************
272
*
273
* add - (double)
274
*/
275
CompoundValueNode* CompoundValueNode::add
4 ✔
276
(
277
  const orion::ValueType  _type,
278
  const std::string&      _name,
279
  double                  _value
280
)
281
{
282
  CompoundValueNode* node = new CompoundValueNode(_name, _value, _type);
4 !
283

284
  return add(node);
4 ✔
285
}
286

287

288

289
/* ****************************************************************************
290
*
291
* add - (bool)
292
*/
293
CompoundValueNode* CompoundValueNode::add
×
294
(
295
  const orion::ValueType  _type,
296
  const std::string&      _name,
297
  bool                    _value
298
)
299
{
300
  CompoundValueNode* node = new CompoundValueNode(_name, _value, _type);
×
301

302
  return add(node);
×
303
}
304

305

306

307
/* ****************************************************************************
308
*
309
* check -
310
*
311
* A vector must have all its children with the same name.
312
* An object cannot have two children with the same name.
313
*
314
*/
315
std::string CompoundValueNode::check(const std::string& path)
10,618 ✔
316
{
317
  if (valueType == orion::ValueTypeVector)
10,618 ✔
318
  {
319
    if (childV.size() == 0)
1,910 ✔
320
    {
321
      return "OK";
80 ✔
322
    }
323

324
    for (uint64_t ix = 1; ix < childV.size(); ++ix)
4,271 ✔
325
    {
326
      if (childV[ix]->name != childV[0]->name)
2,402 ✔
327
      {
328
        std::string error = "bad tag-name of vector item: /" + childV[ix]->name + "/, should be /" + childV[0]->name + "/";
1 ✔
329
        alarmMgr.badInput(clientIp, error);
3 ✔
330
        return error;
1 ✔
331
      }
1 ✔
332
    }
333
  }
334
  else if (valueType == orion::ValueTypeObject)
8,708 ✔
335
  {
336
    if (childV.size() == 0)
2,416 ✔
337
    {
338
      return "OK";
232 ✔
339
    }
340

341
    for (uint64_t ix = 0; ix < childV.size() - 1; ++ix)
4,265 ✔
342
    {
343
      for (uint64_t ix2 = ix + 1; ix2 < childV.size(); ++ix2)
5,157 ✔
344
      {
345
        if (childV[ix]->name == childV[ix2]->name)
3,192 ✔
346
        {
347
          std::string fullPath = (path.empty() ? "/" : path + name + "/");
1 !
348
          std::string error = "duplicated tag-name: /" + childV[ix]->name + "/ in path: " + fullPath;
1 ✔
349
          alarmMgr.badInput(clientIp, error);
3 ✔
350

351
          return error;
1 ✔
352
        }
1 ✔
353
      }
354
    }
355
  }
356
  else
357
  {
358
    if (forbiddenChars(stringValue.c_str()))
6,292 ✔
359
    {
360
      alarmMgr.badInput(clientIp, "found a forbidden character in the value of an attribute", stringValue);
10 ✔
361
      return "Invalid characters in attribute value";
4 ✔
362
    }
363
  }
364

365
  // 'recursively' call the check method for all children
366
  std::string res;
10,458 ✔
367

368
  for (uint64_t ix = 0; ix < childV.size(); ++ix)
18,988 ✔
369
  {
370
    res = childV[ix]->check(path + name + "/");
8,534 ✔
371
    if (res !="OK")
8,534 ✔
372
    {
373
      return res;
4 ✔
374
    }
375
  }
376
  return "OK";
20,908 ✔
377
}
10,458 ✔
378

379

380

381
/* ****************************************************************************
382
*
383
* CompoundValueNode::equal
384
*
385
*/
386
bool CompoundValueNode::equal(const orion::BSONElement& be)
864 ✔
387
{
388
  // Note objects cannot be declared inside switch block
389
  std::vector<orion::BSONElement> ba;
864 ✔
390
  orion::BSONObj bo;
864 ✔
391

392
  switch (valueType)
864 !
393
  {
394
  case orion::ValueTypeString:
200 ✔
395
    return (be.type() == orion::String) && (stringValue == be.String());
200 !
396

397
  case orion::ValueTypeNumber:
90 ✔
398
    // FIXME P2: according to regression tests, this seems to work with all number types (int32/int64/double)
399
    // However, let's keep an eye on this in the case some day it fails...
400
    return (be.type() == orion::NumberDouble && numberValue == be.Number());
90 !
401

402
  case orion::ValueTypeBoolean:
8 ✔
403
    return (be.type() == orion::Bool) && (boolValue == be.Bool());
8 !
404

405
  case orion::ValueTypeNull:
8 ✔
406
    return (be.type() == orion::jstNULL);
8 ✔
407

408
  case orion::ValueTypeVector:
188 ✔
409
    // nodeP must be a vector
410
    if (be.type() != orion::Array)
188 ✔
411
    {
412
      return false;
52 ✔
413
    }
414
    ba = be.Array();
136 ✔
415
    // nodeP must have the same number of elements
416
    if (childV.size() != ba.size())
136 ✔
417
    {
418
      return false;
12 ✔
419
    }
420
    for (unsigned int ix = 0; ix < childV.size(); ix++)
260 ✔
421
    {
422
      if (!(childV[ix]->equal(ba[ix])))
204 ✔
423
      {
424
        return false;
68 ✔
425
      }
426
    }
427
    return true;
56 ✔
428

429
  case orion::ValueTypeObject:
370 ✔
430
    // nodeP must be a object
431
    if (be.type() != orion::Object)
370 ✔
432
    {
433
      return false;
134 ✔
434
    }
435
    bo = be.embeddedObject();
236 ✔
436
    if ((int) childV.size() != bo.nFields())
236 ✔
437
    {
438
      return false;
76 ✔
439
    }
440
    for (unsigned int ix = 0; ix < childV.size(); ix++)
306 ✔
441
    {
442
      if (!bo.hasField(childV[ix]->name))
264 ✔
443
      {
444
        return false;
46 ✔
445
      }
446
      if (!(childV[ix]->equal(getFieldF(bo, childV[ix]->name))))
654 ✔
447
      {
448
        return false;
72 ✔
449
      }
450
    }
451
    return true;
42 ✔
452

453
  case orion::ValueTypeNotGiven:
×
454
    LM_E(("Runtime Error (value type not given (%s))", name.c_str()));
×
455
    return false;
×
456

457
  default:
×
458
    LM_E(("Runtime Error (value type unknown (%s))", name.c_str()));
×
459
    return false;
×
460
  }
461
}
864 ✔
462

463

464

465
/* ****************************************************************************
466
*
467
* CompoundValueNode:toJson
468
*
469
*/
470
std::string CompoundValueNode::toJson(ExprContextObject* exprContextObjectP)
17,862 ✔
471
{
472
  std::string      out;
17,862 ✔
473
  JsonVectorHelper jvh;
17,862 ✔
474
  JsonObjectHelper joh;
17,862 ✔
475

476
  switch (valueType)
17,862 !
477
  {
478
  case orion::ValueTypeString:
5,408 ✔
479
    return smartStringValue(stringValue, exprContextObjectP, "null");
10,816 ✔
480

481
  case orion::ValueTypeNumber:
4,666 ✔
482
    return double2string(numberValue);
4,666 ✔
483

484
  case orion::ValueTypeBoolean:
1,174 ✔
485
    return FT(boolValue);
2,348 ✔
486

487
  case orion::ValueTypeNull:
578 ✔
488
    return "null";
1,156 ✔
489

490
  case orion::ValueTypeVector:
3,029 ✔
491
    for (unsigned int ix = 0; ix < childV.size(); ix++)
10,037 ✔
492
    {
493
      jvh.addRaw(childV[ix]->toJson(exprContextObjectP));
7,008 ✔
494
    }
495
    return jvh.str();
3,029 ✔
496

497
  case orion::ValueTypeObject:
3,007 ✔
498
    for (unsigned int ix = 0; ix < childV.size(); ix++)
10,797 ✔
499
    {
500
      joh.addRaw(childV[ix]->name, childV[ix]->toJson(exprContextObjectP));
7,790 ✔
501
    }
502
    return joh.str();
3,007 ✔
503

504
  case orion::ValueTypeNotGiven:
×
505
    LM_E(("Runtime Error (value type not given (%s))", name.c_str()));
×
506
    return "";
×
507

508
  default:
×
509
    LM_E(("Runtime Error (value type unknown (%s))", name.c_str()));
×
510
    return "";
×
511
  }
512
}
17,862 ✔
513

514

515

516
/* ****************************************************************************
517
*
518
* CompoundValueNode:toExprContextObject
519
*
520
*/
521
ExprContextObject CompoundValueNode::toExprContextObject(void)
×
522
{
523
  ExprContextObject co;
×
524
  for (uint64_t ix = 0; ix < childV.size(); ++ix)
×
525
  {
526
    CompoundValueNode* child = childV[ix];
×
527
    switch (child->valueType)
×
528
    {
529
    case orion::ValueTypeString:
×
530
      co.add(child->name, child->stringValue);
×
531
      break;
×
532

533
    case orion::ValueTypeNumber:
×
534
      co.add(child->name, child->numberValue);
×
535
      break;
×
536

537
    case orion::ValueTypeBoolean:
×
538
      co.add(child->name, child->boolValue);
×
539
      break;
×
540

541
    case orion::ValueTypeNull:
×
542
      co.add(child->name);
×
543
      break;
×
544

545
    case orion::ValueTypeVector:
×
546
      co.add(child->name, child->toExprContextList());
×
547
      break;
×
548

549
    case orion::ValueTypeObject:
×
550
      co.add(child->name, child->toExprContextObject());
×
551
      break;
×
552

553
    case orion::ValueTypeNotGiven:
×
554
      LM_E(("Runtime Error (value type not given (%s))", name.c_str()));
×
555
      break;
×
556

557
    default:
×
558
      LM_E(("Runtime Error (value type unknown (%s))", name.c_str()));
×
559
    }
560
  }
561
  return co;
×
562
}
×
563

564

565

566
/* ****************************************************************************
567
*
568
* CompoundValueNode:toExprContextList
569
*
570
*/
571
ExprContextList CompoundValueNode::toExprContextList(void)
×
572
{
573
  ExprContextList cl;
×
574
  for (uint64_t ix = 0; ix < childV.size(); ++ix)
×
575
  {
576
    CompoundValueNode* child = childV[ix];
×
577
    switch (child->valueType)
×
578
    {
579
    case orion::ValueTypeString:
×
580
      cl.add(child->stringValue);
×
581
      break;
×
582

583
    case orion::ValueTypeNumber:
×
584
      cl.add(child->numberValue);
×
585
      break;
×
586

587
    case orion::ValueTypeBoolean:
×
588
      cl.add(child->boolValue);
×
589
      break;
×
590

591
    case orion::ValueTypeNull:
×
592
      cl.add();
×
593
      break;
×
594

595
    case orion::ValueTypeVector:
×
596
      cl.add(child->toExprContextList());
×
597
      break;
×
598

599
    case orion::ValueTypeObject:
×
600
      cl.add(child->toExprContextObject());
×
601
      break;
×
602

603
    case orion::ValueTypeNotGiven:
×
604
      LM_E(("Runtime Error (value type not given)"));
×
605
      break;
×
606

607
    default:
×
608
      LM_E(("Runtime Error (value type unknown)"));
×
609
    }
610
  }
611
  return cl;
×
612
}
×
613

614

615

616
/* ****************************************************************************
617
*
618
* clone -
619
*/
620
CompoundValueNode* CompoundValueNode::clone(void)
31,468 ✔
621
{
622
  CompoundValueNode* me;
623

624
  LM_T(LmtCompoundValue, ("cloning '%s'", name.c_str()));
31,468 ✔
625

626
  switch (valueType)
31,468 !
627
  {
628
  case orion::ValueTypeString:
20,228 ✔
629
  case orion::ValueTypeObject:
630
  case orion::ValueTypeVector:
631
    me = new CompoundValueNode(name, stringValue, valueType);
20,228 !
632
    break;
20,228 ✔
633

634
  case orion::ValueTypeNumber:
8,138 ✔
635
    me = new CompoundValueNode(name, numberValue, valueType);
8,138 !
636
    break;
8,138 ✔
637

638
  case orion::ValueTypeBoolean:
2,113 ✔
639
    me = new CompoundValueNode(name, boolValue, valueType);
2,113 !
640
    break;
2,113 ✔
641

642
  case orion::ValueTypeNull:
989 ✔
643
    me = new CompoundValueNode(name, stringValue, valueType);
989 !
644
    me->valueType = orion::ValueTypeNull;
989 ✔
645
    break;
989 ✔
646

647
  case orion::ValueTypeNotGiven:
×
648
    me = NULL;
×
649
    LM_E(("Runtime Error (value not given in compound node value)"));
×
650
    break;
×
651

652
  default:
×
653
    me = NULL;
×
654
    LM_E(("Runtime Error (unknown compound node value type: %d)", valueType));
×
655
  }
656

657
  for (unsigned int ix = 0; ix < childV.size(); ++ix)
57,430 ✔
658
  {
659
    LM_T(LmtCompoundValue, ("Adding child %d for '%s'", ix, name.c_str()));
25,962 ✔
660
    me->add(childV[ix]->clone());
25,962 ✔
661
  }
662

663
  return me;
31,468 ✔
664
}
665

666

667

668
/* ****************************************************************************
669
*
670
* isVector -
671
*/
672
bool CompoundValueNode::isVector(void)
1,704 ✔
673
{
674
  return (valueType == orion::ValueTypeVector);
1,704 ✔
675
}
676

677

678

679
/* ****************************************************************************
680
*
681
* isObject -
682
*/
683
bool CompoundValueNode::isObject(void)
344 ✔
684
{
685
  return (valueType == orion::ValueTypeObject);
344 ✔
686
}
687

688

689

690
/* ****************************************************************************
691
*
692
* isString -
693
*/
694
bool CompoundValueNode::isString(void)
272 ✔
695
{
696
  return (valueType == orion::ValueTypeString);
272 ✔
697
}
698
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc