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