summaryrefslogtreecommitdiffstats
path: root/src/lib/yang/adaptor_host.cc
blob: 9df7d5b7a0a670154acaaa9da2184b652cb4646f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <util/encode/hex.h>
#include <util/strutil.h>
#include <yang/adaptor_host.h>
#include <iomanip>
#include <sstream>

using namespace std;
using namespace isc::data;
using namespace isc::util;

namespace isc {
namespace yang {

const string
AdaptorHost::STD_CHARACTERS =
    "0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.@_";

AdaptorHost::AdaptorHost() {
}

AdaptorHost::~AdaptorHost() {
}

void
AdaptorHost::quoteIdentifier(ElementPtr host) {
    ConstElementPtr flex_id = host->get("flex-id");
    if (!flex_id) {
        return;
    }
    const string& id = flex_id->stringValue();
    // Empty is allowed.
    if (id.empty()) {
        return;
    }
    // No special and no not printable characters?
    if (id.find_first_not_of(STD_CHARACTERS) == string::npos) {
        return;
    }
    // Quoted identifier?
    vector<uint8_t> binary = str::quotedStringToBinary(id);
    if (binary.empty()) {
        binary.assign(id.begin(), id.end());
    }
    // Convert in hexadecimal (from DUID::toText()).
    stringstream tmp;
    tmp << hex;
    bool delim = false;
    for (vector<uint8_t>::const_iterator it = binary.begin();
         it != binary.end(); ++it) {
        if (delim) {
            tmp << ":";
        }
        tmp << setw(2) << setfill('0') << static_cast<unsigned int>(*it);
        delim = true;
    }
    host->set("flex-id", Element::create(tmp.str()));
}

}  // end of namespace isc::yang
}  // end of namespace isc