summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/NodeApplicationManager/Containers_Info_Map.cpp
blob: 9f790eb4d4539840dfd6cfcdb872d0c7d76c98d8 (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
// $Id$
#include "Containers_Info_Map.h"
#include "ciao/CIAO_Config.h"
#include "ciao/CIAO_common.h"

namespace CIAO
{
  Containers_Info_Map::
  Containers_Info_Map (const Deployment::DeploymentPlan & plan)
    : map_ (CIAO_DEFAULT_MAP_SIZE),
      plan_ (plan)
  {
    this->initialize_map ();
    this->build_map ();
  }
  
  Deployment::ContainerImplementationInfos *
  Containers_Info_Map::containers_info (void)
  {
    // Fetch the information from the <containers_info_map_>
    // and return the corresponding sequence
    Deployment::ContainerImplementationInfos_var retv;

    ACE_NEW_RETURN (retv, 
                    Deployment::ContainerImplementationInfos,
                    0);

    Iterator end = this->map_.end ();
    CORBA::ULong i = 0;

    for (Iterator b = this->map_.begin ();
         b != end;
         ++b)
      {
        retv->length (i + 1);
        (*retv)[i] = *((*b).int_id_);

        ++i;
      }
    return retv._retn ();
  }
  
  void
  Containers_Info_Map::
  initialize_map (void)
  {
    const CORBA::ULong instance_len = this->plan_.instance.length ();

    // Iterate over the instance list and look at the policy set id of each
    // component instance. For each policy set, we create a separate container
    // to host all the components with such policy set.
    for (CORBA::ULong i = 0; i < instance_len; ++i)
      {
        const char * my_resource_id = "";
        const char * my_policy_set_id = "";

        if (this->plan_.instance[i].deployedResource.length () != 0)
          {
            my_resource_id = 
              this->plan_.instance[i].deployedResource[0].resourceName.in ();

            this->plan_.instance[i].deployedResource[0].resourceValue >>=
              my_policy_set_id;
          }

        // If we find a different policy_set_id, then we bind it.
        if (this->map_.find (my_policy_set_id) == 0)
          continue;
        else if (ACE_OS::strcmp (my_policy_set_id, "") == 0)
          {
            // empty policy_set_id
            Deployment::ContainerImplementationInfo * info;
            ACE_NEW (info, Deployment::ContainerImplementationInfo);
            this->map_.bind (my_policy_set_id, info);
            continue;
          }
        else
          {
            Deployment::ContainerImplementationInfo * info;
            ACE_NEW (info, Deployment::ContainerImplementationInfo);

            // Fetch the actual policy_set_def from the infoProperty
            // Ugly due to the IDL data structure definition! :(
            CORBA::ULong j;
            for (j = 0; 
                  j < this->plan_.infoProperty.length (); 
                  ++j)
              {
                CIAO::DAnCE::ServerResource *server_resource_def = 0;
                this->plan_.infoProperty[j].value >>= server_resource_def;
                if (ACE_OS::strcmp ((*server_resource_def).Id, 
                                    my_resource_id) == 0)
                  {
                    // Iterate over the policy_sets
                    CORBA::ULong k;
                    for (k = 0;
                          k < (*server_resource_def).orb_config.policy_set.length ();
                          ++k)
                      {
                        if (ACE_OS::strcmp (my_policy_set_id,
                          (*server_resource_def).orb_config.policy_set[k].Id) == 0)
                          {
                            // Foud the target policy set def
                            info->container_config.length (1);
                            info->container_config[0].name = 
                              CORBA::string_dup ("ContainerPolicySet");
                            info->container_config[0].value <<= 
                              (*server_resource_def).orb_config.policy_set[k];
                          }
                      }
                    if (k == (*server_resource_def).orb_config.policy_set.length ())
                      {
                        // No Server Resource Def found?
                        ACE_DEBUG ((LM_DEBUG, "No matching policy set def found!\n"));
                      }
                  }
              } // end of for loop for fetching policy_set_def

            if (j == this->plan_.infoProperty.length ()) 
              {
                // No Server Resource Def found?! Inconsistent descriptor files.
                ACE_DEBUG ((LM_ERROR, "(%P|%t) Descriptor error: "
                  "No matching server resrouce def found for component: %s!\n",
                  this->plan_.instance[i].name.in ()));
              }
            this->map_.bind (my_policy_set_id, info);
          }
      }
  }

  bool
  Containers_Info_Map::
  build_map (void)
  {
    const CORBA::ULong instance_len = this->plan_.instance.length ();

    for (CORBA::ULong i = 0; i < instance_len; ++i)
      {
        const Deployment::InstanceDeploymentDescription & instance =
          this->plan_.instance[i];

        if (! this->insert_instance_into_map (instance))
          return false;
      }

    return true;
  }

  bool
  Containers_Info_Map::insert_instance_into_map (
    const Deployment::InstanceDeploymentDescription & instance)
  {
    Deployment::ContainerImplementationInfo container_info;

    const char * policy_set_id = "";
    if (instance.deployedResource.length () != 0)
      {
        instance.deployedResource[0].resourceValue >>= policy_set_id;
      }

    // Find the ContainerImplementationInfo entry from the map
    MAP::ENTRY *entry;
    if (this->map_.find (policy_set_id, entry) != 0)
      return false; //should never happen
    else
      {
        this->insert_instance_into_container (
                instance, 
                entry->int_id_->impl_infos);
      }

    return true;
  }

  bool
  Containers_Info_Map::insert_instance_into_container (
      const Deployment::InstanceDeploymentDescription & instance,
      Deployment::ComponentImplementationInfos & impl_infos)
  {
    // Increase the length of the ComponentImplementationInfos by one
    CORBA::ULong i = impl_infos.length ();
    impl_infos.length (i + 1);

    // Fill in the information about this component instance
    // Get the component instance name.
    impl_infos[i].component_instance_name = instance.name.in ();

    const Deployment::MonolithicDeploymentDescription & impl =
      this->plan_.implementation[instance.implementationRef];

    const CORBA::ULong artifact_num = impl.artifactRef.length ();

    // Copy Component instance related Properties if there is any.
    if (instance.configProperty.length () > 0)
      {
        impl_infos[i].component_config = instance.configProperty;
      }

    bool svnt_found = false;
    bool exec_found = false;

    // For svnt artifact
    for (CORBA::ULong j = 0; j < artifact_num; ++j)
      {
        const Deployment::ArtifactDeploymentDescription & arti =
          this->plan_.artifact[ impl.artifactRef[j] ];

        ACE_CString tmp = arti.name.in ();
        ssize_t pos;

        //@@ Note: I am not checking for redundancy here. Maybe
        //         the modeling tool should make sure of
        //         uniqueness, i.e., one component implementation
        //         should have only 1 _svnt and 1 _exec libs.
        if (!svnt_found &&
                ((pos  = tmp.find ("_svnt")) != ACE_CString::npos ||
                (pos  = tmp.find ("_Svnt")) != ACE_CString::npos))
          {
            if (arti.location.length() < 1 )
              {
                ACE_DEBUG ((LM_DEBUG, "Servant Artifact must have a location!\n"));
                return  0;
              }

            svnt_found = true;
            // Copy the servant dll/so name.
            // @@ Note: I ignore all the other locations except the first one.
            impl_infos[i].servant_dll = 
              CORBA::string_dup (arti.location[0].in ());

            // Get the entry point.
		        const CORBA::ULong prop_length = arti.execParameter.length ();

            for (CORBA::ULong prop_num = 0;
                prop_num < prop_length;
                ++prop_num)
              {
                ACE_CString name (arti.execParameter[prop_num].name.in ());
                if (name == ACE_CString ("entryPoint"))
                  {
                    const char * entry;
                    (arti.execParameter[prop_num].value) >>= entry;
                    impl_infos[i].servant_entrypt = CORBA::string_dup (entry);
                  }
                else
                  {
                    ACE_DEBUG ((LM_DEBUG, "Found unknown property in the artifact!\n"));
                    ACE_DEBUG ((LM_DEBUG, "We only support entrypoint at this point in CIAO.\n"));
                  }
              }
          }

        // As one can see, code is duplicated here. I will come back for this later.
        // For exec artifact
        if (!exec_found &&
                ((pos  = tmp.find ("_exec")) != ACE_CString::npos ||
                (pos  = tmp.find ("_Exec")) != ACE_CString::npos))
          {
            if (arti.location.length() < 1 )
              {
                ACE_DEBUG ((LM_DEBUG, "Executor Artifact must have a location!\n"));
                return 0;
              }

            exec_found = true;
            // Cpoy the servant dll/so name.
            // @@ Note: I ignore all the other locations except the first one.
            exec_found = true;
            impl_infos[i].executor_dll = 
              CORBA::string_dup (arti.location[0].in ());

            // Get the entry point.
            const CORBA::ULong prop_length = arti.execParameter.length ();
            for (CORBA::ULong prop_num = 0;
                prop_num < prop_length;
                ++prop_num)
              {
                ACE_CString name (arti.execParameter[prop_num].name.in ());
                if (name == ACE_CString ("entryPoint"))
                  {
                    const char * entry;
                    (arti.execParameter[prop_num].value) >>= entry;
                    impl_infos[i].executor_entrypt = CORBA::string_dup (entry);
                  }
                else
                  {
                    ACE_DEBUG ((LM_DEBUG, "Found unknown property in the artifact!\n"));
                    ACE_DEBUG ((LM_DEBUG, "We only support entrypoint at this point in CIAO.\n"));
                  }
              }
          }
        else
          // We see artifact other than servant/executor and we ignore them.
          continue;
      }
    return true;
  }
}