tmflib/tmf620/
product_offering.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
//!
//! Product Offering Module

use crate::common::attachment::AttachmentRefOrValue;
use crate::tmf620::bundled_product_offering::BundledProductOffering;
use crate::tmf620::category::CategoryRef;
use crate::tmf620::product_specification::{
    ProductSpecification, ProductSpecificationCharacteristicValueUse, ProductSpecificationRef,
};
use crate::tmf634::resource_candidate::ResourceCandidateRef;
use crate::tmf633::service_candidate::ServiceCandidateRef;


use crate::{
    HasAttachment,
    HasDescription,
    HasLastUpdate, 
    HasId, 
    HasName, 
    HasValidity, 
    TimePeriod, 
    DateTime,
    Uri,
    vec_insert,
    LIB_PATH,
};

use super::product_offering_price::ProductOfferingPriceRef;
use serde::{Deserialize, Serialize};

use super::{ChannelRef,MarketSegmentRef,PlaceRef,SLARef};
use crate::tmf651::agreement::AgreementRef;

use tmflib_derive::{
    HasId,
    HasDescription,
    HasAttachment,
    HasLastUpdate,
    HasName,
    HasValidity,
};

use super::MOD_PATH;

const PO_VERS_INIT: &str = "1.0";
const CLASS_PATH: &str = "productOffering";

/// Product Offering Reference
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ProductOfferingRef {
    /// Unique Id
    pub id: String,
    /// HTTP URI
    pub href: String,
    /// Name of offer
    pub name : String,
}

impl From<ProductOffering> for ProductOfferingRef {
    /// Convert from ProductOffering into ProductOfferingRef
    fn from(po : ProductOffering) -> ProductOfferingRef {
        ProductOfferingRef { 
            id: po.id.unwrap_or("MISSING".to_string()).clone(), 
            href: po.href.unwrap_or("MISSING".to_string()).clone(), 
            name: po.name.unwrap_or("MISSING".to_string()).clone() 
        }
    }
}

/// Product Offering Term
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProductOfferingTerm {}

/// Product Offering Relationship
#[derive(Clone, Debug, Deserialize, Serialize, HasValidity)]
#[serde(rename_all = "camelCase")]
pub struct ProductOfferingRelationship {
    /// Unique Id
    pub id: Option<String>,
    /// HTTP Uri
    pub href: Option<String>,
    /// Name of referenced Product Offer
    pub name: Option<String>,
    /// Type of relationship between product offerings
    /// # Example
    /// Parent/Child
    pub relationship_type: Option<String>,
    /// Role of this relationship
    /// # Example
    /// Child
    pub role: Option<String>,
    /// How long is this relationship valid for?
    pub valid_for: Option<TimePeriod>,
}

impl From<ProductOffering> for ProductOfferingRelationship {
    fn from(po : ProductOffering) -> ProductOfferingRelationship {
        ProductOfferingRelationship {
            id: po.id.clone(),
            href: po.href.clone(),
            name: po.name.clone(),
            relationship_type: None,
            role : None,
            valid_for: None,
        }
    }
}

/// Product Offering
#[derive(Clone, Default, Debug, Deserialize, HasId, HasDescription, HasAttachment, HasLastUpdate, HasName, HasValidity, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProductOffering {
    /// Unique identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// HREF for API use
    #[serde(skip_serializing_if = "Option::is_none")]
    pub href: Option<String>,
    /// Description of offering
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Does this represent a bundle?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_bundle: Option<bool>,
    /// Is this sellable?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_sellable: Option<bool>,
    /// When was this last updated?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_update: Option<DateTime>,
    /// Current status
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lifecycle_status: Option<String>,
    /// Name of this offering
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Status Reason
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_reason: Option<String>,
    /// Version of this offering
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// Validity Period
    #[serde(skip_serializing_if = "Option::is_none")]
    pub valid_for: Option<TimePeriod>,

    /// Associated agreements
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agreement: Option<Vec<AgreementRef>>,
    /// Attachments
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attachment: Option<Vec<AttachmentRefOrValue>>,
    /// Bundled Product Offerings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bundled_product_offering: Option<Vec<BundledProductOffering>>,
    /// Categories
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<Vec<CategoryRef>>,
    /// Channels
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel: Option<Vec<ChannelRef>>,
    /// Market Segments
    #[serde(skip_serializing_if = "Option::is_none")]
    pub market_segment: Option<Vec<MarketSegmentRef>>,
    /// Places
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<Vec<PlaceRef>>,
    /// Product Offering Price
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_offering_price: Option<Vec<ProductOfferingPriceRef>>,
    /// Product Offering Relationship.
    /// Links to other product offers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_offering_relationship: Option<Vec<ProductOfferingRelationship>>,
    /// Product Offering Term
    #[serde(skip_serializing_if = "Option::is_none")]
    pub product_offering_term: Option<Vec<ProductOfferingTerm>>,
    /// Product Specification Characteristic Value Use
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prod_spec_char_value_use: Option<Vec<ProductSpecificationCharacteristicValueUse>>,
    /// Product Specification
    pub product_specification: Option<ProductSpecificationRef>,
    /// Resource Canididates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_candidate: Option<ResourceCandidateRef>,
    /// Service Candidates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_candidate: Option<ServiceCandidateRef>,
    /// Service Level Agreements
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_level_agreement: Option<SLARef>,

    // META
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@baseType")]
    base_type : Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@schemaLocation")]
    schema_location: Option<Uri>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "@type")]
    r#type : Option<String>,
}

impl ProductOffering {
    /// Create a new instance of ProductOffering object
    /// # Examples
    /// ```
    /// # use tmflib::tmf620::product_offering::ProductOffering;
    /// let po = ProductOffering::new(String::from("MyOffer"));
    /// ```
    pub fn new(name: impl Into<String>) -> ProductOffering {
        let mut offer = ProductOffering::create_with_time();
        offer.name = Some(name.into());
        offer.version = Some(PO_VERS_INIT.to_string());
        offer.base_type = Some(ProductOffering::get_class());
        offer.r#type = Some(ProductOffering::get_class());
        offer
    }

    /// Set status of this ProductOffering
    pub fn status(&mut self, status : &str) {
        self.lifecycle_status = Some(status.to_owned());
    }

    /// Added category refernce to ProductOffering
    /// # Examples
    /// ```
    /// # use tmflib::tmf620::product_offering::ProductOffering;
    /// # use tmflib::tmf620::category::{Category,CategoryRef};
    /// let po = ProductOffering::new(String::from("MyOffer"));
    /// let cat= Category::new(String::from("MyCategory"));
    /// let result = po.with_category(CategoryRef::from(&cat));
    /// ```
    pub fn with_category(mut self, category: CategoryRef) -> ProductOffering {
        vec_insert(&mut self.category,category);
        // self.category.as_mut().unwrap().push(category);
        self
    }

    /// Add specification into this Product Offering
    pub fn with_specification(mut self, specification: ProductSpecification) -> ProductOffering {
        self.product_specification = Some(ProductSpecificationRef::from(specification));
        self
    }

    /// Add characteristic value uses into this Product Offering
    pub fn with_char_value_use(mut self, char_value_use : ProductSpecificationCharacteristicValueUse) -> ProductOffering {
        match self.prod_spec_char_value_use.as_mut() {
            Some(v) => v.push(char_value_use),
            None => self.prod_spec_char_value_use = Some(vec![char_value_use]),
        }
        self
    }

    /// Create a link between two ProductOfferings
    pub fn link_po(&mut self, remote_po : ProductOffering, relationship_type : &str, role : &str) {
        // Create a link from ourselves into remote_po using type and role prodived.
        let mut offer_rel = ProductOfferingRelationship::from(remote_po);
        offer_rel.relationship_type = Some(relationship_type.to_string());
        offer_rel.role = Some(role.to_string());
        match self.product_offering_relationship.as_mut() {
            Some(v) => {
                v.push(offer_rel);
            },
            None => self.product_offering_relationship = Some(vec![offer_rel]),
        };
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::tmf620::category::{Category,CategoryRef};
    use crate::{HasId,HasName};

    const PO_NAME : &str = "ProductOffering";
    const PO2_NAME: &str = "Offer Two";
    const PO_STATUS: &str = "A Status";
    const CAT_NAME : &str = "A Category";
    const SPEC_NAME: &str = "A Specification";
    const CHARVALUSE_NAME : &str = "CharValUse";


    const PRODOFFERREF_JSON : &str = "{
        \"id\" : \"PO123\",
        \"href\" : \"http://example.com/tmf620/offering/PO123\",
        \"name\" : \"ProductOffering\"
    }";

    const PRODOFFER_JSON : &str = "{
        \"id\" : \"PO123\",
        \"href\" : \"http://example.com/tmf620/offering/PO123\",
        \"name\" : \"ProductOffering\"
    }";

    const PRODOFFERTERM_JSON : &str = "{}";
    const PRODOFFERREL_JSON : &str = "{
        \"id\" : \"POR123\",
        \"name\" : \"ProductOfferRel\",
        \"relationshipType\" : \"Parent/Child\",
        \"role\" : \"child\"
    }";

    const PO_TERM_JSON : &str = "{}";

    #[test]
    fn test_po_new_name() {
        let po = ProductOffering::new(PO_NAME);

        assert_eq!(po.name, Some(String::from(PO_NAME)));
    }

    #[test]
    fn test_po_new_version() {
        let po = ProductOffering::new(PO_NAME);

        assert_eq!(po.version, Some(PO_VERS_INIT.into()));
    }

    #[test]
    fn test_poref_from_po() {
        let po = ProductOffering::new(PO_NAME);
        let po_ref = ProductOfferingRef::from(po.clone());

        assert_eq!(po.get_id(),po_ref.id);
        assert_eq!(po.get_href(),po_ref.href);
        assert_eq!(po.get_name(),po_ref.name);
    }

    #[test]
    fn test_por_from_po() {
        let po = ProductOffering::new(PO_NAME);
        let por = ProductOfferingRelationship::from(po.clone());

        assert_eq!(po.id,por.id);
        assert_eq!(po.href,por.href);
        assert_eq!(po.name,por.name);
        assert_eq!(por.relationship_type.is_none(),true);
        assert_eq!(por.role.is_none(),true);
        assert_eq!(por.valid_for.is_none(),true);
    }

    #[test]
    fn test_po_status() {
        let mut po = ProductOffering::new(PO_NAME);
        po.status(PO_STATUS);

        assert_eq!(po.lifecycle_status.unwrap(),PO_STATUS.to_string());
    }

    #[test]
    fn test_po_with_cat() {
        let cat = Category::new(CAT_NAME);
        let po = ProductOffering::new(PO_NAME)
            .with_category(CategoryRef::from(&cat));

        assert_eq!(po.category.is_some(),true);
    }

    #[test]
    fn test_po_with_spec() {
        let spec = ProductSpecification::new(SPEC_NAME);
        let po = ProductOffering::new(PO_NAME)
            .with_specification(spec);

        assert_eq!(po.product_specification.is_some(),true);
    }

    #[test]
    fn test_poref_deserialize() {
        let productofferref : ProductOfferingRef = serde_json::from_str(PRODOFFERREF_JSON).unwrap();

        assert_eq!(productofferref.id.as_str(),"PO123");
        assert_eq!(productofferref.name.as_str(),"ProductOffering");
    }

    #[test]
    fn test_po_term_deserialize() {
        let _offerterm : ProductOfferingTerm = serde_json::from_str(PRODOFFERTERM_JSON).unwrap();
    }

    #[test]
    fn test_po_relationship_deserialize() {
        let offer_rel : ProductOfferingRelationship = serde_json::from_str(PRODOFFERREL_JSON).unwrap();

        assert_eq!(offer_rel.id.is_some(),true);
        assert_eq!(offer_rel.name.is_some(),true);
        assert_eq!(offer_rel.relationship_type.is_some(),true);
        assert_eq!(offer_rel.role.is_some(),true);
    }

    #[test]
    fn test_po_deserialize() {
        let po : ProductOffering = serde_json::from_str(PRODOFFER_JSON).unwrap();

        assert_eq!(po.name.is_some(),true);
        assert_eq!(po.get_name().as_str(),PO_NAME);
    }

    #[test]
    fn test_po_hasattachment() {}

    #[test]
    fn test_po_hasvalidity() {
        let mut po = ProductOffering::new(PO_NAME);

        po.set_validity(TimePeriod::period_30days());

        assert_eq!(po.valid_for.is_some(),true);
        assert_eq!(po.get_validity().unwrap().started(),true);
        assert_eq!(po.get_validity().unwrap().finished(),false);
        assert_eq!(po.get_validity_start().is_some(),true);
        assert_eq!(po.get_validity_end().is_some(),true);
    }

    #[test]
    fn test_po_charvaluse() {
        let charvaluse = ProductSpecificationCharacteristicValueUse::new(CHARVALUSE_NAME);

        let po = ProductOffering::new(PO_NAME)
            .with_char_value_use(charvaluse);

        assert_eq!(po.prod_spec_char_value_use.is_some(),true);
        assert_eq!(po.prod_spec_char_value_use.unwrap().len(),1);
    }

    #[test]
    fn test_po_link_po() {
        let mut po1 = ProductOffering::new(PO_NAME);
        let po2 = ProductOffering::new(PO2_NAME);

        po1.link_po(po2, "Parent/Child", "Parent");

        assert_eq!(po1.product_offering_relationship.is_some(),true);
    }

    #[test]
    fn test_pot_deserialize() {
        let _pot : ProductOfferingTerm = serde_json::from_str(PO_TERM_JSON).unwrap();
    }

    #[test]
    fn test_por_hasvalidity() {}
}