blob: 279e179db577a79dd4fff4ffcec74c1698bb96e9 [file] [log] [blame]
Bob Badour9ee7d032021-10-25 16:51:48 -07001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package compliance
16
17import (
18 "fmt"
19)
20
21// SourceSharePrivacyConflict describes an individual conflict between a source-sharing
22// condition and a source privacy condition
23type SourceSharePrivacyConflict struct {
24 SourceNode *TargetNode
25 ShareCondition LicenseCondition
26 PrivacyCondition LicenseCondition
27}
28
29// Error returns a string describing the conflict.
30func (conflict SourceSharePrivacyConflict) Error() string {
Bob Badour103eb0f2022-01-10 13:50:57 -080031 return fmt.Sprintf("%s %s and must share from %s condition\n", conflict.SourceNode.name,
32 conflict.PrivacyCondition.Name(), conflict.ShareCondition.Name())
Bob Badour9ee7d032021-10-25 16:51:48 -070033}
34
35// IsEqualTo returns true when `conflict` and `other` describe the same conflict.
36func (conflict SourceSharePrivacyConflict) IsEqualTo(other SourceSharePrivacyConflict) bool {
37 return conflict.SourceNode.name == other.SourceNode.name &&
Bob Badour103eb0f2022-01-10 13:50:57 -080038 conflict.ShareCondition == other.ShareCondition &&
39 conflict.PrivacyCondition == other.PrivacyCondition
Bob Badour9ee7d032021-10-25 16:51:48 -070040}
41
42// ConflictingSharedPrivateSource lists all of the targets where conflicting conditions to
43// share the source and to keep the source private apply to the target.
44func ConflictingSharedPrivateSource(lg *LicenseGraph) []SourceSharePrivacyConflict {
Bob Badour9ee7d032021-10-25 16:51:48 -070045
Bob Badour103eb0f2022-01-10 13:50:57 -080046 ResolveTopDownConditions(lg)
Bob Badour9ee7d032021-10-25 16:51:48 -070047 // combined is the combination of source-sharing and source privacy.
Bob Badour103eb0f2022-01-10 13:50:57 -080048 combined := WalkActionsForCondition(lg, ImpliesShared.Union(ImpliesPrivate))
Bob Badour9ee7d032021-10-25 16:51:48 -070049
50 // size is the size of the result
51 size := 0
Bob Badour103eb0f2022-01-10 13:50:57 -080052 for _, cs := range combined {
53 size += cs.Intersection(ImpliesShared).Len() * cs.Intersection(ImpliesPrivate).Len()
Bob Badour9ee7d032021-10-25 16:51:48 -070054 }
55 if size == 0 {
Bob Badour103eb0f2022-01-10 13:50:57 -080056 return nil
Bob Badour9ee7d032021-10-25 16:51:48 -070057 }
58 result := make([]SourceSharePrivacyConflict, 0, size)
Bob Badour103eb0f2022-01-10 13:50:57 -080059 for actsOn, cs := range combined {
60 pconditions := cs.Intersection(ImpliesPrivate).AsList()
61 ssconditions := cs.Intersection(ImpliesShared).AsList()
Bob Badour9ee7d032021-10-25 16:51:48 -070062
63 // report all conflicting condition combinations
64 for _, p := range pconditions {
65 for _, ss := range ssconditions {
66 result = append(result, SourceSharePrivacyConflict{actsOn, ss, p})
67 }
68 }
69 }
70 return result
71}