summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/tuscany-assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/PolicyComputer.java
blob: 0e290865bd6d3c14a9fc8660571b8364291668c5 (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
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
/*
 * 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.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.ConfiguredOperation;
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.IntentAttachPointType;
import org.apache.tuscany.sca.policy.PolicySet;
import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
import org.apache.tuscany.sca.policy.ProfileIntent;
import org.apache.tuscany.sca.policy.QualifiedIntent;
import org.apache.tuscany.sca.policy.util.PolicyValidationException;
import org.apache.tuscany.sca.policy.util.PolicyValidationUtils;

/**
 * This class contains policy computation methods common to computing implementation and binding policies
 */
public abstract class PolicyComputer {
    
    protected PolicyComputer() {
        
    }
    
    protected List<Intent> computeInheritableIntents(IntentAttachPointType attachPointType, 
                                                   List<Intent> inheritableIntents) throws PolicyValidationException {
        List<Intent> validInheritableIntents = new ArrayList<Intent>();
        
        //expand profile intents in inherited intents
        expandProfileIntents(inheritableIntents);

        //validate if inherited intent applies to the attachpoint (binding / implementation) and 
        //only add such intents to the attachpoint (binding / implementation)
        for (Intent intent : inheritableIntents) {
            if ( !intent.isUnresolved() ) { 
                for (QName constrained : intent.getConstrains()) {
                    if ( PolicyValidationUtils.isConstrained(constrained, attachPointType)) {
                        validInheritableIntents.add(intent);
                        break;
                    }
                }
            } else {
                throw new PolicyValidationException("Policy Intent '" + intent.getName() + "' is not defined in this domain");
            }
        }
        
        return validInheritableIntents;
    }
    
    protected void expandProfileIntents(List<Intent> intents) {
        List<Intent> expandedIntents = null;
        if ( intents.size() > 0 ) {
            expandedIntents = findAndExpandProfileIntents(intents);
            intents.clear();
            intents.addAll(expandedIntents);
        }
    }
    
    protected void normalizeIntents(IntentAttachPoint intentAttachPoint) {
        //expand profile intents specified in the attachpoint (binding / implementation)
        expandProfileIntents(intentAttachPoint.getRequiredIntents());

        //remove duplicates and ...
        //where qualified form of intent exists retain it and remove the qualifiable intent
        filterDuplicatesAndQualifiableIntents(intentAttachPoint);
    }
    
    protected void trimInherentlyProvidedIntents(IntentAttachPointType attachPointType, List<Intent>intents) {
        //exclude intents that are inherently supported by the 
        //attachpoint-type (binding-type  / implementation-type)
        List<Intent> requiredIntents = new ArrayList<Intent>(intents);
        for ( Intent intent : requiredIntents ) {
            if ( isProvidedInherently(attachPointType, intent) ) {
                intents.remove(intent);
            }
        }
    }
    
    
    protected void computeIntentsForOperations(IntentAttachPoint intentAttachPoint) throws PolicyValidationException {
        if ( intentAttachPoint instanceof OperationsConfigurator ) {
            computeIntentsForOperations((OperationsConfigurator)intentAttachPoint, 
                                        intentAttachPoint, 
                                        intentAttachPoint.getRequiredIntents());
        }
    }
    
    protected void computeIntentsForOperations(OperationsConfigurator opConfigurator, 
                                               IntentAttachPoint intentAttachPoint, 
                                               List<Intent> parentIntents) throws PolicyValidationException {
        IntentAttachPointType attachPointType = intentAttachPoint.getType();
        
        boolean found = false;
        for ( ConfiguredOperation confOp : opConfigurator.getConfiguredOperations() ) {
            //expand profile intents specified on operations
            expandProfileIntents(confOp.getRequiredIntents());
            
            //validateIntents(confOp, attachPointType);
            
            //add intents specified for parent intent attach point (binding / implementation)
            //wherever its not overridden in the operation
            Intent tempIntent = null;
            List<Intent> attachPointOpIntents = new ArrayList<Intent>();
            for (Intent anIntent : parentIntents) {
                found = false;
            
                tempIntent = anIntent;
                while ( tempIntent instanceof QualifiedIntent ) {
                    tempIntent = ((QualifiedIntent)tempIntent).getQualifiableIntent();
                }
                
                for ( Intent opIntent : confOp.getRequiredIntents() ) {
                    if ( opIntent.getName().getLocalPart().startsWith(tempIntent.getName().getLocalPart())) {
                        found = true;
                        break;
                    }
                }
                
                if ( !found ) {
                    attachPointOpIntents.add(anIntent);
                }
            }
            
            confOp.getRequiredIntents().addAll(attachPointOpIntents);
            
            //remove duplicates and ...
            //where qualified form of intent exists retain it and remove the qualifiable intent
            filterDuplicatesAndQualifiableIntents(confOp);
            
            //exclude intents that are inherently supported by the parent
            //attachpoint-type (binding-type  / implementation-type)
            if ( attachPointType != null ) {
                List<Intent> requiredIntents = new ArrayList<Intent>(confOp.getRequiredIntents());
                for ( Intent intent : requiredIntents ) {
                    if ( isProvidedInherently(attachPointType, intent) ) {
                        confOp.getRequiredIntents().remove(intent);
                    }
                }
            }
        }
    }
    
    protected List<PolicySet> computeInheritablePolicySets(List<PolicySet> inheritablePolicySets,
                                                           List<PolicySet> applicablePolicySets) 
                                                               throws PolicyValidationException {
        List<PolicySet> validInheritablePolicySets = new ArrayList<PolicySet>();
        for (PolicySet policySet : inheritablePolicySets) {
            if ( !policySet.isUnresolved() ) { 
                if ( applicablePolicySets.contains(policySet) ) {
                    validInheritablePolicySets.add(policySet);
                }
            } else {
                throw new PolicyValidationException("Policy Set '" + policySet.getName()
                        + "' is not defined in this domain  ");
            }
        }
        
        return validInheritablePolicySets;
    }
    
    protected void normalizePolicySets(PolicySetAttachPoint policySetAttachPoint ) {
        //get rid of duplicate entries
        HashMap<QName, PolicySet> policySetTable = new HashMap<QName, PolicySet>();
        for ( PolicySet policySet : policySetAttachPoint.getPolicySets() ) {
            policySetTable.put(policySet.getName(), policySet);
        }
        
        policySetAttachPoint.getPolicySets().clear();
        policySetAttachPoint.getPolicySets().addAll(policySetTable.values());
            
        //expand profile intents
        for ( PolicySet policySet : policySetAttachPoint.getPolicySets() ) {
            expandProfileIntents(policySet.getProvidedIntents());
        }
    }
    
    protected void computePolicySetsForOperations(List<PolicySet> applicablePolicySets,
                                                  PolicySetAttachPoint policySetAttachPoint) 
                                                                        throws PolicyValidationException {
        if ( policySetAttachPoint instanceof OperationsConfigurator ) {
            computePolicySetsForOperations(applicablePolicySets, 
                                           (OperationsConfigurator)policySetAttachPoint, 
                                           policySetAttachPoint);
        }
        
    }
    
    protected void computePolicySetsForOperations(List<PolicySet> applicablePolicySets, 
                                                  OperationsConfigurator opConfigurator,
                                                  PolicySetAttachPoint policySetAttachPoint) 
                                                                        throws PolicyValidationException {
        //String appliesTo = null;
        //String scdlFragment = "";
        HashMap<QName, PolicySet> policySetTable = new HashMap<QName, PolicySet>();
        IntentAttachPointType attachPointType = policySetAttachPoint.getType();
        
        for ( ConfiguredOperation confOp : opConfigurator.getConfiguredOperations() ) {
            //validate policysets specified for the attachPoint
            for (PolicySet policySet : confOp.getPolicySets()) {
                if ( !policySet.isUnresolved() ) {
                    //appliesTo = policySet.getAppliesTo();
        
                    //if (!PolicyValidationUtils.isPolicySetApplicable(scdlFragment, appliesTo, attachPointType)) {
                    if (!applicablePolicySets.contains(policySet)) {
                        throw new PolicyValidationException("Policy Set '" + policySet.getName() 
                                + " specified for operation " + confOp.getName()  
                            + "' does not constrain extension type  "
                            + attachPointType.getName());
        
                    }
                } else {
                    throw new PolicyValidationException("Policy Set '" + policySet.getName() 
                            + " specified for operation " + confOp.getName()  
                        + "' is not defined in this domain  ");
                }
            }
            
            //get rid of duplicate entries
            for ( PolicySet policySet : confOp.getPolicySets() ) {
                policySetTable.put(policySet.getName(), policySet);
            }
        
            confOp.getPolicySets().clear();
            confOp.getPolicySets().addAll(policySetTable.values());
            policySetTable.clear();
            
            //expand profile intents
            for ( PolicySet policySet : confOp.getPolicySets() ) {
                expandProfileIntents(policySet.getProvidedIntents());
            }
        }
    }
    
        
    protected void trimProvidedIntents(List<Intent> requiredIntents, List<PolicySet> policySets) {
        for ( PolicySet policySet : policySets ) {
            trimProvidedIntents(requiredIntents, policySet);
        }
    }
    
    protected void determineApplicableDomainPolicySets(List<PolicySet> applicablePolicySets,
                                                     PolicySetAttachPoint policySetAttachPoint,
                                                     IntentAttachPointType intentAttachPointType) {

        if (policySetAttachPoint.getRequiredIntents().size() > 0) {
            //since the set of applicable policysets for this attachpoint is known 
            //we only need to check in that list if there is a policyset that matches
            for (PolicySet policySet : applicablePolicySets) {
                int prevSize = policySetAttachPoint.getRequiredIntents().size();
                trimProvidedIntents(policySetAttachPoint.getRequiredIntents(), policySet);
                // if any intent was trimmed off, then this policyset must
                // be attached to the intent attachpoint's policyset
                if (prevSize != policySetAttachPoint.getRequiredIntents().size()) {
                    policySetAttachPoint.getPolicySets().add(policySet);
                }
            } 
        }
    }
    
    private List<Intent> findAndExpandProfileIntents(List<Intent> intents) {
        List<Intent> expandedIntents = new ArrayList<Intent>();
        for ( Intent intent : intents ) {
            if ( intent instanceof ProfileIntent ) {
                ProfileIntent profileIntent = (ProfileIntent)intent;
                List<Intent> requiredIntents = profileIntent.getRequiredIntents();
                expandedIntents.addAll(findAndExpandProfileIntents(requiredIntents));
            } else {
                expandedIntents.add(intent);
            }
        }
        return expandedIntents;
    }
    
    private boolean isProvidedInherently(IntentAttachPointType attachPointType, Intent intent) {
        return ( attachPointType != null && 
                 (( attachPointType.getAlwaysProvidedIntents() != null &&
                     attachPointType.getAlwaysProvidedIntents().contains(intent) ) || 
                  ( attachPointType.getMayProvideIntents() != null &&
                     attachPointType.getMayProvideIntents().contains(intent) )
                 ) );
     }
    
    private void trimProvidedIntents(List<Intent> requiredIntents, PolicySet policySet) {
        for ( Intent providedIntent : policySet.getProvidedIntents() ) {
            if ( requiredIntents.contains(providedIntent) ) {
                requiredIntents.remove(providedIntent);
            } 
        }
        
        for ( Intent mappedIntent : policySet.getMappedPolicies().keySet() ) {
            if ( requiredIntents.contains(mappedIntent) ) {
                requiredIntents.remove(mappedIntent);
            } 
        }
    }
    
    private void filterDuplicatesAndQualifiableIntents(IntentAttachPoint intentAttachPoint) {
        //remove duplicates
        Map<QName, Intent> intentsTable = new HashMap<QName, Intent>();
        for ( Intent intent : intentAttachPoint.getRequiredIntents() ) {
            intentsTable.put(intent.getName(), intent);
        }
        
        //where qualified form of intent exists retain it and remove the qualifiable intent
        Map<QName, Intent> intentsTableCopy = new HashMap<QName, Intent>(intentsTable);
        //if qualified form of intent exists remove the unqualified form
        for ( Intent intent : intentsTableCopy.values() ) {
            if ( intent instanceof QualifiedIntent ) {
                QualifiedIntent qualifiedIntent = (QualifiedIntent)intent;
                if ( intentsTable.get(qualifiedIntent.getQualifiableIntent().getName()) != null ) {
                    intentsTable.remove(qualifiedIntent.getQualifiableIntent().getName());
                }
            }
        }
        intentAttachPoint.getRequiredIntents().clear();
        intentAttachPoint.getRequiredIntents().addAll(intentsTable.values());
    }
    
    private void validateIntents(ConfiguredOperation confOp, IntentAttachPointType attachPointType) throws PolicyValidationException {
        boolean found = false;
        if ( attachPointType != null ) {
            //validate intents specified against the parent (binding / implementation)
            found = false;
            for (Intent intent : confOp.getRequiredIntents()) {
                if ( !intent.isUnresolved() ) {
                    for (QName constrained : intent.getConstrains()) {
                        if (PolicyValidationUtils.isConstrained(constrained, attachPointType)) {
                            found = true;
                            break;
                        }
                    }
        
                    if (!found) {
                        throw new PolicyValidationException("Policy Intent '" + intent.getName() 
                                + " specified for operation " + confOp.getName()  
                            + "' does not constrain extension type  "
                            + attachPointType.getName());
                    }
                } else {
                    throw new PolicyValidationException("Policy Intent '" + intent.getName() 
                            + " specified for operation " + confOp.getName()  
                        + "' is not defined in this domain  ");
                }
            }
        }
    }
}