blob: cd142fdc0b260f0ed3e4997a4935ce21ea6f3a1b [file] [log] [blame]
Khen Nursimuluf8abbc92016-11-04 19:56:45 -04001#
2# Copyright 2016 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import json
17import os
18from unittest import TestCase
19import time
20from tests.itests.docutests.test_utils import run_command_to_completion_with_raw_stdout
21from tests.utests.chameleon.protoc_plugins.test_utils import load_file
22
23
24pyang_cmd = "pyang --plugindir /voltha/experiments/plugin -f protobuf " \
25 "-p /voltha/tests/utests/netconf/yang " \
26 "/voltha/tests/utests/netconf/yang/{}"
27
28class YangToProtoBufTests(TestCase):
29
30 def _compare_file(self, response, expected_response):
31 # compare two files and strip empty lines, blanks, etc
32 def _filter(x):
33 x.strip()
34 return x is not None
35
36 response = filter(_filter,response.split())
37 expected_response = filter(_filter,expected_response.split())
38 self.assertEqual(set(response), set(expected_response))
39
40
41 def test_01_basic_def(self):
42 print "Test_01_basic_def_Start:------------------"
43 t0 = time.time()
44
45 # input file: /voltha/tests/utests/netconf/yang/basic.yang
46
47 expected_response = """
48 syntax = "proto3";
49 package basic;
50
51 message commonAttributes {
52 uint32 my-id = 1 ;
53 string my-name = 2 ;
54 bool my-status = 3 ;
55 }
56 """
57
58 try:
59 cmd = pyang_cmd.format('basic.yang')
60 print 'running command: {}'.format(cmd)
61 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
62 self.assertEqual(rc, 0)
63
64 self._compare_file(response, expected_response)
65 finally:
66 print "Test_01_basic_def_End:------------------ took {} " \
67 "secs\n\n".format(time.time() - t0)
68
69
70 def test_02_container_def(self):
71 print "Test_02_container_def_Start:------------------"
72 t0 = time.time()
73
74 # input file: /voltha/tests/utests/netconf/yang/container.yang
75
76 expected_response = """
77 syntax = "proto3";
78 package container;
79
80 message int-container {
81 int32 eight = 1 ;
82 int32 nine = 2 ;
83 }
84 message int-container {
85 int32 eight = 1 ;
86 int32 nine = 2 ;
87 }
88 """
89
90 try:
91 cmd = pyang_cmd.format('container.yang')
92 print 'running command: {}'.format(cmd)
93 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
94 self.assertEqual(rc, 0)
95
96 self._compare_file(response, expected_response)
97 finally:
98 print "Test_02_container_def_End:------------------ took {} " \
99 "secs\n\n".format(time.time() - t0)
100
101
102 def test_03_mix_simple_types(self):
103 print "Test_03_mix_simple_types_Start:------------------"
104 t0 = time.time()
105
106 # input file: /voltha/tests/utests/netconf/yang/mix_simple_types.yang
107
108 expected_response = """
109 syntax = "proto3";
110 package mix_simple_types;
111
112 message user {
113 string name = 1 ;
114 string full-name = 2 ;
115 string class = 3 ;
116 }
117 message user {
118 string name = 1 ;
119 string full-name = 2 ;
120 string class = 3 ;
121 }
122
123 message int-container {
124 int32 eight = 1 ;
125 int32 nine = 2 ;
126 int32 ten = 3 ;
127 }
128 message container1 {
129 bool a = 1 ;
130 Any b = 2 ;
131 string mleaf = 3 ;
132 repeated string mleaf_list = 4 ;
133 message inner-container {
134 string mleaf1 = 1 ;
135 string mleaf2 = 2 ;
136 }
137 }
138 message int-container {
139 int32 eight = 1 ;
140 int32 nine = 2 ;
141 int32 ten = 3 ;
142 }
143 message container1 {
144 bool a = 1 ;
145 Any b = 2 ;
146 string mleaf = 3 ;
147 repeated string mleaf_list = 4 ;
148 message inner-container {
149 string mleaf1 = 1 ;
150 string mleaf2 = 2 ;
151 }
152 }
153 """
154
155 try:
156 cmd = pyang_cmd.format('mix_simple_types.yang')
157 print 'running command: {}'.format(cmd)
158 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
159 self.assertEqual(rc, 0)
160
161 self._compare_file(response, expected_response)
162 finally:
163 print "Test_03_mix_simple_types_End:------------------ took {} " \
164 "secs\n\n".format(time.time() - t0)
165
166
167 def test_04_cord_tenant(self):
168 print "Test_04_cord_tenant_Start:------------------"
169 t0 = time.time()
170
171 # input file: /voltha/tests/utests/netconf/yang/cord-tenant.yang
172
173 expected_response = """
174 syntax = "proto3";
175 package cord_tenant;
176
177 message subscriber {
178 string label = 1 ;
179 enum status
180 {
181 violation = 0 ;
182 enabled = 1 ;
183 delinquent = 2 ;
184 suspended = 3 ;
185 }
186 bool demo = 3 ;
187 }
188 """
189 try:
190 cmd = pyang_cmd.format('cord-tenant.yang')
191 print 'running command: {}'.format(cmd)
192 response, err, rc = run_command_to_completion_with_raw_stdout(cmd)
193 self.assertEqual(rc, 0)
194
195 self._compare_file(response, expected_response)
196 finally:
197 print "Test_04_cord_tenant_End:------------------ took {} " \
198 "secs\n\n".format(time.time() - t0)