diff --git a/comid/classid.go b/comid/classid.go index 1d69f605..4cf6f631 100644 --- a/comid/classid.go +++ b/comid/classid.go @@ -138,12 +138,55 @@ func (o ClassID) String() string { return o.Value.String() } +// SetImplID sets the value of the target ClassID to the supplied PSA +// Implementation ID (see Section 3.2.2 of draft-tschofenig-rats-psa-token) +func (o *ClassID) SetImplID(implID ImplID) *ClassID { + if o != nil { + o.Value = TaggedImplID(implID) + } + return o +} + +// GetImplID retrieves the value of the PSA Implementation ID +// (see Section 3.2.2 of draft-tschofenig-rats-psa-token) from ClassID +func (o ClassID) GetImplID() (ImplID, error) { + switch t := o.Value.(type) { + case *TaggedImplID: + return ImplID(*t), nil + case TaggedImplID: + return ImplID(t), nil + default: + return ImplID{}, fmt.Errorf("class-id type is: %T", t) + } +} + type IClassIDValue interface { extensions.ITypeChoiceValue Bytes() []byte } +// SetUUID sets the value of the target ClassID to the supplied UUID +func (o *ClassID) SetUUID(uuid UUID) *ClassID { + if o != nil { + o.Value = TaggedUUID(uuid) + } + return o +} + +// SetOID sets the value of the targed ClassID to the supplied OID. +// The OID is a string in dotted-decimal notation +func (o *ClassID) SetOID(s string) *ClassID { + if o != nil { + var berOID OID + if berOID.FromString(s) != nil { + return nil + } + o.Value = TaggedOID(berOID) + } + return o +} + const ImplIDType = "psa.impl-id" type ImplID [32]byte diff --git a/comid/instance.go b/comid/instance.go index 8d51bd9c..1c1383fd 100644 --- a/comid/instance.go +++ b/comid/instance.go @@ -4,8 +4,10 @@ import ( "encoding/json" "fmt" + "github.com/google/uuid" "github.com/veraison/corim/encoding" "github.com/veraison/corim/extensions" + "github.com/veraison/eat" ) // Instance stores an instance identity. The supported formats are UUID, UEID and variable-length opaque bytes. @@ -121,6 +123,45 @@ func (o Instance) MarshalJSON() ([]byte, error) { return json.Marshal(value) } +// SetUEID sets the identity of the target instance to the supplied UEID +func (o *Instance) SetUEID(val eat.UEID) *Instance { + if o != nil { + if val.Validate() != nil { + return nil + } + o.Value = TaggedUEID(val) + } + return o +} + +// SetUUID sets the identity of the target instance to the supplied UUID +func (o *Instance) SetUUID(val uuid.UUID) *Instance { + if o != nil { + o.Value = TaggedUUID(val) + } + return o +} + +func (o Instance) GetUEID() (eat.UEID, error) { + switch t := o.Value.(type) { + case TaggedUEID: + return eat.UEID(t), nil + default: + return eat.UEID{}, fmt.Errorf("instance-id type is: %T", t) + } +} + +func (o Instance) GetUUID() (UUID, error) { + switch t := o.Value.(type) { + case *TaggedUUID: + return UUID(*t), nil + case TaggedUUID: + return UUID(t), nil + default: + return UUID{}, fmt.Errorf("instance-id type is: %T", t) + } +} + // IInstanceValue is the interface implemented by all Instance value // implementations. type IInstanceValue interface { diff --git a/comid/instance_test.go b/comid/instance_test.go index 045b749f..461de5b3 100644 --- a/comid/instance_test.go +++ b/comid/instance_test.go @@ -11,9 +11,17 @@ import ( func TestInstance_GetUUID_OK(t *testing.T) { inst := MustNewUUIDInstance(TestUUID) - u, ok := inst.Value.(*TaggedUUID) - assert.True(t, ok) - assert.EqualValues(t, TestUUID, *u) + require.NotNil(t, inst) + u, err := inst.GetUUID() + assert.Nil(t, err) + assert.Equal(t, u, TestUUID) +} + +func TestInstance_GetUUID_NOK(t *testing.T) { + inst := &Instance{} + expectedErr := "instance-id type is: " + _, err := inst.GetUUID() + assert.EqualError(t, err, expectedErr) } type testInstance string diff --git a/comid/measurement.go b/comid/measurement.go index 8fe7bec8..805b3c7b 100644 --- a/comid/measurement.go +++ b/comid/measurement.go @@ -75,6 +75,8 @@ func (o Mkey) GetPSARefValID() (PSARefValID, error) { return PSARefValID{}, errors.New("MKey is not set") } switch t := o.Value.(type) { + case *TaggedPSARefValID: + return PSARefValID(*t), nil case TaggedPSARefValID: return PSARefValID(t), nil default: @@ -87,7 +89,10 @@ func (o Mkey) GetCCAPlatformConfigID() (CCAPlatformConfigID, error) { if !o.IsSet() { return "", errors.New("MKey is not set") } + switch t := o.Value.(type) { + case *TaggedCCAPlatformConfigID: + return CCAPlatformConfigID(*t), nil case TaggedCCAPlatformConfigID: return CCAPlatformConfigID(t), nil default: