Merge from master to connectivity-abstraction branch
[platform/upstream/iotivity.git] / resource / src / OCRepresentation.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file OCRepresentation.cpp
22
23 /// @brief  This file contains the implementation of classes and its members
24 ///         related to OCRepresentation
25
26
27 #include <OCRepresentation.h>
28
29 #include <cereal/cereal.hpp>
30 #include <cereal/types/map.hpp>
31 #include <cereal/types/vector.hpp>
32 #include <cereal/types/utility.hpp>
33 #include <OicJsonSerializer.hpp>
34 #include <algorithm>
35
36 // code needed to serialize a string=>Attribute value map
37 namespace OC
38 {
39     namespace detail
40     {
41         template<class Archive>
42         class WriteAttributeValue : public boost::static_visitor<>
43         {
44             public:
45                 WriteAttributeValue(const std::string& name, Archive& ar)
46                     :m_name(name), m_archive(ar)
47                 {}
48
49                 template<class T>
50                 void operator()(const T& value) const
51                 {
52                     m_archive(cereal::make_nvp(m_name, value));
53                 }
54             private:
55                 std::string m_name;
56                 Archive& m_archive;
57         };
58     }
59 }
60
61 namespace cereal
62 {
63     // take no action when serializing the null type, because the 'save' below
64     // doesn't use the visitor for this type.
65     template <class Archive>
66     void serialize(Archive&, OC::NullType t)
67     {}
68
69     template<class Archive>
70     void save(Archive& ar, const std::map<std::string, OC::AttributeValue>& vals)
71     {
72         for(const auto& kv : vals)
73         {
74             const auto& k = kv.first;
75             const auto& v = kv.second;
76
77             if(v.which() != OC::AttributeValueNullIndex)
78             {
79                 OC::detail::WriteAttributeValue<Archive> writer(k,ar);
80                 boost::apply_visitor(writer, v);
81             }
82             else
83             {
84                 ar.setNextName(k.c_str());
85                 ar.writeName();
86                 ar.saveValue();
87             }
88         }
89     }
90
91     template<class Archive>
92     void load(Archive& ar, std::map<std::string, OC::AttributeValue>& vals)
93     {
94         ar.loadAttributeValues(vals);
95     }
96 }
97
98 namespace OC
99 {
100     typedef cereal::JSONOutputArchive OutputArchiveType;
101     typedef cereal::JSONInputArchive InputArchiveType;
102
103     void MessageContainer::setJSONRepresentation(const std::string& payload)
104     {
105         std::stringstream os(payload);
106         {
107             InputArchiveType archive(os);
108             archive(cereal::make_nvp(OC::Key::OCKEY, m_reps));
109         }
110     }
111
112     void MessageContainer::setJSONRepresentation(const unsigned char* payload)
113     {
114         setJSONRepresentation(std::string(reinterpret_cast<const char*>(payload)));
115     }
116
117     std::string MessageContainer::getJSONRepresentation(OCInfoFormat f) const
118     {
119         std::stringstream os;
120
121         // note: the block is required because cereal closes the JSON string
122         // upon destruction, so the archive needs to be destroyed before accessing
123         // the data
124         {
125             if(f == OCInfoFormat::IncludeOC)
126             {
127                 OutputArchiveType archive(os);
128                 archive(cereal::make_nvp(OC::Key::OCKEY, m_reps));
129             }
130             else if(f== OCInfoFormat::ExcludeOC)
131             {
132                 bool firstPrinted = false;
133                 for(std::vector<OCRepresentation>::size_type i = 0; i< m_reps.size();++i)
134                 {
135                     if(!m_reps[i].empty())
136                     {
137                         if(firstPrinted)
138                         {
139                             os<<',';
140                         }
141                         firstPrinted=true;
142                         os << m_reps[i].getJSONRepresentation();
143                     }
144                 }
145             }
146         }
147         return os.str();
148     }
149
150     const std::vector<OCRepresentation>& MessageContainer::representations() const
151     {
152         return m_reps;
153     }
154
155     void MessageContainer::addRepresentation(const OCRepresentation& rep)
156     {
157         m_reps.push_back(rep);
158     }
159 }
160
161 namespace OC
162 {
163     OCRepresentation::OCRepresentation()
164     :m_interfaceType(InterfaceType::None)
165     { }
166     std::string OCRepresentation::getJSONRepresentation() const
167     {
168         if(empty())
169         {
170             return "{}";
171         }
172
173         std::stringstream os;
174
175         // note: the block is required because cereal closes the JSON string
176         // upon destruction, so the archive needs to be destroyed before accessing
177         // the data
178         {
179             OutputArchiveType archive (os);
180             save(archive);
181         }
182
183         return os.str();
184     }
185
186     void OCRepresentation::addChild(const OCRepresentation& rep)
187     {
188         m_children.push_back(rep);
189     }
190
191     void OCRepresentation::clearChildren()
192     {
193         m_children.clear();
194     }
195
196     const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
197     {
198         return m_children;
199     }
200
201     void OCRepresentation::setUri(const std::string& uri)
202     {
203         m_uri = uri;
204     }
205
206     std::string OCRepresentation::getUri() const
207     {
208         return m_uri;
209     }
210
211     const std::vector<std::string>& OCRepresentation::getResourceTypes() const
212     {
213         return m_resourceTypes;
214     }
215
216     void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
217     {
218         m_resourceTypes = resourceTypes;
219     }
220
221     const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
222     {
223         return m_interfaces;
224     }
225
226     void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
227     {
228         m_interfaces = resourceInterfaces;
229     }
230
231     bool OCRepresentation::hasAttribute(const std::string& str) const
232     {
233         return m_values.find(str) != m_values.end();
234     }
235
236     bool OCRepresentation::empty() const
237     {
238         // This logic is meant to determine whether based on the JSON serialization rules
239         // if this object will result in empty JSON.  URI is only serialized if there is valid
240         // data, ResourceType and Interfaces are only serialized if we are a nothing, a
241         // child of a default or link item.
242         // Our values array is only printed in the if we are the child of a Batch resource,
243         // the parent in a 'default' situation, or not in a child/parent relationship.
244         if(!m_uri.empty())
245         {
246             return false;
247         }
248         else if ((m_interfaceType == InterfaceType::None
249                         || m_interfaceType==InterfaceType::DefaultChild
250                         || m_interfaceType==InterfaceType::LinkChild)
251                     && (m_resourceTypes.size()>0 || m_interfaces.size()>0))
252         {
253             return false;
254         }
255         else if((m_interfaceType == InterfaceType::None
256                         || m_interfaceType == InterfaceType::BatchChild
257                         || m_interfaceType == InterfaceType::DefaultParent)
258                     && m_values.size()>0)
259         {
260             return false;
261         }
262
263         if(m_children.size() > 0)
264         {
265             return false;
266         }
267
268         return true;
269     }
270
271     int OCRepresentation::numberOfAttributes() const
272     {
273         return m_values.size();
274     }
275
276     bool OCRepresentation::erase(const std::string& str)
277     {
278         return m_values.erase(str);
279     }
280
281     void OCRepresentation::setNULL(const std::string& str)
282     {
283         m_values[str] = OC::NullType();
284     }
285
286     bool OCRepresentation::isNULL(const std::string& str) const
287     {
288         auto x = m_values.find(str);
289
290         if(m_values.end() != x)
291         {
292             return x->second.which() == AttributeValueNullIndex;
293         }
294         else
295         {
296             throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
297         }
298     }
299 }
300
301 namespace OC
302 {
303     template <class Archive, class Val>
304     void OCRepresentation::optional_load(Archive& ar, Val&& v)
305     {
306         try
307         {
308             ar(v);
309         }
310         catch(cereal::Exception& e)
311         {
312             ar.setNextName(nullptr);
313             // Loading a key that doesn't exist results in an exception
314             // Since "Not Found" is a valid condition for us, we swallow
315             // this exception and the archive will not load anything
316         }
317     }
318
319     template<class Archive>
320     void OCRepresentation::save(Archive& ar) const
321     {
322         // printed for all interface types
323         if(!m_uri.empty())
324         {
325             ar(cereal::make_nvp(Key::URIKEY, m_uri));
326         }
327
328         if((m_interfaceType == InterfaceType::None
329                     || m_interfaceType==InterfaceType::DefaultChild
330                     || m_interfaceType==InterfaceType::LinkChild)
331                 && (m_resourceTypes.size()>0 || m_interfaces.size()>0))
332         {
333             // The Prop object requires that it refer to non-const vectors
334             // so that it can alter them in the 'load' case.  In the save case
335             // (initiated here) it will not modify the object.  So, to keep the
336             // compiler happy, removing the 'const' context here is necessary.
337             const std::vector<std::string>& rt(m_resourceTypes);
338             const std::vector<std::string>& intf(m_interfaces);
339             Prop temp(const_cast<std::vector<std::string>&>(rt),
340                     const_cast<std::vector<std::string>&>(intf));
341             ar(cereal::make_nvp(Key::PROPERTYKEY, temp));
342         }
343
344         // printed only for BatchChildren and DefaultParent
345         if((m_interfaceType == InterfaceType::None
346                     || m_interfaceType == InterfaceType::BatchChild
347                     || m_interfaceType == InterfaceType::DefaultParent)
348                 && m_values.size()>0)
349         {
350             ar(cereal::make_nvp(Key::REPKEY, m_values));
351         }
352     }
353
354     template<class Archive>
355     void OCRepresentation::load(Archive& ar)
356     {
357         optional_load(ar, cereal::make_nvp(Key::URIKEY, m_uri));
358         {
359             Prop temp(m_resourceTypes, m_interfaces);
360             optional_load(ar, cereal::make_nvp(Key::PROPERTYKEY, temp));
361         }
362         optional_load(ar, cereal::make_nvp(Key::REPKEY, m_values));
363     }
364
365     template<class Archive>
366     void OCRepresentation::Prop::save(Archive& ar) const
367     {
368         if(m_types.size() > 0)
369         {
370             ar(cereal::make_nvp(Key::RESOURCETYPESKEY, m_types));
371         }
372
373         if(m_interfaces.size()>0)
374         {
375             ar(cereal::make_nvp(Key::INTERFACESKEY, m_interfaces));
376         }
377     }
378
379     template<class Archive>
380     void OCRepresentation::Prop::load(Archive& ar)
381     {
382         optional_load(ar, cereal::make_nvp(Key::RESOURCETYPESKEY, m_types));
383         optional_load(ar, cereal::make_nvp(Key::INTERFACESKEY, m_interfaces));
384     }
385 }
386
387 // note: the below is used to load an AttributeValue map out of JSON
388 namespace OC
389 {
390     namespace detail
391     {
392         enum class typeTag:uint8_t
393         {
394             NOTHING = 0,
395             _string,
396             _int,
397             _double,
398             _bool,
399             _representation
400         };
401
402         typedef rapidjson::Document::GenericValue GenericValue;
403
404         AttributeValue parseAttributeValue(const GenericValue& v);
405         AttributeValue parseAttributeValue(const GenericValue& v,
406                 const unsigned int curLevel, unsigned int& maxDepth, typeTag& t);
407         AttributeValue parseAttributeValueObject(const GenericValue& v, typeTag& t);
408         AttributeValue parseAttributeValueArray(const GenericValue& v,
409                 const unsigned int curLevel, unsigned int& maxDepth, typeTag& t);
410         AttributeValue parseAttributeValuePrimitive(const GenericValue& v, typeTag& t);
411
412         AttributeValue parseAttributeValue(const GenericValue& v)
413         {
414             // base entrance, start everything at '0'
415             unsigned int max_depth {0};
416             typeTag t {typeTag::NOTHING};
417
418             return parseAttributeValue(v, 0, max_depth, t);
419         }
420
421         AttributeValue parseAttributeValue(const GenericValue& v,
422                 const unsigned int curLevel, unsigned int& maxDepth, typeTag& t)
423         {
424             if(v.IsObject())
425             {
426                 return parseAttributeValueObject(v, t);
427             }
428             else if(v.IsArray())
429             {
430                 return parseAttributeValueArray(v, curLevel + 1, maxDepth, t);
431             }
432             else
433             {
434                 return parseAttributeValuePrimitive(v,t);
435             }
436         }
437
438         AttributeValue parseAttributeValueObject(const GenericValue& v, typeTag& t)
439         {
440             typedef rapidjson::Value::ConstMemberIterator CMI;
441             t = typeTag::_representation;
442             OC::OCRepresentation rep;
443
444             for(CMI itr = v.MemberBegin(); itr!= v.MemberEnd(); ++itr)
445             {
446                 std::string keyName = itr->name.GetString();
447
448                 if(keyName == OC::Key::URIKEY)
449                 {
450                     rep.setUri(boost::get<std::string>(parseAttributeValue(itr->value)));
451                 }
452                 else if (keyName == OC::Key::PROPERTYKEY)
453                 {
454                     for(CMI itr2 = itr->value.MemberBegin();
455                             itr->value.MemberEnd()!=itr2;
456                             ++itr2)
457                     {
458                         if(keyName == OC::Key::RESOURCETYPESKEY)
459                         {
460                             rep.setResourceTypes(
461                                     boost::get<std::vector<std::string>>(
462                                         parseAttributeValue(itr->value)));
463                         }
464                         else if(keyName == OC::Key::PROPERTYKEY)
465                         {
466                             rep.setResourceInterfaces(
467                                     boost::get<std::vector<std::string>>(
468                                         parseAttributeValue(itr->value)));
469                         }
470                     }
471                 }
472                 else if (keyName == OC::Key::REPKEY)
473                 {
474                     for(CMI itr2 = itr->value.MemberBegin();
475                             itr->value.MemberEnd()!=itr2;
476                             ++itr2)
477                     {
478                         rep.setValue(itr2->name.GetString(),
479                                 parseAttributeValue(itr2->value));
480                     }
481                 }
482             }
483
484             return rep;
485         }
486
487         AttributeValue parseAttributeValuePrimitive(const GenericValue& v, typeTag& t)
488         {
489             if(v.IsString())
490             {
491                 t = typeTag::_string;
492                 return std::string(v.GetString());
493             }
494             else if (v.IsNumber())
495             {
496                 if(v.IsDouble())
497                 {
498                     t = typeTag::_double;
499                     return double(v.GetDouble());
500                 }
501                 else if (v.IsInt())
502                 {
503                     t = typeTag::_int;
504                     return int(v.GetInt());
505                 }
506                 else
507                 {
508                     throw OC::OCException(OC::Exception::INVALID_JSON_NUMERIC
509                             + std::to_string(v.GetType()));
510                 }
511             }
512             else if(v.IsBool_())
513             {
514                 t=typeTag::_bool;
515                 return bool(v.GetBool_());
516             }
517             else if(v.IsNull_())
518             {
519                 return OC::NullType();
520             }
521             else
522             {
523                 throw OC::OCException(OC::Exception::INVALID_JSON_TYPE
524                         + std::to_string(v.GetType()));
525             }
526         }
527
528         std::vector<AttributeValue> gatherArrayContents(const GenericValue& v,
529                 const unsigned int curLevel, unsigned int& maxDepth, typeTag& t)
530         {
531             std::vector<AttributeValue> out;
532
533             std::transform(v.Begin(), v.End(), back_inserter(out),
534                     [curLevel, &maxDepth, &t](const GenericValue& x)
535                     {
536                         return parseAttributeValue(x, curLevel, maxDepth, t);
537                     });
538             return out;
539         }
540
541         template<class OutT>
542         struct valueToConcrete
543         {
544             OutT operator()(const AttributeValue& v)
545             {
546                 return boost::get<OutT>(v);
547             }
548
549         };
550
551         template <class OutSeqT>
552         OutSeqT valuesToConcreteVectors(const std::vector<AttributeValue>& vs)
553         {
554             OutSeqT ret;
555
556             std::transform(begin(vs),end(vs), back_inserter(ret),
557                 valueToConcrete<typename OutSeqT::value_type>());
558             return ret;
559         }
560
561         template<class valueType>
562         AttributeValue remapArrayDepth(const unsigned int curLevel,
563                 const std::vector<OC::AttributeValue>& vs)
564         {
565             switch(curLevel)
566             {
567                 default:
568                     throw OC::OCException(OC::Exception::INVALID_JSON_ARRAY_DEPTH);
569                     break;
570                 case 1:
571                     return valuesToConcreteVectors<std::vector<valueType>>(vs);
572                     break;
573                 case 2:
574                     return valuesToConcreteVectors<std::vector<std::vector<valueType>>>(vs);
575                     break;
576                 case 3:
577                     return valuesToConcreteVectors
578                         <std::vector<std::vector<std::vector<valueType>>>>(vs);
579                     break;
580             }
581         }
582
583         AttributeValue convertArrayToConcretes(const typeTag t,
584                 const unsigned int curLevel, const std::vector<OC::AttributeValue>& vs)
585         {
586             // This function converts a std::vector of AttributeValue to a std::vector
587             // of concrete types.  Since we don't use a recursive Variant, we need
588             // to get back to a 'base' primitive type
589             switch(t)
590             {
591                 default:
592                 case typeTag::NOTHING:
593                     throw OC::OCException(OC::Exception::INVALID_JSON_TYPE_TAG);
594                     break;
595                 case typeTag::_string:
596                     return remapArrayDepth<std::string>(curLevel, vs);
597                     break;
598                 case typeTag::_int:
599                     return remapArrayDepth<int>(curLevel, vs);
600                     break;
601                 case typeTag::_double:
602                     return remapArrayDepth<double>(curLevel, vs);
603                     break;
604                 case typeTag::_bool:
605                     return remapArrayDepth<bool>(curLevel, vs);
606                     break;
607                 case typeTag::_representation:
608                     return remapArrayDepth<OCRepresentation>(curLevel, vs);
609                     break;
610             }
611         }
612
613         AttributeValue parseAttributeValueArray(const GenericValue& v,
614                 const unsigned int curLevel, unsigned int& maxDepth, typeTag& t)
615         {
616             const unsigned int max_level = 3;
617
618             if(curLevel > max_level)
619             {
620                 throw OC::OCException(OC::Exception::INVALID_JSON_ARRAY_DEPTH);
621             }
622
623             if(curLevel > maxDepth)
624             {
625                 maxDepth = curLevel;
626             }
627
628             auto arrayItems = gatherArrayContents(v, curLevel, maxDepth, t);
629             const int remapLevel = maxDepth - (curLevel -1);
630             return convertArrayToConcretes(t, remapLevel, arrayItems);
631         }
632     }
633 }
634
635 namespace cereal
636 {
637    void JSONInputArchive::loadAttributeValues(std::map<std::string, OC::AttributeValue>& map)
638    {
639        for(auto&b = itsIteratorStack.back();
640            b.Member && b.itsMemberItEnd != b.itsMemberItBegin+b.itsIndex;
641            ++b)
642        {
643            std::string key = b.itsMemberItBegin[b.itsIndex].name.GetString();
644            const GenericValue& v = itsIteratorStack.back().value();
645            map[key] = OC::detail::parseAttributeValue(v);
646        }
647    }
648 }