Edit-conf changes for CLA use case.
[o-du/l2.git] / src / o1 / SessionHandler.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020] [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 methods of Session/Connection creation and Subscription to
20    YANG modules */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "sysrepo.h"
25 #include "SessionHandler.hpp"
26 #include "InitConfig.hpp"
27 #include "NrCellCb.hpp"
28
29 using namespace std;
30 /* Default constructor */
31 SessionHandler::SessionHandler()
32 {    
33 }
34
35
36 /* Destructor */
37 SessionHandler::~SessionHandler()
38 {  
39 }
40
41 /********************************************************************** 
42    Description : This function will create Connection, Session, and 
43                  subscribe. These sysrepo class provide netconf connection
44                  related services.
45    Params[In]  : void
46    Return      : true  - started successful
47                  false - start failed
48 **********************************************************************/
49 bool SessionHandler::init()
50 {
51    try
52    {
53       mConn = createConnection();
54       if(mConn != NULL)
55       {
56          O1_LOG("\nO1 SessionHandler : Connection created");
57          //removing nacm module temperary for auth issue resolve
58          //mConn.remove_module("ietf-netconf-acm");
59          mSess = createSession(mConn);
60          if(mSess != NULL)
61          {
62             O1_LOG("\nO1 SessionHandler : Session created");
63             mSub  = createSubscribe(mSess);
64             if(mSub != NULL)
65             {
66                O1_LOG("\nO1 SessionHandler : Subscription created");
67                if(InitConfig::instance().init(mSess))
68                {
69                   return true;
70                }
71                else
72                {
73                   return false;
74                }
75             }
76             else 
77             {
78                O1_LOG("\nO1 SessionHandler : Subscription failed");
79                return false;
80             }
81          }
82          else
83          {
84             O1_LOG("\nO1 SessionHandler : Session failed");
85             return false;
86          }
87       }
88       else
89       {
90          O1_LOG("\nO1 SessionHandler : connection failed");
91          return false;
92       }
93    }
94    catch( const std::exception& e )
95    {
96       O1_LOG("\nO1 SessionHandler : Exception : %s", e.what());
97       return false;
98    }
99 }
100
101 /********************************************************************** 
102    Description : This function will create Connection instance and 
103                  return the same  
104    Params[In]  : void
105    Return      : sysrepo::S_Connection instance
106 **********************************************************************/
107 sysrepo::S_Connection SessionHandler::createConnection()
108 {
109    sysrepo::S_Connection conn(new sysrepo::Connection());
110    return conn;
111 }
112
113
114 /********************************************************************** 
115    Description : This function will create Session instance and
116                  return the same
117    Params[In]  : sysrepo::S_Connection
118    Return      : sysrepo::S_Session instance
119 **********************************************************************/
120 sysrepo::S_Session SessionHandler::createSession(sysrepo::S_Connection conn)
121 {
122    sysrepo::S_Session sess(new sysrepo::Session(conn));
123    return sess;
124 }
125
126
127
128 /**********************************************************************
129    Description : This function will create Subscribe instance and
130                  return the same
131    Params[In]  : sysrepo::S_Session
132    Return      : sysrepo::S_Subscribe instance
133 **********************************************************************/
134 sysrepo::S_Subscribe SessionHandler::createSubscribe(sysrepo::S_Session sess)
135 {
136    sysrepo::S_Subscribe subscrb(new sysrepo::Subscribe(sess));
137    if(subscribeModule(subscrb))
138    {
139       O1_LOG("\nO1 SessionHandler : Subscription done successfully");
140    }
141    return subscrb;
142 }
143
144
145 /********************************************************************** 
146    Description : This function will create a callback object and register
147                  it for callback. 
148    Params[In]  : sysrepo::S_Subscribe
149    Return      : true   - on success
150 **********************************************************************/
151 bool SessionHandler::subscribeModule(sysrepo::S_Subscribe subscrb)
152 {
153    sysrepo::S_Callback alarmOranCb(new AlarmOranYangModel());
154
155    subscrb->oper_get_items_subscribe(ALARM_MODULE_NAME_ORAN, \
156                                      ALARM_MODULE_PATH_ORAN, \
157                                      alarmOranCb);
158
159    sysrepo::S_Callback nrCellCb(new NrCellCb());
160
161    subscrb->oper_get_items_subscribe(CELL_STATE_MODULE_NAME, \
162                                      CELL_STATE_MODULE_PATH, \
163                                      nrCellCb);
164    subscrb->module_change_subscribe(CELL_STATE_MODULE_NAME, nrCellCb);
165
166
167    return true;
168 }
169
170 /**********************************************************************
171          End of file
172 **********************************************************************/