From 2282d778d8631b0009ef5b9378557e8216094b6f Mon Sep 17 00:00:00 2001 From: DinethH Date: Fri, 3 May 2024 11:48:51 +0530 Subject: [PATCH] Add name generation for apk-conf --- .../config-deployer-service/ballerina/APIClient.bal | 7 ++++++- .../java/org/wso2/apk/config/RuntimeAPICommonUtil.java | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 9151f2119..549ae6423 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -46,7 +46,7 @@ public class APIClient { encodedString = encodedString.substring(0, encodedString.length() - 1); } APKConf apkConf = { - name: api.getName(), + name: api.getType() == API_TYPE_GRPC ? self.getUniqueNameForGrpcApi(api.getName()) : api.getName(), basePath: api.getBasePath().length() > 0 ? api.getBasePath() : encodedString, version: api.getVersion(), 'type: api.getType() == "" ? API_TYPE_REST : api.getType().toUpperAscii() @@ -1679,6 +1679,11 @@ public class APIClient { return hashedValue.toBase16(); } + public isolated function getUniqueNameForGrpcApi(string concatanatedServices) returns string { + byte[] hashedValue = crypto:hashSha1(concatanatedServices.toBytes()); + return hashedValue.toBase16(); + } + public isolated function retrieveHttpRouteRefName(APKConf apkConf, string 'type, commons:Organization organization) returns string { return uuid:createType1AsString(); } diff --git a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java index ebca7e8e4..c93073e6d 100644 --- a/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java +++ b/runtime/config-deployer-service/java/src/main/java/org/wso2/apk/config/RuntimeAPICommonUtil.java @@ -148,7 +148,11 @@ private static API getGRPCAPIFromProtoDefinition(String definition) { API api = new API(); api.setBasePath("/"+protoParser.protoFile.basePath); api.setVersion(protoParser.protoFile.version); + StringBuilder apiName = new StringBuilder(); + List sortedServices = new ArrayList<>(); + for (ProtoParser.Service service : protoParser.getServices()) { + sortedServices.add(service.name); for (String method : service.methods) { URITemplate uriTemplate = new URITemplate(); uriTemplate.setUriTemplate(protoParser.protoFile.packageName + "." + service.name); @@ -156,6 +160,12 @@ private static API getGRPCAPIFromProtoDefinition(String definition) { uriTemplates.add(uriTemplate); } } + sortedServices.sort(String::compareTo); + for (String service : sortedServices) { + apiName.append(service).append("-"); + } + apiName.deleteCharAt(apiName.length() - 1); + api.setName(apiName.toString()); api.setUriTemplates(uriTemplates.toArray(new URITemplate[uriTemplates.size()])); return api;