1#!/usr/bin/env bash
2# Test augmented state
3# Use main example -- -sS option to add state via a file
4#
5# Magic line must be first in script (see README.md)
6s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
7
8APPNAME=example
9
10cfg=$dir/conf_yang.xml
11fyang=$dir/example-augment.yang
12fyang2=$dir/example-lib.yang
13fstate=$dir/state.xml
14
15cat <<EOF > $cfg
16<clixon-config xmlns="http://clicon.org/config">
17  <CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
18  <CLICON_FEATURE>a:test</CLICON_FEATURE>
19  <CLICON_YANG_DIR>$dir</CLICON_YANG_DIR>
20  <CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
21  <CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
22  <CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
23  <CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
24  <CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
25  <CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
26  <CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
27  <CLICON_BACKEND_DIR>/usr/local/lib/$APPNAME/backend</CLICON_BACKEND_DIR>
28  <CLICON_RESTCONF_PRETTY>false</CLICON_RESTCONF_PRETTY>
29  <CLICON_XMLDB_DIR>/usr/local/var/$APPNAME</CLICON_XMLDB_DIR>
30  <CLICON_MODULE_LIBRARY_RFC7895>false</CLICON_MODULE_LIBRARY_RFC7895>
31</clixon-config>
32EOF
33
34# This is the lib function with the base container
35cat <<EOF > $fyang2
36module example-lib {
37  yang-version 1.1;
38  namespace "urn:example:lib";
39  revision "2019-03-04";
40  prefix lib;
41  container global-state {
42    config false;
43    leaf gbds{
44      description "Global base default state";
45      type string;
46      default "gbds";
47    }	  
48    leaf gbos{
49      description "Global base optional state";
50      type string;
51    }	  
52    container nopres{
53      description "This should be removed";
54    }
55  }
56  container base-config {
57    list parameter{
58      key name;
59      leaf name{
60	type string;
61      }
62      container param-state {
63        config false;
64        leaf lbds{
65	  description "Local base default state";
66  	  type string;
67	  default "lbds";
68        }
69        leaf lbos{
70	  description "Local base optional state";
71  	  type string;
72        }
73      }
74    }    
75  }
76}
77EOF
78
79# This is the main module where the augment exists
80cat <<EOF > $fyang
81module example-augment {
82  yang-version 1.1;
83  namespace "urn:example:augment";
84  prefix aug;
85  revision "2020-09-25";
86  import example-lib {
87     prefix lib;
88  }
89  /* Augments global state */
90  augment "/lib:global-state" {
91    leaf gads{
92      description "Global augmented default state";
93      type string;
94      default "gads";
95    }	  
96    leaf gaos{
97      description "Global augmented optional state";
98      type string;
99    }	  
100  }
101  /* Augments state in config in-line */
102  augment "/lib:base-config/lib:parameter/lib:param-state" {
103    leaf lads{
104      description "Local augmented default state";
105      type string;
106      default "lads";
107    }	  
108    leaf laos{
109      description "Local augmented optional state";
110      type string;
111    }	  
112  }
113}
114EOF
115
116# Get config and state
117# Arguments
118# - expected config
119# - expected state
120testrun()
121{
122    config=$1
123    state=$2
124
125    new "get config"
126    if [ -z "$config" ]; then
127	expecteof "$clixon_netconf -qf $cfg" 0 "<rpc $DEFAULTNS><get-config><source><running/></source></get-config></rpc>]]>]]>" "^<rpc-reply $DEFAULTNS><data/></rpc-reply>]]>]]>$"
128    else
129	expecteof "$clixon_netconf -qf $cfg" 0 "<rpc $DEFAULTNS><get-config><source><running/></source></get-config></rpc>]]>]]>" "^<rpc-reply $DEFAULTNS><data>$config</data></rpc-reply>]]>]]>$"
130    fi
131
132    new "get state"
133    expecteof "$clixon_netconf -qf $cfg" 0 "<rpc $DEFAULTNS><get/></rpc>]]>]]>" "^<rpc-reply $DEFAULTNS><data>$state</data></rpc-reply>]]>]]>$"
134}
135
136new "test params: -f $cfg"
137
138if [ $BE -ne 0 ]; then
139    new "kill old backend"
140    sudo clixon_backend -zf $cfg
141    if [ $? -ne 0 ]; then
142	err
143    fi
144    new "start backend -s init -f $cfg -- -sS $fstate"
145    start_backend -s init -f $cfg -- -sS $fstate
146fi
147new "waiting"
148wait_backend
149
150
151#-----------------------------
152new "1. Empty config/state, expect global default state"
153
154CONFIG=""
155
156cat <<EOF > $fstate
157EOF
158
159EXPSTATE=$(cat <<EOF
160<global-state xmlns="urn:example:lib"><gbds>gbds</gbds><aug:gads xmlns:aug="urn:example:augment">gads</aug:gads></global-state>
161EOF
162)
163
164testrun "$CONFIG" "$EXPSTATE"
165
166#-----------------------------
167new "2. Empty config/top-level state, expect global default state"
168cat <<EOF > $fstate
169<global-state xmlns="urn:example:lib"/>
170EOF
171
172testrun "$CONFIG" "$EXPSTATE"
173
174#-----------------------------
175new "3. Empty config/top-level w non-presence state, expect global default state"
176cat <<EOF > $fstate
177<global-state xmlns="urn:example:lib">
178   <nopres/>
179</global-state>
180EOF
181
182testrun "$CONFIG" "$EXPSTATE"
183
184#-----------------------------
185new "4. Empty config + optional state, expect global default + optional state"
186cat <<EOF > $fstate
187<global-state xmlns="urn:example:lib">
188  <gbos>gbos</gbos>
189  <aug:gaos xmlns:aug="urn:example:augment">gaos</aug:gaos>
190</global-state>
191EOF
192
193EXPSTATE=$(cat <<EOF
194<global-state xmlns="urn:example:lib"><gbds>gbds</gbds><gbos>gbos</gbos><aug:gads xmlns:aug="urn:example:augment">gads</aug:gads><aug:gaos xmlns:aug="urn:example:augment">gaos</aug:gaos></global-state>
195EOF
196)
197
198testrun "$CONFIG" "$EXPSTATE"
199
200#-----------------------------
201# From here, add a config tree
202new "5. Config tree, empty top state, expect default top state and default local state"
203
204CONFIG=$(cat <<EOF
205<base-config xmlns="urn:example:lib"><parameter><name>a</name></parameter></base-config>
206EOF
207)
208
209cat <<EOF > $fstate
210EOF
211
212EXPSTATE=$(cat <<EOF
213<global-state xmlns="urn:example:lib"><gbds>gbds</gbds><aug:gads xmlns:aug="urn:example:augment">gads</aug:gads></global-state><base-config xmlns="urn:example:lib"><parameter><name>a</name><param-state><lbds>lbds</lbds><aug:lads xmlns:aug="urn:example:augment">lads</aug:lads></param-state></parameter></base-config>
214EOF
215)
216
217new "Add config"
218expecteof "$clixon_netconf -qf $cfg" 0 "<rpc $DEFAULTNS><edit-config><target><candidate/></target><config>$CONFIG</config></edit-config></rpc>]]>]]>" "^<rpc-reply $DEFAULTNS><ok/></rpc-reply>]]>]]>"
219
220new "netconf commit"
221expecteof "$clixon_netconf -qf $cfg" 0 "<rpc $DEFAULTNS><commit/></rpc>]]>]]>" "^<rpc-reply $DEFAULTNS><ok/></rpc-reply>]]>]]>$"
222
223testrun "$CONFIG" "$EXPSTATE"
224
225#-----------------------------
226
227new "6. Config tree and optional tree state, empty top state, expect default top state and default local state"
228
229cat <<EOF > $fstate
230<base-config xmlns="urn:example:lib">
231   <parameter>
232      <name>a</name>
233      <param-state>
234         <lbos>lbos</lbos>
235         <laos xmlns="urn:example:augment">laos</laos>
236      </param-state>
237   </parameter>
238</base-config>
239EOF
240
241EXPSTATE=$(cat <<EOF
242<global-state xmlns="urn:example:lib"><gbds>gbds</gbds><aug:gads xmlns:aug="urn:example:augment">gads</aug:gads></global-state><base-config xmlns="urn:example:lib"><parameter><name>a</name><param-state><lbds>lbds</lbds><lbos>lbos</lbos><aug:lads xmlns:aug="urn:example:augment">lads</aug:lads><laos xmlns="urn:example:augment">laos</laos></param-state></parameter></base-config>
243EOF
244)
245
246testrun "$CONFIG" "$EXPSTATE"
247
248#-----------------------------
249
250if [ $BE -eq 0 ]; then
251    exit # BE
252fi
253
254new "Kill backend"
255# Check if premature kill
256pid=$(pgrep -u root -f clixon_backend)
257if [ -z "$pid" ]; then
258    err "backend already dead"
259fi
260# kill backend
261stop_backend -f $cfg
262
263rm -rf $dir
264