This commit includes:
1) Make descriptiuon fields mandatory
2) Make some minor changes to comply to IETF
3) Add a few test cases (more to come)
Change-Id: Id871b2015ddb72c72a03c9fd5d0e08461fe67398
diff --git a/experiments/proto2Yang/addressbook.proto b/experiments/proto2yang/addressbook.proto
similarity index 95%
rename from experiments/proto2Yang/addressbook.proto
rename to experiments/proto2yang/addressbook.proto
index bfdceea..fc1a10f 100644
--- a/experiments/proto2Yang/addressbook.proto
+++ b/experiments/proto2yang/addressbook.proto
@@ -25,6 +25,7 @@
}
repeated PhoneNumber phones = 4;
+ repeated string khen = 5;
}
// Our address book file is just one of these.
diff --git a/experiments/proto2Yang/descriptor.desc b/experiments/proto2yang/descriptor.desc
similarity index 100%
rename from experiments/proto2Yang/descriptor.desc
rename to experiments/proto2yang/descriptor.desc
Binary files differ
diff --git a/experiments/proto2Yang/descriptor_parser.py b/experiments/proto2yang/descriptor_parser.py
similarity index 100%
rename from experiments/proto2Yang/descriptor_parser.py
rename to experiments/proto2yang/descriptor_parser.py
diff --git a/experiments/proto2Yang/proto2yang.py b/experiments/proto2yang/proto2yang.py
similarity index 90%
rename from experiments/proto2Yang/proto2yang.py
rename to experiments/proto2yang/proto2yang.py
index 88ce1a7..8d9a40b 100755
--- a/experiments/proto2Yang/proto2yang.py
+++ b/experiments/proto2yang/proto2yang.py
@@ -26,7 +26,7 @@
$ python -m grpc.tools.protoc -I.
--plugin=protoc-gen-custom=./proto2yang.py --custom_out=. <proto file>.proto
- - the above will produce a <proto file>.yang file formatted for yang
+ - the above will produce a ietf-<proto file>.yang file formatted for yang
- two examples of proto that can be used in the same directory are
yang.proto and addressbook.proto
@@ -42,37 +42,34 @@
from google.protobuf.descriptor import FieldDescriptor
template_yang = Template("""
-module {{ module.name }} {
-
- namespace "https://gerrit.opencord.org/voltha/{{ module.package }}";
+module ietf-{{ module.name }} {
yang-version 1.1;
-
+ namespace "urn:ietf:params:xml:ns:yang:ietf-{{ module.name }}";
prefix "voltha";
- revision 2016-11-15 {{ module.revision }} {
- {% if module.description %}
- /* {{ message.description }} */
- {% else %}
+ organization "CORD";
+ contact
+ " Any name";
+
+ description
+ "{{ module.description }}";
+
+ revision "2016-11-15" {
description "Initial revision.";
- {% endif %}
+ reference "reference";
}
{% for enum in module.enums %}
- {% if enum.description %}
- /* {{ enum.description }} */
- {% endif %}
typedef {{ enum.name }} {
type enumeration {
{% for v in enum.value %}
- {% if v.description %}
enum {{ v.name }} {
description "{{ v.description }}";
}
- {% else %}
- enum {{ v.name }} ;
- {% endif %}
{% endfor %}
}
+ description
+ "{{ enum.description }}";
}
{% endfor %}
@@ -82,14 +79,10 @@
{% else %}
container {{ message.name }} {
{% endif %}
- {% if message.description %}
- /* {{ message.description }} */
- {% endif %}
+ description
+ "{{ message.description }}";
{% for field in message.fields %}
{% if field.type_ref %}
- {% if field.description %}
- /* {{ field.description }} */
- {% endif %}
{% for dict_item in module.referred_messages_with_keys %}
{% if dict_item.name == field.type %}
list {{ field.name }} {
@@ -98,6 +91,8 @@
max-elements 1;
{% endif %}
uses {{ field.type }};
+ description
+ "{{ field.description }}";
}
{% endif %}
{% endfor %}
@@ -110,13 +105,13 @@
fraction-digits 5;
}
{% else %}
- type {{ field.type }} ;
+ type {{ field.type }};
{% endif %}
- {% if field.description %}
description
- "{{ field.description }}" ;
- {% endif %}
+ "{{ field.description }}";
}
+ description
+ "{{ field.description }}";
}
{% else %}
leaf {{ field.name }} {
@@ -125,32 +120,25 @@
fraction-digits 5;
}
{% else %}
- type {{ field.type }} ;
+ type {{ field.type }};
{% endif %}
- {% if field.description %}
description
- "{{ field.description }}" ;
- {% endif %}
+ "{{ field.description }}";
}
{% endif %}
{% endfor %}
{% for enum_type in message.enums %}
- {% if enum_type.description %}
- /* {{ enum_type.description }} */
- {% endif %}
typedef {{ enum_type.name }} {
type enumeration {
{% for v in enum_type.value %}
- {% if v.description %}
enum {{ v.name }} {
description "{{ v.description }}";
}
- {% else %}
- enum {{ v.name }} ;
- {% endif %}
{% endfor %}
}
+ description
+ "{{ enum_type.description }}";
}
{% endfor %}
@@ -165,17 +153,16 @@
/* {{ service.description }}" */
{% endif %}
{% for method in service.methods %}
- {% if method.description %}
- /* {{ method.description }} */
- {% endif %}
rpc {{ service.service }}-{{ method.method }} {
+ description
+ "{{ method.description }}";
{% if method.input %}
input {
{% if method.input_ref %}
- uses {{ method.input }} ;
+ uses {{ method.input }};
{% else %}
leaf {{ method.input }} {
- type {{ method.input }} ;
+ type {{ method.input }};
}
{% endif %}
}
@@ -183,10 +170,10 @@
{% if method.output %}
output {
{% if method.output_ref %}
- uses {{ method.output }} ;
+ uses {{ method.output }};
{% else %}
leaf {{ method.output }} {
- type {{ method.output }} ;
+ type {{ method.output }};
}
{% endif %}
}
@@ -418,7 +405,8 @@
# TODO: We should have a separate file for each output. There is an
# issue reusing the same filename with an incremental suffix. Using
# a different file name works but not the actual proto file name
- f.name = proto_file.name.replace('.proto', '.yang')
+ f.name = '{}-{}'.format('ietf', proto_file.name.replace('.proto',
+ '.yang'))
# f.name = '{}_{}{}'.format(_rchop(proto_file.name, '.proto'), idx,
# '.yang')
# idx += 1
diff --git a/experiments/proto2Yang/yang.proto b/experiments/proto2yang/yang.proto
similarity index 81%
rename from experiments/proto2Yang/yang.proto
rename to experiments/proto2yang/yang.proto
index 94959a3..718951c 100644
--- a/experiments/proto2Yang/yang.proto
+++ b/experiments/proto2yang/yang.proto
@@ -2,8 +2,6 @@
package experiment;
-//import "google/protobuf/empty.proto";
-
message AsyncEvent {
int32 seq = 1;
enum EventType {
@@ -15,27 +13,26 @@
string details = 3;
}
-enum khenType {
- BIG_KHEN = 0;
- SMALL_KHEN = 1;
- NO_KHEN = 2;
+enum SimpleEnum {
+ APPLE = 0;
+ BANANA = 1;
+ ORANGE = 2;
}
-
message Packet {
int32 source = 1;
bytes content = 2;
- message Result {
+ message InnerPacket {
string url = 1;
string title = 2;
repeated string snippets = 3;
- message Success {
+ message InnerInnerPacket {
string input = 1;
string desc = 2;
}
- repeated Success success = 4;
+ repeated InnerInnerPacket inner_inner_packet = 4;
}
- repeated Result results = 3;
+ repeated InnerPacket inner_packets = 3;
}
message Echo {