summaryrefslogtreecommitdiff
path: root/Controller/Component/Descriptor.cpp
blob: 22cb980280c6d8bfd2708a058e4b5374be014a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#ifndef DESCRIPTOR_CPP
#define DESCRIPTOR_CPP

#include "Descriptor.h"
#include "Config_Handlers/DnC_Dump.h"
#include <sstream>
#include "ace/SString.h"
namespace CIAO
{
  namespace RACE
  {
    namespace CIDL_Controller_Impl
    {
      #define CRITICAL_PATH "edu.vanderbilt.dre.CIAO.RACE.criticalPath"
      #define DEADLINE "/Deadline"
      #define IMPORTANCE "/Importance"
      #define ELEMENT ';'
      #define PORT ','

      Descriptor::Descriptor (Poller *poller)
        : poller_ (poller)
      {
      }

      Descriptor::~Descriptor ()
      {}

      int
      Descriptor::parse_deployment_plan
      (const ::Deployment::DeploymentPlan & plan,
       App_String & app_string)
      {
        /// First dump the plan.
        //        Deployment::DnC_Dump::dump (plan);

        /// Frst get the plan id and the label.
        app_string.plan_id_ = CORBA::string_dup (plan.UUID);
        app_string.label_ = CORBA::string_dup (plan.label);
        /// Now get the InstanceDeploymentDescriptions which contains the
        /// information about the components in this plan.
        Deployment::InstanceDeploymentDescriptions idd = plan.instance;

        /// Now parse the InstanceDeploymentDescriptions to get the
        /// component_id and the name of the node where the component is
        /// depoyed.

        if (idd.length () > 0)
          {

            for (CORBA::ULong i (0); i < idd.length (); ++i)
              {
                Component_Info comp_info;
                comp_info.id_ = CORBA::string_dup (idd [i].name);
                comp_info.node_ = CORBA::string_dup (idd [i].node);
                app_string.components_.push_back (comp_info);
              }
            return 0;

          }
        return -1;

      }

      void
      Descriptor::unregister_string (const char * UUID)
        throw (::CORBA::SystemException)
      {}


      void
      Descriptor::register_string
      (const ::Deployment::PackageConfiguration & pcd,
       const ::Deployment::DeploymentPlan & plan
       ACE_ENV_ARG_DECL)
        ACE_THROW_SPEC ((::CORBA::SystemException))
      {
        ACE_DEBUG ((LM_DEBUG, "Descriptor::push_descriptors.\n"));

        App_String app_string;
        /// Register the string with the poller only if the input
        /// descriptors contain the critical path information.
        if ((this->parse_package_config (pcd, app_string) == 0) &&
            (this->parse_deployment_plan (plan, app_string) == 0))
          {
            this->poller_->register_string (app_string);
            this->poller_->start_poller ();
          }
      }

      int
      Descriptor::parse_package_config
      (const ::Deployment::PackageConfiguration &pcd,
       App_String & app_string)
      {
        /// First get the ComponentPackageDescriptions from the
        /// PackageConfiguration.
        Deployment::ComponentPackageDescriptions base_package
          = pcd.basePackage;

        /// Now iterate thro each element in the
        /// ComponentPackageDescription sequence.

        for (CORBA::ULong i = 0; i < base_package.length (); ++i)
          {
            /// Obtaining the PackagedComponentImplementations from each
            /// element in the sequence.
            Deployment::PackagedComponentImplementations &impl =
              base_package [i].implementation;

            /// Now iterate thro each element in the
            /// PackagedComponentImplementation sequence.
            for (CORBA::ULong j = 0; j < impl.length (); ++j)
              {
                /// Ontaining the ComponentImplementationDescription form
                /// each element in the sequence.
                Deployment::ComponentImplementationDescription &cid =
                  impl [j].referencedImplementation;

                /// After getting the CID, parse the CID to obtain the
                /// critical path sequence.

                /// Since there is only one critical path now, if a
                /// critical path is found, there is no need to parse the
                /// remaining config properties.
                if (this->parse_path (cid.configProperty, app_string) == 0)
                  {
                    return 0;
                  }
              }
          }
        return -1;

      }

      char*
      Descriptor::get_path_name (Deployment::Properties &properties)
      {
        const char * path_name;

        /// Get the name of the critical path by seaching for the string
        /// edu.vanderbilt.dre.CIAO.RACE.criticalPath.
        for (CORBA::ULong ctr (0); ctr < properties.length (); ++ctr)
          {
            Deployment::Property & property = properties [ctr];
            if (ACE_OS::strcmp (property.name.in (),
                                CRITICAL_PATH) == 0)
              {
                ACE_DEBUG ((LM_DEBUG, "Found critical path\n"));

                CORBA::Any & value = property.value;
                CORBA::TypeCode_var tc = value.type ();
                if (tc->kind () == CORBA::tk_string)
                  {
                    value >>= path_name;
                    return CORBA::string_dup (path_name);
                  }
              }
          }
        return 0;
      }

      CORBA::Double
      Descriptor::get_deadline (Deployment::Properties &properties,
                                const char *path_name)
      {
        /// Now search for path_name+"_Deadline"
        std::stringstream path_deadline;
        path_deadline << path_name;
        path_deadline << DEADLINE;
        CORBA::Double deadline;

        for (CORBA::ULong ctr = 0; ctr < properties.length (); ++ctr)
          {
            Deployment::Property & property = properties [ctr];
            if (ACE_OS::strcmp (property.name.in (),
                                path_deadline.str ().c_str ()) == 0)
              {
                CORBA::Any & value = property.value;
                CORBA::TypeCode_var tc = value.type ();
                if (tc->kind () == CORBA::tk_double)
                  {
                    value >>= deadline;
                    ACE_DEBUG ((LM_DEBUG, "Critical path deadline is:%.2f\n",
                                deadline));
                    return deadline;

                  }
              }
          }
        return 0;
      }

      CORBA::Long
      Descriptor::get_importance (Deployment::Properties &properties,
                                  const char *path_name)
      {
        /// Now search for path_name+"/Importance"
        std::stringstream path_importance;
        path_importance << path_name;
        path_importance << IMPORTANCE;
        CORBA::Long importance;

        for (CORBA::ULong ctr = 0; ctr < properties.length (); ++ctr)
          {
            Deployment::Property & property = properties [ctr];
            if (ACE_OS::strcmp (property.name.in (),
                                path_importance.str ().c_str ()) == 0)
              {
                CORBA::Any & value = property.value;
                CORBA::TypeCode_var tc = value.type ();
                if (tc->kind () == CORBA::tk_long)
                  {
                    value >>= importance;
                    ACE_DEBUG ((LM_DEBUG, "App-string importance is: %d\n",
                                importance));
                    return importance;
                  }
              }
          }
        return 0;
      }

      char*
      Descriptor::get_path (Deployment::Properties &properties,
                            const char *path_name)
      {
        const char *path;
        /// Now search for path_name
        for (CORBA::ULong ctr = 0; ctr < properties.length (); ++ctr)
          {
            Deployment::Property & property = properties [ctr];
            if (ACE_OS::strcmp (property.name.in (),
                                path_name) == 0)
              {
                CORBA::Any & value = property.value;
                CORBA::TypeCode_var tc = value.type ();
                if (tc->kind () == CORBA::tk_string)
                  {
                    value >>= path;
                    ACE_DEBUG ((LM_DEBUG, "Critical path is:%s\n",
                                path));
                    return CORBA::string_dup (path);
                  }
              }
          }

        return 0;
      }


      int
      Descriptor::parse_path (Deployment::Properties &properties,
                              App_String & app_string)
      {
        /// Dump all the properties.
        //         for (CORBA::ULong ctr (0); ctr < properties.length (); ++ctr)
        //           Deployment::DnC_Dump::dump (properties [ctr]);

        CORBA::String_var critical_path_name;
        CORBA::String_var critical_path;

        critical_path_name =  this->get_path_name (properties);
        if (critical_path_name.in ())
          {
            /// Populate the deadline filed of the app_string.
            app_string.deadline_ =
              this->get_deadline (properties, critical_path_name.in ());

            app_string.importance_ =
              this->get_importance (properties, critical_path_name.in ());

            /// @TODO: Right now if the importance of the string is > 0, we
            /// assume that the string is of guaranteed class. This may not
            /// be true.

            if (app_string.importance_ > 0)
              {
                app_string.group_ = GUARANTEED;
              }
            else
              {
                app_string.group_ = BEST_EFFORT;
              }


            critical_path =
              this->get_path (properties, critical_path_name.in ());

            if (critical_path.in ())
              {
                /// Populate the critical path filed of the app_string.
                app_string.critical_path_ =
                  this->tokenize_path (critical_path.in ());
                return 0;
              }
          }
        return -1;
      }


      Critical_Path
      Descriptor::tokenize_path (const char* path)
      {
        CORBA::String_var critical_path (CORBA::string_dup (path));
        ACE_Tokenizer tok (critical_path);
        Path_Element path_element;
        Critical_Path path_seq;
        CORBA::ULong ctr = 0;
        tok.delimiter_replace (ELEMENT, 0);
        for (char *p = tok.next (); p; p = tok.next (), ++ctr)
          {
            ACE_DEBUG ((LM_DEBUG, "%s\n", p));
            ACE_Tokenizer temp_tok (p);
            temp_tok.delimiter_replace (PORT, 0);

            path_element.node = CORBA::string_dup (temp_tok.next ());
            path_element.src = CORBA::string_dup (temp_tok.next ());
            path_element.dst = CORBA::string_dup (temp_tok.next ());

            path_seq.length (ctr + 1);
            path_seq [ctr] = path_element;
          }
        return path_seq;
      }
    }
  }
}

#endif /*DESCRIPTOR_CPP */