summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/tuscany-assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/ImplementationPolicyComputer.java
blob: 2bd010c4893836bd8bb6325011747c1a177c4c6b (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.assembly.builder.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ConfiguredOperation;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.OperationsConfigurator;
import org.apache.tuscany.sca.policy.Intent;
import org.apache.tuscany.sca.policy.IntentAttachPoint;
import org.apache.tuscany.sca.policy.PolicySet;
import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
import org.apache.tuscany.sca.policy.util.PolicyValidationException;

/**
 * Policy computation methods pertaining to computing Implementation Policies
 */
public class ImplementationPolicyComputer extends PolicyComputer {
    
    public ImplementationPolicyComputer() {
        super();
    }
    
    public void computeImplementationIntentsAndPolicySets(Implementation implementation, Component parent)  
                                                                throws PolicyValidationException, PolicyComputationException {
        if ( implementation instanceof PolicySetAttachPoint ) {
            PolicySetAttachPoint policiedImplementation = (PolicySetAttachPoint)implementation;
            //since for an implementation the component has its policy intents and policysets its possible
            //that there are some intents there that does not constrain the implementation.. so prune 
            List<Intent> prunedIntents = computeInheritableIntents(policiedImplementation.getType(), 
                                                                   parent.getRequiredIntents());
            parent.getRequiredIntents().clear();
            parent.getRequiredIntents().addAll(prunedIntents);
            normalizeIntents(parent);
         
            computeIntentsForOperations((OperationsConfigurator)parent,
                                        (IntentAttachPoint)implementation,
                                        parent.getRequiredIntents());
            
            
            List<PolicySet> prunedPolicySets = computeInheritablePolicySets(parent.getPolicySets(),
                                                                            parent.getApplicablePolicySets());
            parent.getPolicySets().clear();
            parent.getPolicySets().addAll(prunedPolicySets);
            normalizePolicySets(parent);
            computePolicySetsForOperations(parent.getApplicablePolicySets(), 
                                           (OperationsConfigurator)parent, 
                                           (PolicySetAttachPoint)implementation);
            
            determineApplicableImplementationPolicySets(parent);
        }
    }
    
    private void determineApplicableImplementationPolicySets(Component component) throws PolicyComputationException {
        List<Intent> intentsCopy = null;
        if ( component.getImplementation() instanceof PolicySetAttachPoint ) {
            PolicySetAttachPoint policiedImplementation = (PolicySetAttachPoint)component.getImplementation();
           
            //trim intents specified in operations.  First check for policysets specified on the operation
            //and then in the parent implementation
            if ( component instanceof OperationsConfigurator ) {
                OperationsConfigurator opConfigurator = (OperationsConfigurator)component;
                
                for ( ConfiguredOperation confOp : opConfigurator.getConfiguredOperations() ) {
                    intentsCopy = new ArrayList<Intent>(confOp.getRequiredIntents());
                    trimInherentlyProvidedIntents(policiedImplementation.getType(), 
                                                  confOp.getRequiredIntents());
                    trimProvidedIntents(confOp.getRequiredIntents(), confOp.getPolicySets());
                    trimProvidedIntents(confOp.getRequiredIntents(), component.getPolicySets());
                    
                    determineApplicableDomainPolicySets(component.getApplicablePolicySets(), 
                                                        confOp,
                                                        policiedImplementation.getType());

                    if (confOp.getRequiredIntents().size() > 0) {
                        new PolicyComputationException("The following are unfulfilled intents for operations configured in "
                                + "component implementation - " + component.getName() + "\nUnfulfilled Intents = " +
                                confOp.getRequiredIntents());
                    }
                    
                    //the intents list could have been trimmed when matching for policysets
                    //since the implementation may need the original set of intents we copy that back
                    confOp.getRequiredIntents().clear();
                    confOp.getRequiredIntents().addAll(intentsCopy);
                }
            }
                
            intentsCopy = new ArrayList<Intent>(component.getRequiredIntents());
            trimInherentlyProvidedIntents(policiedImplementation.getType(), 
                                          component.getRequiredIntents());
            trimProvidedIntents(component.getRequiredIntents(), component.getPolicySets());
                
            //determine additional policysets that match remaining intents
            //if there are intents that are not provided by any policy set throw a warning
            //TODO: resolved to domain policy registry and attach suitable policy sets to the implementation
            //...for now using the SCA Definitions instead of registry
            //if ( domainPolicySets != null)  {
                determineApplicableDomainPolicySets(component.getApplicablePolicySets(), 
                                                    component,
                                                    policiedImplementation.getType());
                                                    
                if (component.getRequiredIntents().size() > 0) {
                    throw new PolicyComputationException("The following are unfulfilled intents for component implementation - " + component
                        .getName() + "\nUnfulfilled Intents = " + component.getRequiredIntents());
                }
            //}
            
            //the intents list could have been trimmed when matching for policysets
            //since the bindings may need the original set of intents we copy that back
            component.getRequiredIntents().clear();
            component.getRequiredIntents().addAll(intentsCopy);
        }
    }
}