1 // Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <util/encode/hex.h>
10 #include <util/strutil.h>
11 #include <yang/adaptor_host.h>
12 #include <iomanip>
13 #include <sstream>
14 
15 using namespace std;
16 using namespace isc::data;
17 using namespace isc::util;
18 
19 namespace isc {
20 namespace yang {
21 
22 const string
23 AdaptorHost::STD_CHARACTERS =
24     "0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.@_";
25 
AdaptorHost()26 AdaptorHost::AdaptorHost() {
27 }
28 
~AdaptorHost()29 AdaptorHost::~AdaptorHost() {
30 }
31 
32 void
quoteIdentifier(ElementPtr host)33 AdaptorHost::quoteIdentifier(ElementPtr host) {
34     ConstElementPtr flex_id = host->get("flex-id");
35     if (!flex_id) {
36         return;
37     }
38     const string& id = flex_id->stringValue();
39     // Empty is allowed.
40     if (id.empty()) {
41         return;
42     }
43     // No special and no not printable characters?
44     if (id.find_first_not_of(STD_CHARACTERS) == string::npos) {
45         return;
46     }
47     // Quoted identifier?
48     vector<uint8_t> binary = str::quotedStringToBinary(id);
49     if (binary.empty()) {
50         binary.assign(id.begin(), id.end());
51     }
52     // Convert in hexadecimal (from DUID::toText()).
53     stringstream tmp;
54     tmp << hex;
55     bool delim = false;
56     for (vector<uint8_t>::const_iterator it = binary.begin();
57          it != binary.end(); ++it) {
58         if (delim) {
59             tmp << ":";
60         }
61         tmp << setw(2) << setfill('0') << static_cast<unsigned int>(*it);
62         delim = true;
63     }
64     host->set("flex-id", Element::create(tmp.str()));
65 }
66 
67 }; // end of namespace isc::yang
68 }; // end of namespace isc
69