Cell and Network slicing configuration over o1.
[o-du/l2.git] / src / o1 / NetconfUtils.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [HCL Technologies Ltd.]                          #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /* This file contains defination of generic netconf utility function which can
20  *  be used by any class*/
21
22 #include <sstream>
23 #include "NetconfUtils.hpp"
24
25 /*******************************************************************
26  *
27  * @brief print changes of given operation
28  *
29  * @details
30  *
31  *    Function : printChange
32  *
33  *    Functionality:
34  *      -  print changes of given operation, old and new value
35  *
36  *
37  * @params[in] sysrepo::S_Change change
38  * @return void
39  ******************************************************************/
40
41 void NetconfUtils::printChange(sysrepo::S_Change change) {
42    switch(change->oper()) {
43       case SR_OP_CREATED:
44          if (nullptr != change->new_val()) {
45             O1_LOG("\nO1 NetconfUtils : CREATED: %s", \
46                     change->new_val()->to_string().c_str());
47          }
48          break;
49       case SR_OP_DELETED:
50          if (nullptr != change->old_val()) {
51              O1_LOG("\nO1 NetconfUtils : DELETED:  %s", \
52                      change->old_val()->to_string().c_str());
53          }
54          break;
55       case SR_OP_MODIFIED:
56          if (nullptr != change->old_val() && nullptr != change->new_val()) {
57             O1_LOG("\nO1 NetconfUtils : MODIFIED: old value %s :new value %s", \
58                     change->old_val()->to_string().c_str(), \
59                     change->new_val()->to_string().c_str());
60         }
61         break;
62      case SR_OP_MOVED:
63         if (nullptr != change->old_val() && nullptr != change->new_val()) {
64            O1_LOG("\nO1 NetconfUtils : MOVED: %s :after %s ", \
65                     change->new_val()->xpath(), \
66                     change->old_val()->xpath());
67         }
68         else if (nullptr != change->new_val()) {
69            O1_LOG("\nO1 NetconfUtils : MOVED: %s : first", \
70                    change->new_val()->xpath());
71         }
72         break;
73     }
74 }
75
76 /*******************************************************************
77  *
78  * @brief convert event type to string
79  *
80  * @details
81  *
82  *    Function : evToStr
83  *
84  *    Functionality:
85  *      - convert event type to string
86  *
87  *
88  * @params[in] sr_event_t event
89  * @return event name in string form
90  ******************************************************************/
91
92 /* Helper function for printing events. */
93 const char *NetconfUtils::evToStr(sr_event_t ev) {
94    switch (ev) {
95       case SR_EV_CHANGE:
96          return "change";
97       case SR_EV_DONE:
98          return "done";
99       case SR_EV_ABORT:
100       default:
101          return "abort";
102    }
103 }
104
105 /*******************************************************************
106  *
107  * @brief get the leaf name from xpath
108  *
109  * @details
110  *
111  *    Function : getLeafInfo
112  *
113  *    Functionality:
114  *      - extract the leaf name from xpath
115  *
116  *
117  * @params[in] xpath, output string
118  * @return void
119  ******************************************************************/
120
121 void NetconfUtils::getLeafInfo(string xpath,string &parent, string &leaf ){
122    std::stringstream ssLeaf(xpath);
123    std::stringstream ssParent(xpath);
124    char ch='/';
125    getline(ssLeaf, leaf, ch);
126    while(getline(ssLeaf, leaf, ch)){
127        getline(ssParent, parent, ch);
128    } //get leaf name after the last '/'
129    std::stringstream ss(leaf);
130    ch=' ';
131    getline(ss, leaf, ch); //remove extra space
132    //O1_LOG("\nO1 NetconfUtils : parent = [%s]", parent.c_str());
133    //O1_LOG("\nO1 NetconfUtils : leaf = [%s]", leaf.c_str());
134
135 }
136
137 /**********************************************************************
138          End of file
139 **********************************************************************/