summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java
blob: 408db23401910b08d3975249937e50c204761a4b (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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 * 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.builder.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.ComponentService;
import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.CompositeReference;
import org.apache.tuscany.sca.assembly.CompositeService;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.Reference;
import org.apache.tuscany.sca.assembly.Service;
import org.apache.tuscany.sca.assembly.builder.BuilderContext;
import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
import org.apache.tuscany.sca.assembly.builder.Messages;
import org.apache.tuscany.sca.assembly.builder.PolicyBuilder;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.definitions.Definitions;
import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.monitor.Problem;
import org.apache.tuscany.sca.monitor.Problem.Severity;
import org.apache.tuscany.sca.policy.Intent;
import org.apache.tuscany.sca.policy.IntentMap;
import org.apache.tuscany.sca.policy.PolicyExpression;
import org.apache.tuscany.sca.policy.PolicySet;
import org.apache.tuscany.sca.policy.PolicySubject;
import org.apache.tuscany.sca.policy.Qualifier;

/**
 * A composite builder that computes policy sets based on attached intents and policy sets.
 * Useful if you want to build the model without making any runtime decisions such as
 * reference/services matching
 *
 * @version $Rev$ $Date$
 */
public class CompositePolicyBuilderImpl implements CompositeBuilder {
    
    protected BuilderExtensionPoint builders;
    
    public CompositePolicyBuilderImpl(ExtensionPointRegistry registry) {
        this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class);
    }

    /**
     * Report a warning.
     *
     * @param monitor
     * @param problems
     * @param message
     * @param model
     */
    protected void warning(Monitor monitor, String message, Object model, Object... messageParameters) {
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(this.getClass().getName(),
                                      Messages.ASSEMBLY_VALIDATION,
                                      Severity.WARNING,
                                      model,
                                      message,
                                      messageParameters);
            monitor.problem(problem);
        }
    }

    /**
     * Report a error.
     *
     * @param monitor
     * @param problems
     * @param message
     * @param model
     */
    private void error(Monitor monitor, String message, Object model, Object... messageParameters) {
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(this.getClass().getName(),
                                      Messages.ASSEMBLY_VALIDATION,
                                      Severity.ERROR,
                                      model,
                                      message,
                                      messageParameters);
            monitor.problem(problem);
        }
    }    

    public String getID() {
        return "org.apache.tuscany.sca.assembly.builder.CompositePolicyBuilder";
    }

    public Composite build(Composite composite, BuilderContext context) throws CompositeBuilderException {
        computePolicies(composite, context);
        buildPolicies(composite, context);
        return composite;
    }

    /**
     * Inherit the intents and policySets from the list of models
     * @param intents
     * @param policySets
     * @param models
     */
    private void inherit(PolicySubject policySubject, Object... models) {
        for (Object model : models) {
            if (model instanceof PolicySubject) {
                PolicySubject subject = (PolicySubject)model;
                // FIXME: We should ignore the mutually exclusive intents from different levels
                policySubject.getRequiredIntents().addAll(subject.getRequiredIntents());
                policySubject.getPolicySets().addAll(subject.getPolicySets());
            }
        }
    }

    /**
     * Check if two policy subjects requires multually exclusive intents
     * @param subject1
     * @param subject2
     * @param monitor 
     * @return
     */
    private boolean checkMutualExclusion(PolicySubject subject1, PolicySubject subject2, BuilderContext context) {
        if (subject1 == subject2 || subject1 == null || subject2 == null) {
            return false;
        }
        for (Intent i1 : subject1.getRequiredIntents()) {
            for (Intent i2 : subject2.getRequiredIntents()) {
                if (i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) {
                    error(context.getMonitor(), "MutuallyExclusiveIntents", new Object[] {subject1, subject2}, i1, i2);
                    return true;
                }
            }
        }
        return false;
    }

    private boolean resolveAndCheck(PolicySubject subject, BuilderContext context) {
        if (subject == null) {
            return false;
        }
        // FIXME: [rfeng] Should we resolve the intents during the "build" phase?
        resolveAndNormalize(subject, context);
        List<Intent> intents = subject.getRequiredIntents();
        int size = intents.size();
        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                Intent i1 = intents.get(i);
                Intent i2 = intents.get(j);
                if (i1 != i2 && i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) {
                    error(context.getMonitor(), "MutuallyExclusiveIntents", subject, i1, i2);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Inherit the policySets and intents from the implementation hierarchy
     * @param subject
     * @param composite
     * @param component
     * @param service
     */
    private void inheritFromService(PolicySubject subject, Composite composite, Component component, Service service) {
        if (service == null) {
            return;
        }
        if (service instanceof ComponentService) {
            // component!=null
            if (component.getImplementation() instanceof Composite) {
                composite = (Composite)component.getImplementation();
            }
            inheritFromService(subject, composite, component, ((ComponentService)service).getService());
            // Component service also inherits the intents/policySets from composite/component
            inherit(subject, composite, component);
        } else if (service instanceof CompositeService) {
            // composite!=null, component is not used
            CompositeService compositeService = (CompositeService)service;
            // Handle the promoted component service
            inheritFromService(subject, composite, compositeService.getPromotedComponent(), compositeService
                .getPromotedService());
        }
        // For atomic service, the composite is not used
        inherit(subject, component.getImplementation(), service);
    }

    /**
     * Inherit the policySets and intents from the implementation hierarchy
     * @param subject
     * @param composite
     * @param component
     * @param reference
     */
    private void inheritFromReference(PolicySubject subject,
                                      Composite composite,
                                      Component component,
                                      Reference reference) {
        if (reference == null) {
            return;
        }
        if (reference instanceof ComponentReference) {
            // component!=null
            if (component.getImplementation() instanceof Composite) {
                composite = (Composite)component.getImplementation();
            }
            inheritFromReference(subject, composite, component, ((ComponentReference)reference).getReference());
        } else if (reference instanceof CompositeReference) {
            CompositeReference compositeReference = (CompositeReference)reference;
            for (int i = 0, n = compositeReference.getPromotedReferences().size(); i < n; i++) {
                inheritFromReference(subject,
                                     composite,
                                     compositeReference.getPromotedComponents().get(i),
                                     compositeReference.getPromotedReferences().get(i));
            }
        }
        // Inherit from the componentType/reference
        inherit(subject, component.getImplementation(), reference);
    }

    /**
     * Check if two names are equal
     * @param name1
     * @param name2
     * @return
     */
    private boolean isEqual(String name1, String name2) {
        if (name1 == name2) {
            return true;
        }
        if (name1 != null) {
            return name1.equals(name2);
        } else {
            return name2.equals(name1);
        }
    }

    private Intent resolve(Definitions definitions, Intent proxy) {
        for (Intent i : definitions.getIntents()) {
            if (i.equals(proxy)) {
                return i;
            }
            for (Intent qi : i.getQualifiedIntents()) {
                if (qi.equals(proxy)) {
                    return qi;
                }
            }
        }
        return null;
    }

    private void resolveAndNormalize(PolicySubject subject, BuilderContext context) {
        Definitions definitions = context.getDefinitions();
        Set<Intent> intents = new HashSet<Intent>();
        if (definitions != null) {
            for (Intent i : subject.getRequiredIntents()) {
                Intent resolved = resolve(definitions, i);
                if (resolved != null) {
                    intents.add(resolved);
                } else {
                    warning(context.getMonitor(), "IntentNotFound", subject, i);
                    // Intent cannot be resolved
                }
            }
        }

        // Replace profile intents with their required intents
        while (!intents.isEmpty()) {
            boolean profileIntentsFound = false;
            Set<Intent> copy = new HashSet<Intent>(intents);
            for (Intent i : copy) {
                if (!i.getRequiredIntents().isEmpty()) {
                    intents.remove(i);
                    intents.addAll(i.getRequiredIntents());
                    profileIntentsFound = true;
                }
            }
            if (!profileIntentsFound) {
                // No more profileIntents
                break;
            }
        }

        // Remove the intents whose @contraints do not include the current element
        // Replace unqualified intents if there is a qualified intent in the list
        Set<Intent> copy = new HashSet<Intent>(intents);
        for (Intent i : copy) {
            if (i.getQualifiableIntent() != null) {
                intents.remove(i.getQualifiableIntent());
            }
        }

        // Replace qualifiable intents with the default qualified intent
        copy = new HashSet<Intent>(intents);
        for (Intent i : copy) {
            if (i.getDefaultQualifiedIntent() != null) {
                intents.remove(i);
                intents.add(i.getDefaultQualifiedIntent());
            }
        }

        subject.getRequiredIntents().clear();
        subject.getRequiredIntents().addAll(intents);

        Set<PolicySet> policySets = new HashSet<PolicySet>();
        if (definitions != null) {
            for (PolicySet policySet : subject.getPolicySets()) {
                int index = definitions.getPolicySets().indexOf(policySet);
                if (index != -1) {
                    policySets.add(definitions.getPolicySets().get(index));
                } else {
                    // PolicySet cannot be resolved
                    warning(context.getMonitor(), "PolicySetNotFound", subject, policySet);
                }
            }
        }

        for (Intent intent : subject.getRequiredIntents()) {
            loop: for (PolicySet ps : definitions.getPolicySets()) {
                // FIXME: We will have to check the policy references and intentMap too
                // as well as the appliesTo
                if (ps.getProvidedIntents().contains(intent)) {
                    policySets.add(ps);
                    break;
                }
                for (IntentMap map : ps.getIntentMaps()) {
                    for (Qualifier q : map.getQualifiers()) {
                        if (intent.equals(q.getIntent())) {
                            policySets.add(ps);
                            break loop;
                        }
                    }
                }
            }
        }

        subject.getPolicySets().clear();
        subject.getPolicySets().addAll(policySets);

    }

    protected void computePolicies(Composite composite, BuilderContext context) {
        resolveAndCheck(composite, context);

        for (Service service : composite.getServices()) {
            CompositeService compositeService = (CompositeService)service;
            checkMutualExclusion(compositeService, compositeService.getPromotedService(), context);
        }

        for (Reference reference : composite.getReferences()) {
            CompositeReference compositeReference = (CompositeReference)reference;
            for (Reference promoted : compositeReference.getPromotedReferences()) {
                checkMutualExclusion(compositeReference, promoted, context);
            }
        }

        // compute policies recursively
        for (Component component : composite.getComponents()) {
            Implementation implementation = component.getImplementation();
            resolveAndCheck(component, context);

            // Check component against implementation
            checkMutualExclusion(component, component.getImplementation(), context);

            for (ComponentService componentService : component.getServices()) {
                resolveAndCheck(componentService, context);
                resolveAndCheck(componentService.getService(), context);

                // Check component/service against componentType/service 
                checkMutualExclusion(componentService, componentService.getService(), context);

                if (componentService.getInterfaceContract() != null && componentService.getService() != null) {
                    resolveAndCheck(componentService.getInterfaceContract().getInterface(), context);
                    resolveAndCheck(componentService.getService().getInterfaceContract().getInterface(), context);

                    checkMutualExclusion(componentService.getInterfaceContract().getInterface(), componentService
                        .getService().getInterfaceContract().getInterface(), context);

                    resolveAndCheck(componentService.getInterfaceContract().getCallbackInterface(), context);
                    resolveAndCheck(componentService.getService().getInterfaceContract().getCallbackInterface(),
                                    context);

                    checkMutualExclusion(componentService.getInterfaceContract().getCallbackInterface(),
                                         componentService.getService().getInterfaceContract().getCallbackInterface(),
                                         context);
                }

                for (Endpoint ep : componentService.getEndpoints()) {
                    // Inherit from the componentType.service.interface
                    if (componentService.getService() != null && componentService.getService().getInterfaceContract() != null) {
                        inherit(ep, componentService.getService().getInterfaceContract().getInterface());
                    }
                    if (componentService.getInterfaceContract() != null) {
                        // Inherit from the component.service.interface
                        inherit(ep, componentService.getInterfaceContract().getInterface());
                    }
                    // Inherit from the componentType/service
                    inheritFromService(ep, composite, component, componentService.getService());
                    // Find the corresponding binding in the componentType and inherit the intents/policySets
                    if (componentService.getService() != null) {
                        for (Binding binding : componentService.getService().getBindings()) {
                            resolveAndCheck((PolicySubject)binding, context);
                            if (isEqual(ep.getBinding().getName(), binding.getName()) && (binding instanceof PolicySubject)) {
                                checkMutualExclusion((PolicySubject)ep.getBinding(), (PolicySubject)binding, context);
                                // Inherit from componentType.service.binding
                                inherit(ep, binding);
                                break;
                            }
                        }
                    }
                    // Inherit from composite/component/service
                    inheritFromService(ep, composite, ep.getComponent(), ep.getService());
                    // Inherit from binding
                    inherit(ep, ep.getBinding());

                    // Replace profile intents with their required intents
                    // Remove the intents whose @contraints do not include the current element
                    // Replace unqualified intents if there is a qualified intent in the list
                    // Replace qualifiable intents with the default qualied intent
                    resolveAndNormalize(ep, context);
                }
            }

            for (ComponentReference componentReference : component.getReferences()) {
                resolveAndCheck(componentReference, context);
                resolveAndCheck(componentReference.getReference(), context);

                // Check component/reference against componentType/reference
                checkMutualExclusion(componentReference, componentReference.getReference(), context);

                if (componentReference.getInterfaceContract() != null && componentReference.getReference() != null) {
                    resolveAndCheck(componentReference.getInterfaceContract().getInterface(), context);
                    resolveAndCheck(componentReference.getReference().getInterfaceContract().getInterface(), context);

                    checkMutualExclusion(componentReference.getInterfaceContract().getInterface(), componentReference
                        .getReference().getInterfaceContract().getInterface(), context);

                    resolveAndCheck(componentReference.getInterfaceContract().getCallbackInterface(), context);
                    resolveAndCheck(componentReference.getReference().getInterfaceContract().getCallbackInterface(),
                                    context);

                    checkMutualExclusion(componentReference.getInterfaceContract().getCallbackInterface(),
                                         componentReference.getReference().getInterfaceContract()
                                             .getCallbackInterface(),
                                         context);
                }

                for (EndpointReference epr : componentReference.getEndpointReferences()) {
                    // Inherit from the componentType.reference.interface
                    if (componentReference.getReference() != null && componentReference.getReference()
                        .getInterfaceContract() != null) {
                        inherit(epr, componentReference.getReference().getInterfaceContract().getInterface());
                    }
                    // Inherit from the component.reference.interface
                    if (componentReference.getInterfaceContract() != null) {
                        inherit(epr, componentReference.getInterfaceContract().getInterface());
                    }
                    // Inherit from the componentType/reference
                    inheritFromReference(epr, composite, component, componentReference.getReference());
                    // Find the corresponding binding in the componentType and inherit the intents/policySets
                    if (componentReference.getReference() != null) {
                        for (Binding binding : componentReference.getReference().getBindings()) {
                            if (epr.getBinding() != null && isEqual(epr.getBinding().getName(), binding.getName())
                                && (binding instanceof PolicySubject)) {
                                resolveAndCheck((PolicySubject)binding, context);
                                checkMutualExclusion((PolicySubject)epr.getBinding(), (PolicySubject)binding, context);
                                // Inherit from componentType.reference.binding
                                inherit(epr, binding);
                                break;
                            }
                        }
                    }
                    // Inherit from composite/component/reference/binding
                    inheritFromReference(epr, composite, epr.getComponent(), epr.getReference());
                    inherit(epr, epr.getBinding());

                    // Replace profile intents with their required intents
                    // Remove the intents whose @contraints do not include the current element
                    // Replace unqualified intents if there is a qualified intent in the list
                    // Replace qualifiable intents with the default qualied intent
                    resolveAndNormalize(epr, context);
                }
            }
            
            if (implementation instanceof Composite) {
                inherit(implementation, component, composite);
                computePolicies((Composite)implementation, context);
            } else {
                resolveAndCheck(implementation, context);
                if (implementation != null) {
                    inherit(implementation, component, composite);
                }
            }
        }

    }

    private Set<QName> getPolicyNames(PolicySubject subject) {
        if (subject == null) {
            return Collections.emptySet();
        }
        Set<QName> names = new HashSet<QName>();
        for (PolicySet ps : subject.getPolicySets()) {
            for (PolicyExpression exp : ps.getPolicies()) {
                names.add(exp.getName());
            }
        }
        return names;
    }

    protected void buildPolicies(Composite composite, BuilderContext context) {

        // compute policies recursively
        for (Component component : composite.getComponents()) {
            Implementation implementation = component.getImplementation();
            if (implementation instanceof Composite) {
                buildPolicies((Composite)implementation, context);
            }
        }

        for (Component component : composite.getComponents()) {

            for (ComponentService componentService : component.getServices()) {
                for (Endpoint ep : componentService.getEndpoints()) {
                    for (QName policyType : getPolicyNames(ep)) {
                        PolicyBuilder builder = builders.getPolicyBuilder(policyType);
                        if (builder != null) {
                            builder.build(ep, context);
                        }
                    }
                }
            }

            for (ComponentReference componentReference : component.getReferences()) {
                for (EndpointReference epr : componentReference.getEndpointReferences()) {
                    for (QName policyType : getPolicyNames(epr)) {
                        PolicyBuilder builder = builders.getPolicyBuilder(policyType);
                        if (builder != null) {
                            builder.build(epr, context);
                        }
                    }
                }
            }

            Implementation implementation = component.getImplementation();
            if (implementation != null) {
                for (QName policyType : getPolicyNames(implementation)) {
                    PolicyBuilder builder = builders.getPolicyBuilder(policyType);
                    if (builder != null) {
                        builder.build(component, implementation, context);
                    }
                }
            }
        }
    }
}