From a96145115db3f9aab066b5b94ecc3c6c53ae5463 Mon Sep 17 00:00:00 2001
From: Lluis Gifre <lluis.gifre@cttc.es>
Date: Thu, 14 Jul 2022 17:57:06 +0200
Subject: [PATCH] Proto files:

- fixed issue with messages in policy.proto and context-policy.proto
- modified and unified script to generate Python code from proto files
- segregated script to generate UML code
- added/modified apropriate .gitignore files to prevent uploading generated code. It should be generated locally or during creation of Docker containers
- added symbolic link from common/proto to folder containing python generated code
---
 proto/.gitignore                      |  7 ++++-
 proto/context-policy.proto            | 10 +++----
 proto/generate_code_python.sh         | 42 +++++++++++++++++++++++++++
 proto/{compile.sh => generate_uml.sh} |  3 --
 proto/policy.proto                    |  6 ++--
 proto/src/java/.gitignore             |  5 ++++
 proto/src/python/.gitignore           |  5 ++++
 proto/src/python/__init__.py          | 13 +++++++++
 src/common/proto                      |  1 +
 9 files changed, 80 insertions(+), 12 deletions(-)
 create mode 100755 proto/generate_code_python.sh
 rename proto/{compile.sh => generate_uml.sh} (89%)
 create mode 100644 proto/src/java/.gitignore
 create mode 100644 proto/src/python/.gitignore
 create mode 100644 proto/src/python/__init__.py
 create mode 120000 src/common/proto

diff --git a/proto/.gitignore b/proto/.gitignore
index 79d17a55d..d1dea37b3 100644
--- a/proto/.gitignore
+++ b/proto/.gitignore
@@ -1,2 +1,7 @@
-src/
+src/*/*
+
+# used to prevent breaking symbolic links from source code folders
+!src/*/.gitignore
+!src/python/__init__.py
+
 uml/generated
diff --git a/proto/context-policy.proto b/proto/context-policy.proto
index 84a5c0edb..9fe25dec5 100644
--- a/proto/context-policy.proto
+++ b/proto/context-policy.proto
@@ -20,9 +20,9 @@ import "policy.proto";
 
 // created as a separate service to prevent import-loops in context and policy
 service ContextPolicyService {
-  rpc ListPolicyRuleIds  (context.Empty      ) returns (       policy.PolicyRuleIdList) {}
-  rpc ListPolicyRules    (context.Empty      ) returns (       policy.PolicyRuleList  ) {}
-  rpc GetPolicyRule      (policy.PolicyRuleId) returns (       policy.PolicyRule      ) {}
-  rpc SetPolicyRule      (policy.PolicyRule  ) returns (       policy.PolicyRuleId    ) {}
-  rpc RemovePolicyRule   (policy.PolicyRuleId) returns (       context.Empty          ) {}
+  rpc ListPolicyRuleIds(context.Empty         ) returns (policy.PolicyRuleIdList) {}
+  rpc ListPolicyRules  (context.Empty         ) returns (policy.PolicyRuleList  ) {}
+  rpc GetPolicyRule    (policy.PolicyRuleId   ) returns (policy.PolicyRuleBasic ) {}
+  rpc SetPolicyRule    (policy.PolicyRuleBasic) returns (policy.PolicyRuleId    ) {}
+  rpc RemovePolicyRule (policy.PolicyRuleId   ) returns (context.Empty          ) {}
 }
diff --git a/proto/generate_code_python.sh b/proto/generate_code_python.sh
new file mode 100755
index 000000000..b0df357eb
--- /dev/null
+++ b/proto/generate_code_python.sh
@@ -0,0 +1,42 @@
+#!/bin/bash -eu
+# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Make folder containing the script the root folder for its execution
+cd $(dirname $0)
+
+mkdir -p src/python
+rm -rf src/python/*.py
+
+tee src/python/__init__.py << EOF > /dev/null
+# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+EOF
+
+# Generate Python code
+python3 -m grpc_tools.protoc -I=./ --python_out=src/python/ --grpc_python_out=src/python/ *.proto
+
+# Arrange generated code imports to enable imports from arbitrary subpackages
+find src/python -type f -iname *.py -exec sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' {} \;
diff --git a/proto/compile.sh b/proto/generate_uml.sh
similarity index 89%
rename from proto/compile.sh
rename to proto/generate_uml.sh
index 520ead69a..6f20b6f4c 100755
--- a/proto/compile.sh
+++ b/proto/generate_uml.sh
@@ -13,12 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-python3 -m grpc_tools.protoc -I=./ --python_out=../src/ --grpc_python_out=../src/ *.proto
 #requires installation of protoc-gen-uml
 export PATH=${HOME}/protoc-gen-uml/target/universal/stage/bin:$PATH
 protoc --uml_out=uml/ -I ./ *.proto
 cd uml
 java -jar plantuml.7997.jar *.puml
 rm *.puml
-
-
diff --git a/proto/policy.proto b/proto/policy.proto
index dc3f700b4..9e686f180 100644
--- a/proto/policy.proto
+++ b/proto/policy.proto
@@ -98,7 +98,7 @@ message PolicyRuleDeviceList {
   repeated PolicyRuleDevice policyRuleDeviceList = 1;
 }
 
-// A list of policy rule Ids
-message PolicyRuleIdList {
-  repeated PolicyRuleId policyRuleIds = 1;
+// A list of policy rules
+message PolicyRuleList {
+  repeated PolicyRuleId policyRules = 1;
 }
diff --git a/proto/src/java/.gitignore b/proto/src/java/.gitignore
new file mode 100644
index 000000000..3f93458ac
--- /dev/null
+++ b/proto/src/java/.gitignore
@@ -0,0 +1,5 @@
+*
+
+# used to prevent breaking symbolic links from source code folders
+!.gitignore
+!__init__.py
diff --git a/proto/src/python/.gitignore b/proto/src/python/.gitignore
new file mode 100644
index 000000000..3f93458ac
--- /dev/null
+++ b/proto/src/python/.gitignore
@@ -0,0 +1,5 @@
+*
+
+# used to prevent breaking symbolic links from source code folders
+!.gitignore
+!__init__.py
diff --git a/proto/src/python/__init__.py b/proto/src/python/__init__.py
new file mode 100644
index 000000000..9953c8205
--- /dev/null
+++ b/proto/src/python/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/src/common/proto b/src/common/proto
new file mode 120000
index 000000000..0ae252a78
--- /dev/null
+++ b/src/common/proto
@@ -0,0 +1 @@
+../../proto/src/python
\ No newline at end of file
-- 
GitLab