tosca_solver/
lib.rs

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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright (c) 2024 Adam Souzis
// SPDX-License-Identifier: MIT
//! This crate infers the relationships between [TOSCA](https://docs.unfurl.run/tosca.html) node templates when given a set of node templates and their requirements.
//!
//! Each TOSCA requirement found on the node templates is encoded as a set of constraints, including:
//! * node and capability types
//! * the relationship's valid_target_types
//! * node_filter constraints
//! * node_filter match expressions
//!
//! [solve()] will return the nodes that match the requirements associated with a given set of nodes.
//! By default this crate is exposed as a Python extension module and is used by [Unfurl](https://github.com/onecommons/unfurl), but it can be used by any TOSCA 1.3 processor.
use ascent::hashbrown::HashMap;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use std::{time::Duration};
// use log::debug;

mod topology;

pub use topology::{
    Constraint, Criteria, CriteriaTerm, EntityRef, Field, FieldValue, QueryType, SimpleValue,
    ToscaValue,
};

use topology::{sym, Topology};

/// A partial representations of a TOSCA node template (enough for [solve()])
#[cfg_attr(feature = "python", derive(FromPyObject))]
pub struct Node {
    /// node template name
    pub name: String,
    /// TOSCA type of the template
    pub tosca_type: String,
    /// properties, capabilities, requirements
    pub fields: Vec<Field>,
    /// Set if any of its fields has [restrictions](FieldValue)
    pub has_restrictions: bool,
}

/// HashMap mapping tosca type names to a list of ancestor types it inherits (including itsself)
pub type ToscaTypes = HashMap<String, Vec<String>>;

fn get_types(tosca_type: &String, type_parents: &ToscaTypes) -> Vec<String> {
    match type_parents.get(tosca_type) {
        Some(parents) => parents.clone(),
        None => vec![tosca_type.clone()],
    }
}

fn add_field_to_topology(
    f: Field,
    topology: &mut Topology,
    node_name: &String,
    type_parents: &HashMap<String, Vec<String>>,
) -> Result<(), PyErr> {
    match f.value {
        FieldValue::Property { value } => {
            topology
                .property_value
                .push((sym(node_name), None, sym(&f.name), value))
        }
        FieldValue::Capability {
            tosca_type,
            properties,
        } => {
            let cap_name = format!("{node_name}__{cap}", cap = f.name);
            let entityref = EntityRef::Capability(cap_name);
            topology
                .capability
                .push((sym(node_name), sym(&f.name), entityref.clone()));
            for tosca_type in get_types(&tosca_type, type_parents) {
                topology.entity.push((entityref.clone(), tosca_type));
            }
            for field in properties {
                match field.value {
                    FieldValue::Property { value } => topology.property_value.push((
                        sym(node_name),
                        Some(sym(&f.name)),
                        field.name,
                        value,
                    )),
                    _ => {
                        return Err(PyErr::new::<PyTypeError, _>(
                            "This field must be a TOSCA property",
                        ))
                    }
                }
            }
        }
        FieldValue::Requirement {
            terms,
            tosca_type,
            restrictions,
        } => {
            let mut criteria = Criteria::default();
            for term in terms.iter() {
                criteria.0.insert(term.clone());
                match term {
                    CriteriaTerm::NodeName { n } => {
                        topology.req_term_node_name.push((
                            sym(node_name),
                            sym(&f.name),
                            term.clone(),
                            sym(n),
                        ));
                    }
                    CriteriaTerm::NodeType { n } => {
                        topology.req_term_node_type.push((
                            sym(node_name),
                            sym(&f.name),
                            term.clone(),
                            sym(n),
                        ));
                    }
                    CriteriaTerm::CapabilityName { n } => {
                        topology.req_term_cap_name.push((
                            sym(node_name),
                            sym(&f.name),
                            term.clone(),
                            sym(n),
                        ));
                    }
                    CriteriaTerm::CapabilityTypeGroup { names } => {
                        // if any of these match, this CriteriaTerm will be added to the filtered lattice
                        for n in names {
                            topology.req_term_cap_type.push((
                                sym(node_name),
                                sym(&f.name),
                                term.clone(),
                                sym(n),
                            ));
                        }
                    }
                    CriteriaTerm::PropFilter { n, capability, .. } => {
                        topology.req_term_prop_filter.push((
                            sym(node_name),
                            sym(&f.name),
                            term.clone(),
                            capability.clone(),
                            sym(n),
                        ));
                    }
                    CriteriaTerm::NodeMatch { query } => {
                        topology.result.push((
                            sym(node_name),
                            sym(&f.name),
                            0,
                            sym(node_name),
                            false,
                        ));

                        for (index, (q, n)) in query.iter().enumerate() {
                            topology.query.push((
                                sym(node_name),
                                sym(&f.name),
                                index,
                                *q,
                                sym(n),
                                index + 1 == query.len(),
                            ));
                        }

                        topology.req_term_query.push((
                            sym(node_name),
                            sym(&f.name),
                            term.clone(),
                            query.len(), // match the last result
                        ));
                    }
                }
            }
            topology
                .requirement
                .push((node_name.clone(), f.name.clone(), criteria, restrictions));

            if let Some(rel_type) = tosca_type {
                for tosca_type in get_types(&rel_type, type_parents) {
                    topology
                        .relationship
                        .push((node_name.clone(), f.name.clone(), tosca_type));
                }
            }
        } // _ => continue,
    }
    Ok(())
}

/// Add the given node to the Ascent program modeling a topology.
///
/// # Errors
///
/// This function will return a PyTypeError if a field is of an unexpected tosca type.
fn add_node_to_topology(
    node: &Node,
    topology: &mut Topology,
    type_parents: &ToscaTypes,
    req_only: bool,
    include_restrictions: bool,
) -> Result<(), PyErr> {
    let name = sym(&node.name);
    for tosca_type in get_types(&node.tosca_type, type_parents) {
        topology.node.push((sym(&name), sym(&tosca_type)));
    }
    for f in node.fields.iter() {
        if let FieldValue::Requirement { restrictions, .. } = &f.value {
            let has_restrictions = !restrictions.is_empty();
            if has_restrictions != include_restrictions || (!req_only && !include_restrictions) {
                continue; // include_restrictions ignores req_only == false
            }
        } else if req_only {
            continue; // not a requirement, skip
        }
        add_field_to_topology(f.clone(), topology, &name, type_parents)?;
    }
    Ok(())
}

/// Given a set of nodes that have requirements with restrictions,
/// for each requirement, apply the requirement's restrictions to its matched node.
///
/// Restrictions can constrain a matched node's requirements or its property values,
/// which are expressed by updating the matching nodes fields to the topology.
/// So its important that the match nodes' fields have not already by added.
///
/// Adds matched target nodes' fields constrained by restrictions to the topology.
/// Iterates through matched requirements, and applies each requirement's restrictions
/// to its matched target node.
///
/// # Arguments
/// * `nodes` - Nodes that have requirements with restrictions.
/// * `topology` - The topology so far.
/// * `start` - The starting requirement match index corresponding to the given nodes.
/// * `type_parents` - HashMap of type ancestors
///
/// # Returns
/// A tuple containing the updated `topology` and the new index after the last requirement_match.
fn apply_restrictions_to_matched_nodes(
    nodes: &HashMap<String, Node>,
    mut topology: Topology,
    type_parents: &ToscaTypes,
    start: usize,
) -> (Topology, usize) {
    let mut index = start;
    let matches = topology.requirement_match.clone();
    let requirements = topology.requirement_indices_0_1.0.clone();

    // for each matched target node, add fields that are constrained by the restrictions
    // note: we can ignore target_capability_name because we don't support placing constraints on matching capabilities, just nodes
    for (source_node_name, req_name, target_node_name, _target_capability_name) in
        matches.iter().skip(start)
    {
        index += 1;
        let requirement_values = requirements
            .get(&(source_node_name.clone(), req_name.clone()))
            .expect("missing requirement");
        let (_, restrictions) = &requirement_values[0];
        let target_node = nodes.get(target_node_name).expect("node not found!");
        for restriction_field in restrictions {
            let req_field = target_node
                .fields
                .iter()
                .find(|f| f.name == restriction_field.name)
                .expect("missing field");
            // requirement shouldn't have been added to the topology yet
            // XXX add now with extra criteria now, add nested restrictions
            if let FieldValue::Requirement {
                terms,
                tosca_type: _,
                restrictions,
            } = &req_field.value
            {
                if let FieldValue::Requirement {
                    terms: restricted_terms,
                    tosca_type,
                    restrictions: restricted_restrictions,
                } = &restriction_field.value
                {
                    // add req_field with the extra terms and nested restrictions from restriction added
                    add_field_to_topology(
                        Field {
                            name: req_field.name.clone(),
                            value: FieldValue::Requirement {
                                terms: [terms.clone(), restricted_terms.clone()].concat(),
                                tosca_type: tosca_type.clone(), // restrict type
                                restrictions: [
                                    restrictions.clone(),
                                    restricted_restrictions.clone(),
                                ]
                                .concat(),
                            },
                        },
                        &mut topology,
                        &target_node.name,
                        type_parents,
                    )
                    .expect("bad field");
                }
            }
            // else if let () XXX other kinds of constraints, i.e. property values
        }
    }
    (topology, index)
}

/// HashMap mapping (source node name, requirement name) pairs to a list of (target node name, capability name) pairs.
pub type RequirementMatches = HashMap<(String, String), Vec<(String, String)>>;

/// Runs the ascent program to find the matches for the given topology
///
/// # Arguments
///
/// * `prog`: the `Topology` to run
/// * `timeout`: a timeout in milliseconds to abort the computation
///
/// # Returns
///
/// This function returns a `PyResult` containing a `()` if the computation succeeded and a `PyTimeoutError` if the computation timed out
fn run_program(prog: &mut Topology, timeout: u64) -> PyResult<()> {
  let run_timeout_res = prog.run_timeout(Duration::from_millis(timeout));
  if !run_timeout_res {
      return Err(pyo3::exceptions::PyTimeoutError::new_err("inference timeout"));
  }
  Ok(())
}


/// Finds missing requirements for the given topology. (Main Python entry point)
///
/// # Arguments
/// * `nodes` - The topology's nodes.
/// * `type_parents` - [ToscaTypes] HashMap of type ancestors
///
/// # Returns
/// A [RequirementMatches] HashMap of the found matches.
///
/// # Errors
///
/// This function will return an error if the topology can't converted to an Ascent program.
#[cfg_attr(feature = "python", pyfunction)]
pub fn solve(
    nodes: HashMap<String, Node>,
    type_parents: ToscaTypes,
) -> PyResult<RequirementMatches> {
    let mut prog = Topology::default();

    // Requirements can project additional terms on the matching node's requirements
    // We need to avoid the situation where a requirement finds a match before a projection is applied
    // to it since that match might not fulfill the projection's additional terms.

    // After solving round, for each new requirement_matches with projections: merge projections with requirement and add the requirement and req_term_*
    // for each req req Field, find the matching Field in the requirement and add the terms or add the Field if not found

    // So solve requirements with projections first:
    // 1. only add requirements (and their nodes) with projections (in case they match each other)
    //    solve and
    // 2. add remaining nodes but omit requirements and req_term_*
    // 3. the new requirement_matches will have projections
    // 4. repeat 3 until no new matches
    // 5. add the remaining requirement and req_term_*

    // add the constraining nodes but only with requirements with restrictions (in case they match each other)
    for node in nodes.values() {
        if node.has_restrictions {
            add_node_to_topology(node, &mut prog, &type_parents, false, true)?;
        }
    }
    let timeout = nodes.len() as u64 * 100;
    run_program(&mut prog, timeout)?;
    // update matched requirements
    let mut index = 0;
    (prog, index) = apply_restrictions_to_matched_nodes(&nodes, prog, &type_parents, index);

    // add remaining nodes but omit all requirements and req_term_*
    for node in nodes.values() {
        if !node.has_restrictions {
            add_node_to_topology(node, &mut prog, &type_parents, false, false)?;
        }
    }

    // keep searching for matches for restricted requirements
    loop {
        run_program(&mut prog, timeout)?;
        let start = index;
        (prog, index) = apply_restrictions_to_matched_nodes(&nodes, prog, &type_parents, index);
        if index == start {
            break;
        }
    }

    // no more restricted matches,
    // add the remaining requirement and req_term_* and finish the search
    for node in nodes.values() {
        add_node_to_topology(node, &mut prog, &type_parents, true, false)?;
    }
    run_program(&mut prog, timeout)?;

    // return requirement_match
    let mut requirements = RequirementMatches::new();
    for (source_node_name, req_name, target_node_name, target_capability_name) in
        prog.requirement_match.clone()
    {
        if let Some(x) = requirements.get_mut(&(source_node_name.clone(), req_name.clone())) {
            x.push((target_node_name, target_capability_name));
        } else {
            requirements.insert(
                (source_node_name, req_name),
                vec![(target_node_name, target_capability_name)],
            );
        }
    }
    // XXX: return computed capabilities and property values
    Ok(requirements)
}

/// A Python module implemented in Rust.
#[cfg(feature = "python")]
#[pymodule]
fn tosca_solver(m: &Bound<'_, PyModule>) -> PyResult<()> {
    pyo3_log::init();
    m.add_function(wrap_pyfunction!(solve, m)?)?;
    m.add_class::<CriteriaTerm>()?;
    m.add_class::<SimpleValue>()?;
    m.add_class::<ToscaValue>()?;
    m.add_class::<Constraint>()?;
    m.add_class::<Field>()?;
    m.add_class::<FieldValue>()?;
    m.add_class::<QueryType>()?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_node(name: &str, connects_to: Option<&str>) -> (String, Node) {
        (
            name.into(),
            Node {
                name: name.into(),
                tosca_type: "Service".into(),
                has_restrictions: false,
                fields: vec![
                    // Field { name: "feature",
                    //         value: FieldValue::Capability {
                    //           tosca_type: "tosca.capabilities.Node",
                    //           properties: vec![] }
                    //         },
                    // Field { name: "url_scheme",
                    //         value: Property {
                    //           value: ToscaValue { typename: None,
                    //                 v: string { v: "https" } } } },
                    Field {
                        name: "parent".into(),
                        value: FieldValue::Requirement {
                            terms: vec![
                                CriteriaTerm::NodeType {
                                    n: "Service".into(),
                                },
                                CriteriaTerm::NodeMatch {
                                    query: vec![(QueryType::Sources, "connects_to".into())],
                                },
                            ],
                            tosca_type: Some("tosca.relationships.Root".into()),
                            restrictions: vec![],
                        },
                    },
                    Field {
                        name: "connects_to".into(),
                        value: FieldValue::Requirement {
                            terms: {
                                if let Some(connects_to_node) = connects_to {
                                    vec![CriteriaTerm::NodeName {
                                        n: connects_to_node.into(),
                                    }]
                                } else {
                                    vec![]
                                }
                            },
                            tosca_type: Some("unfurl.relationships.Configures".into()),
                            restrictions: vec![],
                        },
                    },
                ],
            },
        )
    }

    #[test]
    fn test_querytype_source() {
        let mut nodes = HashMap::<String, Node>::default();
        nodes.extend([
            make_node("test.connection", None),
            make_node("test.service", Some("test.connection")),
        ]);

        let result = solve(nodes, ToscaTypes::new()).expect("solved");
        let expected: RequirementMatches = [
            // this requirement match was hardcoded:
            (
                ("test.service".into(), "connects_to".into()),
                vec![("test.connection".into(), "feature".into())],
            ),
            // make_node sets parent to look for the service that connect to it:
            (
                ("test.connection".into(), "parent".into()),
                vec![("test.service".into(), "feature".into())],
            ),
        ]
        .iter()
        .cloned()
        .collect();
        assert_eq!(result, expected);
    }
    #[test]
    fn test_captypes() {
        let mut nodes = HashMap::<String, Node>::default();
        nodes.extend([
            (
                sym("1"),
                Node {
                    name: "1".into(),
                    tosca_type: "Service".into(),
                    has_restrictions: false,
                    fields: vec![Field {
                        name: "cap".into(),
                        value: FieldValue::Capability {
                            tosca_type: "captype1".into(),
                            properties: vec![],
                        },
                    }],
                },
            ),
            (
                sym("2"),
                Node {
                    name: "2".into(),
                    tosca_type: "Service".into(),
                    has_restrictions: false,
                    fields: vec![Field {
                        name: "req".into(),
                        value: FieldValue::Requirement {
                            terms: vec![CriteriaTerm::CapabilityTypeGroup {
                                names: vec![sym("captype1"), sym("captype2")],
                            }],
                            tosca_type: None,
                            restrictions: vec![],
                        },
                    }],
                },
            ),
        ]);

        let result = solve(nodes, ToscaTypes::new()).expect("solved");
        let expected: RequirementMatches =
            [(("2".into(), "req".into()), vec![("1".into(), "cap".into())])]
                .iter()
                .cloned()
                .collect();
        assert_eq!(result, expected);
    }
}