summaryrefslogtreecommitdiff
path: root/osinfo/osinfo_deployment.c
blob: 3678fa2da5235482f6a0247a7755565cd2be0edf (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * libosinfo:
 *
 * Copyright (C) 2009-2012 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors:
 *   Arjun Roy <arroy@redhat.com>
 *   Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <osinfo/osinfo.h>
#include <glib/gi18n-lib.h>

G_DEFINE_TYPE (OsinfoDeployment, osinfo_deployment, OSINFO_TYPE_ENTITY);

#define OSINFO_DEPLOYMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), OSINFO_TYPE_DEPLOYMENT, OsinfoDeploymentPrivate))

/**
 * SECTION:osinfo_deployment
 * @short_description: An virtualization deployment
 * @see_also: #OsinfoOs, #OsinfoDeployment
 *
 * #OsinfoDeployment is an entity representing an virtualization
 * deployment. Deployments have a list of supported devices
 */

struct _OsinfoDeploymentPrivate
{
    // Value: List of device_link structs
    GList *deviceLinks;

    OsinfoOs *os;
    OsinfoPlatform *platform;
};


enum {
    PROP_0,

    PROP_OS,
    PROP_PLATFORM,
};


static void
osinfo_deployment_set_property(GObject *object,
                               guint property_id,
                               const GValue *value,
                               GParamSpec *pspec)
{
    OsinfoDeployment *deployment = OSINFO_DEPLOYMENT (object);

    switch (property_id)
        {
        case PROP_OS:
            if (deployment->priv->os)
                g_object_unref(deployment->priv->os);
            deployment->priv->os = g_value_get_object(value);
            if (deployment->priv->os)
                g_object_ref(deployment->priv->os);
            break;
        case PROP_PLATFORM:
            if (deployment->priv->platform)
                g_object_unref(deployment->priv->platform);
            deployment->priv->platform = g_value_get_object(value);
            if (deployment->priv->platform)
                g_object_ref(deployment->priv->platform);
            break;
        default:
            /* We don't have any other property... */
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
        }
}

static void
osinfo_deployment_get_property(GObject *object,
                               guint property_id,
                               GValue *value,
                               GParamSpec *pspec)
{
    OsinfoDeployment *deployment = OSINFO_DEPLOYMENT (object);

    switch (property_id)
        {
        case PROP_OS:
            g_value_set_object(value, deployment->priv->os);
            break;
        case PROP_PLATFORM:
            g_value_set_object(value, deployment->priv->platform);
            break;
        default:
            /* We don't have any other property... */
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
            break;
        }
}



static void osinfo_device_link_free(gpointer data, gpointer opaque G_GNUC_UNUSED)
{
    g_object_unref(OSINFO_DEVICELINK(data));
}

static void
osinfo_deployment_finalize (GObject *object)
{
    OsinfoDeployment *deployment = OSINFO_DEPLOYMENT (object);

    g_list_foreach(deployment->priv->deviceLinks, osinfo_device_link_free, NULL);
    g_list_free(deployment->priv->deviceLinks);

    g_object_unref(deployment->priv->os);
    g_object_unref(deployment->priv->platform);

    /* Chain up to the parent class */
    G_OBJECT_CLASS (osinfo_deployment_parent_class)->finalize (object);
}

/* Init functions */
static void
osinfo_deployment_class_init (OsinfoDeploymentClass *klass)
{
    GObjectClass *g_klass = G_OBJECT_CLASS (klass);
    GParamSpec *pspec;

    g_klass->set_property = osinfo_deployment_set_property;
    g_klass->get_property = osinfo_deployment_get_property;

    /**
     * OsinfoDeployment:os:
     *
     * The operating system to be deployed
     */
    pspec = g_param_spec_object("os",
                                "Os",
                                _("Operating system"),
                                OSINFO_TYPE_OS,
                                G_PARAM_CONSTRUCT_ONLY |
                                G_PARAM_READWRITE |
                                G_PARAM_STATIC_STRINGS);
    g_object_class_install_property(g_klass,
                                    PROP_OS,
                                    pspec);
    /**
     * OsinfoDeployment:platform:
     *
     * The platform to deploy on
     */
    pspec = g_param_spec_object("platform",
                                "Platform",
                                _("Virtualization platform"),
                                OSINFO_TYPE_PLATFORM,
                                G_PARAM_CONSTRUCT_ONLY |
                                G_PARAM_READWRITE |
                                G_PARAM_STATIC_STRINGS);
    g_object_class_install_property(g_klass,
                                    PROP_PLATFORM,
                                    pspec);

    g_klass->finalize = osinfo_deployment_finalize;
    g_type_class_add_private (klass, sizeof (OsinfoDeploymentPrivate));
}

static void
osinfo_deployment_init (OsinfoDeployment *deployment)
{
    OsinfoDeploymentPrivate *priv;
    deployment->priv = priv = OSINFO_DEPLOYMENT_GET_PRIVATE(deployment);

    deployment->priv->deviceLinks = NULL;
}


/**
 * osinfo_deployment_new:
 * @id: the unique identifier
 * @os: the operating system to deploy
 * @platform: the platform to deploy on
 *
 * Create a new deployment entity
 *
 * Returns: (transfer full): A deployment entity
 */
OsinfoDeployment *osinfo_deployment_new(const gchar *id,
                                        OsinfoOs *os,
                                        OsinfoPlatform *platform)
{
    return g_object_new(OSINFO_TYPE_DEPLOYMENT,
                        "id", id,
                        "os", os,
                        "platform", platform,
                        NULL);
}


/**
 * osinfo_deployment_get_os:
 * @deployment: the deployment entity
 *
 * Get the operating system for the deployment
 *
 * Returns: (transfer none): an OS, or NULL
 */
OsinfoOs *osinfo_deployment_get_os(OsinfoDeployment *deployment)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);

    return deployment->priv->os;
}


/**
 * osinfo_deployment_get_platform:
 * @deployment: the deployment entity
 *
 * Get the platform for the deployment
 *
 * Returns: (transfer none): a platform, or NULL
 */
OsinfoPlatform *osinfo_deployment_get_platform(OsinfoDeployment *deployment)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);

    return deployment->priv->platform;
}


/**
 * osinfo_deployment_get_preferred_device:
 * @deployment: the deployment entity
 * @filter: (transfer none)(allow-none): a device metadata filter
 *
 * Get the preferred device matching a given filter
 *
 * Returns: (transfer none): a device, or NULL
 */
OsinfoDevice *osinfo_deployment_get_preferred_device(OsinfoDeployment *deployment, OsinfoFilter *filter)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);
    g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);

    OsinfoDeviceLinkFilter *linkfilter = osinfo_devicelinkfilter_new(filter);
    OsinfoDeviceLink *devlink = osinfo_deployment_get_preferred_device_link(deployment, OSINFO_FILTER(linkfilter));
    if (devlink)
        return osinfo_devicelink_get_target(devlink);
    return NULL;
}


/**
 * osinfo_deployment_get_preferred_device_link:
 * @deployment: the deployment entity
 * @filter: (transfer none)(allow-none): a device metadata filter
 *
 * Get the preferred device link matching a given filter and platform.
 * The filter matches against attributes on the link, not the device.
 *
 * Returns: (transfer none): a device, or NULL
 */
OsinfoDeviceLink *osinfo_deployment_get_preferred_device_link(OsinfoDeployment *deployment, OsinfoFilter *filter)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);
    g_return_val_if_fail(OSINFO_IS_FILTER(filter), NULL);

    GList *tmp = deployment->priv->deviceLinks;

    // For each device in section list, apply filter. If filter passes, return device.
    while (tmp) {
        OsinfoDeviceLink *devlink = OSINFO_DEVICELINK(tmp->data);

        if (!filter || osinfo_filter_matches(filter, OSINFO_ENTITY(devlink))) {
           return devlink;
        }

       tmp = tmp->next;
    }

    // If no devices pass filter, return NULL.
    return NULL;
}


/**
 * osinfo_deployment_get_devices:
 * @deployment: a deployment entity
 * @filter: (transfer none)(allow-none): an optional filter
 *
 * Retrieve all the associated devices matching the filter.
 * The filter matches against the device, not the link.
 *
 * Returns: (transfer full): a list of #OsinfoDevice entities
 */
OsinfoDeviceList *osinfo_deployment_get_devices(OsinfoDeployment *deployment, OsinfoFilter *filter)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);
    g_return_val_if_fail(!filter || OSINFO_IS_FILTER(filter), NULL);

    OsinfoDeviceList *newList = osinfo_devicelist_new();
    GList *tmp = deployment->priv->deviceLinks;

    while (tmp) {
        OsinfoDeviceLink *devlink = OSINFO_DEVICELINK(tmp->data);
        OsinfoDevice *dev = osinfo_devicelink_get_target(devlink);
        if (!filter || osinfo_filter_matches(filter, OSINFO_ENTITY(dev)))
            osinfo_list_add(OSINFO_LIST(newList), OSINFO_ENTITY(dev));

        tmp = tmp->next;
    }

    return newList;
}


/**
 * osinfo_deployment_get_device_links:
 * @deployment: a deployment entity
 * @filter: (transfer none)(allow-none): an optional filter
 *
 * Retrieve all the associated devices matching the filter.
 * The filter matches against the link, not the device.
 *
 * Returns: (transfer full): a list of #OsinfoDevice entities
 */
OsinfoDeviceLinkList *osinfo_deployment_get_device_links(OsinfoDeployment *deployment, OsinfoFilter *filter)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);
    g_return_val_if_fail(!filter || OSINFO_IS_FILTER(filter), NULL);

    OsinfoDeviceLinkList *newList = osinfo_devicelinklist_new();
    GList *tmp = deployment->priv->deviceLinks;

    while (tmp) {
        OsinfoDeviceLink *devlink = OSINFO_DEVICELINK(tmp->data);

        if (!filter || osinfo_filter_matches(filter, OSINFO_ENTITY(devlink)))
            osinfo_list_add(OSINFO_LIST(newList), OSINFO_ENTITY(devlink));

        tmp = tmp->next;
    }

    return newList;
}


/**
 * osinfo_deployment_add_device:
 * @deployment: a deployment entity
 * @dev: (transfer none): the device to associate
 *
 * Associate a device with a deployment. The returned #OsinfoDeviceLink
 * can be used to record extra metadata against the link
 *
 * Returns: (transfer none): the device association
 */
OsinfoDeviceLink *osinfo_deployment_add_device(OsinfoDeployment *deployment, OsinfoDevice *dev)
{
    g_return_val_if_fail(OSINFO_IS_DEPLOYMENT(deployment), NULL);
    g_return_val_if_fail(OSINFO_IS_DEVICE(dev), NULL);

    OsinfoDeviceLink *devlink = osinfo_devicelink_new(dev);

    deployment->priv->deviceLinks = g_list_prepend(deployment->priv->deviceLinks, devlink);

    return devlink;
}
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 */