Doxygen Book
ml_agent.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-only */
15 #include <json-glib/json-glib.h>
16 #include <nnstreamer_log.h>
17 #include <mlops-agent-interface.h>
18 
19 #include "ml_agent.h"
20 
21 const gchar URI_SCHEME[] = "mlagent";
22 const gchar URI_KEYWORD_MODEL[] = "model";
23 const gchar JSON_KEY_MODEL_PATH[] = "path";
24 
32 gchar *
33 mlagent_get_model_path_from (const GValue * val)
34 {
35  g_autofree gchar *scheme = NULL;
36  g_autofree gchar *uri = g_value_dup_string (val);
37  GError *err = NULL;
38  gchar *uri_hier_part;
39  gchar **parts;
40 
41  if (!uri)
42  return NULL;
43 
45  scheme = g_uri_parse_scheme (uri);
46  if (!scheme || g_strcmp0 (URI_SCHEME, scheme)) {
47  return g_steal_pointer (&uri);
48  }
49 
50  uri_hier_part = g_strstr_len (uri, -1, ":");
51  while (*uri_hier_part == ':' || *uri_hier_part == '/') {
52  uri_hier_part++;
53  }
54 
63  parts = g_strsplit_set (uri_hier_part, "/", 0);
64  {
65  enum MODEL_PART_CONSTANTS
66  {
67  MODEL_PART_IDX_NAME = 1,
68  MODEL_PART_IDX_VERSION = 2,
69  MODEL_VERSION_MIN = 1,
70  MODEL_VERSION_MAX = 255,
71  };
72  const size_t NUM_PARTS_MODEL = 3;
73  size_t num_parts;
74 
75  num_parts = g_strv_length (parts);
76  if (num_parts == 0) {
77  goto fallback;
78  }
79 
80  if (!g_strcmp0 (parts[0], URI_KEYWORD_MODEL)) {
82  g_autofree gchar *name = g_strdup (parts[MODEL_PART_IDX_NAME]);
83  g_autofree gchar *stringified_json = NULL;
84  g_autoptr (JsonParser) json_parser = NULL;
85  gint rcode;
86 
87  if (num_parts < NUM_PARTS_MODEL - 1) {
88  goto fallback;
89  }
90 
95  if (num_parts == NUM_PARTS_MODEL - 1) {
96  rcode = ml_agent_model_get_activated (name, &stringified_json);
97  } else {
98  guint version = strtoul (parts[MODEL_PART_IDX_VERSION], NULL, 10);
99  rcode = ml_agent_model_get (name, version, &stringified_json);
100  }
101 
102  if (rcode != 0) {
103  nns_loge
104  ("Failed to get the stringified JSON using the given URI(%s)", uri);
105  goto fallback;
106  }
107 
108  json_parser = json_parser_new ();
110  if (!json_parser_load_from_data (json_parser, stringified_json, -1, &err)) {
111  nns_loge ("Failed to parse the stringified JSON while "
112  "get the model's path: %s",
113  (err ? err->message : "unknown reason"));
114  goto fallback;
115  }
116  g_clear_error (&err);
117 
118  {
119  const gchar *path = NULL;
120  JsonNode *jroot;
121  JsonObject *jobj;
122 
123  jroot = json_parser_get_root (json_parser);
124  if (jroot == NULL) {
125  nns_loge ("Failed to get JSON root node while get the model's path");
126  goto fallback;
127  }
128 
129  jobj = json_node_get_object (jroot);
130  if (jobj == NULL) {
131  nns_loge
132  ("Failed to get JSON object from the root node while get the model's path");
133  goto fallback;
134  }
135 
136  if (!json_object_has_member (jobj, JSON_KEY_MODEL_PATH)) {
137  nns_loge
138  ("Failed to get the model's path from the given URI: "
139  "There is no key named, %s, in the JSON object",
141  goto fallback;
142  }
143 
144  path = json_object_get_string_member (jobj, JSON_KEY_MODEL_PATH);
145  if (path == NULL || !g_strcmp0 (path, "")) {
146  nns_loge
147  ("Failed to get the model's path from the given URI: "
148  "Invalid value for the key, %s", JSON_KEY_MODEL_PATH);
149  goto fallback;
150  }
151 
152  g_strfreev (parts);
153  return g_strdup (path);
154  }
155  }
156  }
157 
158 fallback:
159  g_clear_error (&err);
160  g_strfreev (parts);
161 
162  return g_strdup (uri);
163 }
URI_KEYWORD_MODEL
const gchar URI_KEYWORD_MODEL[]
Definition: ml_agent.c:22
mlagent_get_model_path_from
gchar * mlagent_get_model_path_from(const GValue *val)
Get a path of the model file from a given GValue.
Definition: ml_agent.c:33
nnstreamer_log.h
Internal log util for NNStreamer plugins and native APIs.
JSON_KEY_MODEL_PATH
const gchar JSON_KEY_MODEL_PATH[]
Definition: ml_agent.c:23
nns_loge
#define nns_loge
Definition: nnstreamer_log.h:142
URI_SCHEME
const gchar URI_SCHEME[]
Definition: ml_agent.c:21
ml_agent.h
Internal header to make a bridge between NNS filters and the ML Agent service.