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 package org.ogf.graap.wsag.it.samples.negotiation;
36
37 import java.util.Calendar;
38 import java.util.GregorianCalendar;
39
40 import org.apache.log4j.Logger;
41 import org.apache.xmlbeans.XmlDateTime;
42 import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionDocument;
43 import org.ggf.schemas.jsdl.x2005.x11.jsdl.RangeValueType;
44 import org.ggf.schemas.jsdl.x2005.x11.jsdl.ResourcesType;
45 import org.ogf.graap.wsag.api.AgreementOffer;
46 import org.ogf.graap.wsag.api.client.AgreementClient;
47 import org.ogf.graap.wsag.api.client.AgreementFactoryClient;
48 import org.ogf.graap.wsag.api.client.NegotiationClient;
49 import org.ogf.graap.wsag.api.exceptions.NegotiationException;
50 import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
51 import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
52 import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
53 import org.ogf.graap.wsag.api.types.AgreementOfferType;
54 import org.ogf.graap.wsag.it.AbstractIntegrationTest;
55 import org.ogf.graap.wsag.samples.actions.SampleAgreementTemplate;
56 import org.ogf.graap.wsag.samples.actions.SampleNegotiationOffer;
57 import org.ogf.graap.wsag4j.types.scheduling.TimeConstraintDocument;
58 import org.ogf.graap.wsag4j.types.scheduling.TimeConstraintType;
59 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
60 import org.ogf.schemas.graap.wsAgreement.OfferItemType.ItemConstraint;
61 import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
62 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationConstraintSectionType;
63 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextDocument;
64 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
65 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferContextType;
66 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferItemType;
67 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferStateType;
68 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
69 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationRoleType;
70 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationType;
71
72
73
74
75
76
77
78 public class AbstractSampleNegotiatorTest extends AbstractIntegrationTest
79 {
80
81
82
83
84 private static final int DURATION = 15;
85
86
87
88
89 private static final int STARTTIME_OFFSET = 10;
90
91
92
93
94 private static final int ENDTIME_OFFSET = 20;
95
96 private static final Logger LOG = Logger.getLogger( AbstractSampleNegotiatorTest.class );
97
98 private static final int EXPECTED_FACTORIES = 2;
99
100 private static final String FACTORY_RESOURCE_ID = "SAMPLE-INSTANCE-1";
101
102 private static final String TEMPLATE_NAME = "SAMPLE-TEMPLATE";
103
104 private static final String RESOURCES_SDT_NAME = "RESOURCE_SDT";
105
106 private static final String TIME_CONSTRAINT_SDT_NAME = "TIME_CONSTRAINT_SDT";
107
108 private static final int END_TIME_OFFSET = 45;
109
110
111
112
113
114
115 public AbstractSampleNegotiatorTest( String name )
116 {
117 super( name );
118 }
119
120
121
122
123
124
125
126 public void testSampleNegotiator() throws Exception
127 {
128
129 NegotiationClient negotiation = null;
130
131
132
133
134 negotiation = initiateNegotiation( negotiation );
135
136 try
137 {
138
139
140
141 SampleAgreementTemplate negotiationTemplate = getNegotiableTemplate( negotiation );
142
143
144
145
146
147
148
149
150
151
152
153
154 NegotiationOfferType counterOffer1 = negotiateRound1( negotiation, negotiationTemplate );
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 NegotiationOfferType selectedCounterOffer = negotiateRound2( negotiation, counterOffer1 );
175
176
177
178
179 AgreementOffer offer = new AgreementOfferType( selectedCounterOffer );
180 AgreementClient agreement = getFactoryClient().createAgreement( offer );
181 assertNotNull( agreement );
182 LOG.info( "negotiated agreement successfully created" );
183
184
185
186
187 LOG.info( "terminating negotiated agreement" );
188 negotiation.terminate();
189
190 }
191 catch ( NegotiationException e )
192 {
193 fail( "NegotiationException: " + e.getMessage() );
194 }
195 catch ( ResourceUnavailableException e )
196 {
197 fail( "ResourceUnavailableException: " + e.getMessage() );
198 }
199 catch ( ResourceUnknownException e )
200 {
201 fail( "ResourceUnknownException: " + e.getMessage() );
202 }
203 catch ( Exception e )
204 {
205 e.printStackTrace();
206 fail( "sample-negotiator fails. Error: " + e.getMessage() );
207 }
208 }
209
210
211
212
213
214
215
216
217
218
219 private NegotiationOfferType negotiateRound2( NegotiationClient negotiation,
220 NegotiationOfferType counterOffer1 ) throws Exception
221 {
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 SampleNegotiationOffer negotiationOffer2 = new SampleNegotiationOffer( counterOffer1 );
241
242 ResourcesType jobResources2 = negotiationOffer2.getResourceDefinition();
243
244 RangeValueType totalCountRange2 = RangeValueType.Factory.newInstance();
245 totalCountRange2.addNewExact().setDoubleValue( 5 );
246 jobResources2.setTotalResourceCount( totalCountRange2 );
247
248 TimeConstraintType timeConstraint2 = negotiationOffer2.getTimeConstraint();
249
250 Calendar startTime2 = (Calendar) timeConstraint2.getStartTime().clone();
251 startTime2.add( Calendar.MINUTE, STARTTIME_OFFSET );
252 Calendar endTime2 = (Calendar) startTime2.clone();
253 endTime2.add( Calendar.MINUTE, ENDTIME_OFFSET );
254 timeConstraint2.setStartTime( startTime2 );
255 timeConstraint2.setEndTime( endTime2 );
256 timeConstraint2.setDuration( DURATION );
257
258 setResourcesSDT( negotiationOffer2, jobResources2 );
259 setTimeConstraintSDT( negotiationOffer2, timeConstraint2 );
260
261 NegotiationOfferType[] negotiationOfferTypes2 = { negotiationOffer2.getXMLObject() };
262 if ( LOG.isTraceEnabled() )
263 {
264 for ( int i = 0; i < negotiationOfferTypes2.length; i++ )
265 {
266 LOG.trace( "Iteration-2: negotiation offers: " + negotiationOfferTypes2[i].toString() );
267 }
268 }
269
270 NegotiationOfferType[] counterOffers2 = negotiation.negotiate( negotiationOfferTypes2 );
271 assertNotNull( counterOffers2 );
272 assertEquals( 1, counterOffers2.length );
273
274 LOG.info( "Iteration-2: Number of counter offers received: " + counterOffers2.length );
275
276 if ( LOG.isTraceEnabled() )
277 {
278 for ( int i = 0; i < counterOffers2.length; i++ )
279 {
280 LOG.trace( "Iteration-2: counter_offer: " + counterOffers2[i].xmlText() );
281 }
282 }
283
284 LOG.info( "second iteration of negotiation is successful" );
285
286 NegotiationOfferType selectedCounterOffer = counterOffers2[0];
287
288
289
290
291 if ( selectedCounterOffer.getNegotiationOfferContext().getState().isSetRejected() )
292 {
293 String message =
294 "Iteration-2: counter offer [" + selectedCounterOffer.getOfferId()
295 + "] is rejected. Reason: "
296 + selectedCounterOffer.getNegotiationOfferContext().getState().xmlText();
297 LOG.error( message );
298 fail( "Iteration-2: NegotiationException: " + message );
299 }
300 return selectedCounterOffer;
301 }
302
303
304
305
306
307
308
309
310
311
312 private NegotiationOfferType negotiateRound1( NegotiationClient negotiation,
313 SampleAgreementTemplate negotiationTemplate )
314 throws Exception
315 {
316
317
318
319
320
321
322
323
324
325
326
327
328 String offerID =
329 negotiationTemplate.getContext().getTemplateId() + "-" + negotiationTemplate.getName();
330
331 ResourcesType jobResources1 = negotiationTemplate.getResourceDefinition();
332
333 RangeValueType totalCountRange1 = RangeValueType.Factory.newInstance();
334 totalCountRange1.addNewExact().setDoubleValue( 5 );
335 jobResources1.setTotalResourceCount( totalCountRange1 );
336
337
338
339
340
341
342
343
344
345
346 TimeConstraintType timeConstraint1 = negotiationTemplate.getTimeConstraint();
347
348 Calendar startTime1 = (Calendar) timeConstraint1.getStartTime().clone();
349 Calendar endTime1 = (Calendar) timeConstraint1.getEndTime().clone();
350 timeConstraint1.setStartTime( startTime1 );
351 timeConstraint1.setEndTime( endTime1 );
352 timeConstraint1.setDuration( 15 );
353
354
355
356
357
358 SampleNegotiationOffer negotiationOffer1 = negotiationTemplate.getNegotiationOffer();
359 negotiationOffer1.setOfferId( offerID );
360
361
362
363
364 NegotiationOfferContextType negOfferContext = NegotiationOfferContextType.Factory.newInstance();
365 negOfferContext.setCreator( NegotiationRoleType.NEGOTIATION_INITIATOR );
366 GregorianCalendar expireDate = new GregorianCalendar();
367 expireDate.add( Calendar.MINUTE, 5 );
368 negOfferContext.setExpirationTime( expireDate );
369 NegotiationOfferStateType negOfferState = NegotiationOfferStateType.Factory.newInstance();
370 negOfferState.addNewAdvisory();
371 negOfferContext.setState( negOfferState );
372 negOfferContext.setCounterOfferTo( offerID );
373
374
375 negotiationOffer1.setNegotiationOfferContext( negOfferContext );
376
377
378
379
380
381
382
383 NegotiationConstraintSectionType constraints1 =
384 addNeogtiationOfferConstraints( timeConstraint1.getStartTime() );
385 negotiationOffer1.setNegotiationConstraints( constraints1 );
386
387
388
389
390
391 setResourcesSDT( negotiationOffer1, jobResources1 );
392 setTimeConstraintSDT( negotiationOffer1, timeConstraint1 );
393
394 NegotiationOfferType[] negotiationOfferTypes1 = { negotiationOffer1.getXMLObject() };
395 if ( LOG.isTraceEnabled() )
396 {
397 for ( int i = 0; i < negotiationOfferTypes1.length; i++ )
398 {
399 LOG.trace( "Iteration-1: negotiation offers: " + negotiationOfferTypes1[i].toString() );
400 }
401 }
402
403
404
405
406
407 NegotiationOfferType[] counterOffers1 = negotiation.negotiate( negotiationOfferTypes1 );
408 assertNotNull( counterOffers1 );
409 assertEquals( 2, counterOffers1.length );
410
411 LOG.info( "Iteration-1: Number of counter offers received: " + counterOffers1.length );
412
413 if ( LOG.isTraceEnabled() )
414 {
415 for ( int i = 0; i < counterOffers1.length; i++ )
416 {
417 LOG.trace( "Iteration-1: counter_offer: " + counterOffers1[i].xmlText() );
418 }
419 }
420
421
422
423
424 NegotiationOfferType counterOffer1 = counterOffers1[0];
425 if ( counterOffer1.getNegotiationOfferContext().getState().isSetRejected() )
426 {
427 String message =
428 "Iteration-1: counter offer [" + counterOffer1.getOfferId() + "] is rejected. Reason: "
429 + counterOffer1.getNegotiationOfferContext().getState().xmlText();
430 LOG.error( message );
431 fail( "Iteration-1: NegotiationException: " + message );
432 }
433
434 LOG.info( "first iteration of negotiation is successful" );
435 return counterOffer1;
436 }
437
438
439
440
441
442
443
444 private SampleAgreementTemplate getNegotiableTemplate( NegotiationClient negotiation )
445 throws ResourceUnknownException, ResourceUnavailableException
446 {
447
448
449
450
451 LOG.info( "getting negotiable templates" );
452
453 AgreementTemplateType[] negotiableTemplates = negotiation.getNegotiableTemplates();
454 assertNotNull( negotiableTemplates );
455
456 AgreementTemplateType template = null;
457 for ( int i = 0; i < negotiableTemplates.length; i++ )
458 {
459 AgreementTemplateType agreementTemplate = negotiableTemplates[i];
460 LOG.debug( "retrieved template: " + agreementTemplate.getName() + ":"
461 + agreementTemplate.getTemplateId() );
462 LOG.trace( agreementTemplate.toString() );
463 if ( agreementTemplate.getName().equals( TEMPLATE_NAME ) )
464 {
465 template = agreementTemplate;
466 }
467 }
468 assertEquals( TEMPLATE_NAME, template.getName() );
469
470
471
472
473
474 SampleAgreementTemplate negotiationTemplate = new SampleAgreementTemplate( template );
475 if ( LOG.isTraceEnabled() )
476 {
477 LOG.trace( "negotiation-template: " + negotiationTemplate.getXMLObject().xmlText() );
478 }
479 return negotiationTemplate;
480 }
481
482
483
484
485
486
487 private NegotiationClient initiateNegotiation( NegotiationClient negotiation )
488 {
489 try
490 {
491
492 AgreementFactoryClient factory = getFactoryClient();
493
494
495
496
497
498 NegotiationContextDocument negContextDoc = NegotiationContextDocument.Factory.newInstance();
499 NegotiationContextType negContext = negContextDoc.addNewNegotiationContext();
500 negContext.setAgreementFactoryEPR( factory.getEndpoint() );
501 negContext.setAgreementResponder( NegotiationRoleType.NEGOTIATION_RESPONDER );
502 GregorianCalendar expireDate = new GregorianCalendar();
503 expireDate.add( Calendar.HOUR, 1 );
504 negContext.setExpirationTime( expireDate );
505
506
507
508 NegotiationType negotiationType = negContext.addNewNegotiationType();
509 negotiationType.addNewNegotiation();
510
511
512
513 negotiation = factory.initiateNegotiation( negContext );
514 assertNotNull( "the created negotiatin client is not null", negotiation );
515
516 LOG.info( "negotiation instance is created successfully" );
517 }
518 catch ( NegotiationFactoryException e )
519 {
520 fail( "NegotiationFactoryException: " + e.getMessage() );
521 }
522 catch ( ResourceUnavailableException e )
523 {
524 fail( "ResourceUnavailableException: " + e.getMessage() );
525 }
526 catch ( ResourceUnknownException e )
527 {
528 fail( "ResourceUnknownException: " + e.getMessage() );
529 }
530 catch ( Exception e )
531 {
532 fail( "Could not create negotiation client instance. Error: " + e.getMessage() );
533 }
534 return negotiation;
535 }
536
537
538
539
540
541
542 private AgreementFactoryClient getFactoryClient()
543 throws ResourceUnknownException, ResourceUnavailableException
544 {
545 AgreementFactoryClient factory;
546
547
548
549
550 AgreementFactoryClient[] factories = getAgreementFactoryClients();
551 assertEquals( EXPECTED_FACTORIES, factories.length );
552 LOG.info( "factories: " + factories.length );
553
554 if ( factories[0].getResourceId().equals( FACTORY_RESOURCE_ID ) )
555 {
556 factory = getAgreementFactoryClients()[0];
557 }
558 else
559 {
560 factory = getAgreementFactoryClients()[1];
561 }
562 return factory;
563 }
564
565 private void setResourcesSDT( SampleNegotiationOffer negotiationOffer, ResourcesType jobResources )
566 throws Exception
567 {
568
569 ServiceDescriptionTermType resourcesSDT = null;
570
571 ServiceDescriptionTermType[] sdts =
572 negotiationOffer.getTerms().getAll().getServiceDescriptionTermArray();
573
574 if ( sdts != null )
575 {
576 for ( int i = 0; i < sdts.length; i++ )
577 {
578 if ( sdts[i].getName().equals( RESOURCES_SDT_NAME ) )
579 {
580 resourcesSDT = sdts[i];
581 break;
582 }
583 }
584 }
585
586 String name = resourcesSDT.getName();
587 String serviceName = resourcesSDT.getServiceName();
588
589 JobDefinitionDocument resourcesDoc = JobDefinitionDocument.Factory.newInstance();
590 resourcesDoc.addNewJobDefinition().addNewJobDescription().addNewResources();
591 resourcesDoc.getJobDefinition().getJobDescription().getResources().set( jobResources );
592
593 resourcesSDT.set( resourcesDoc );
594 resourcesSDT.setName( name );
595 resourcesSDT.setServiceName( serviceName );
596 }
597
598 private void setTimeConstraintSDT( SampleNegotiationOffer negotiationOffer,
599 TimeConstraintType timeConstraint )
600 {
601
602 ServiceDescriptionTermType timeConstraintSDT = null;
603
604 ServiceDescriptionTermType[] sdts =
605 negotiationOffer.getTerms().getAll().getServiceDescriptionTermArray();
606
607 if ( sdts != null )
608 {
609 for ( int i = 0; i < sdts.length; i++ )
610 {
611 if ( sdts[i].getName().equals( TIME_CONSTRAINT_SDT_NAME ) )
612 {
613 timeConstraintSDT = sdts[i];
614 break;
615 }
616 }
617 }
618
619 String name = timeConstraintSDT.getName();
620 String serviceName = timeConstraintSDT.getServiceName();
621
622 TimeConstraintDocument timeConstraintDoc = TimeConstraintDocument.Factory.newInstance();
623 timeConstraintDoc.addNewTimeConstraint();
624 timeConstraintDoc.getTimeConstraint().set( timeConstraint );
625
626 timeConstraintSDT.set( timeConstraintDoc );
627 timeConstraintSDT.setName( name );
628 timeConstraintSDT.setServiceName( serviceName );
629 }
630
631 private NegotiationConstraintSectionType addNeogtiationOfferConstraints( Calendar startTime )
632 {
633
634 final String constraintItemName = "TimeConstraintSDT_TimeConstraint_START_TIME";
635 final String constraintXpath =
636 "declare namespace wsag-tc='http://schemas.wsag4j.org/2009/07/wsag4j-scheduling-extensions';"
637 + "declare namespace wsag='http://schemas.ggf.org/graap/2007/03/ws-agreement';"
638 + "$this/wsag:Terms/wsag:All/wsag:ServiceDescriptionTerm[@wsag:Name = 'TIME_CONSTRAINT_SDT']"
639 + "/wsag4jt:TimeConstraint";
640
641 NegotiationConstraintSectionType constraints = NegotiationConstraintSectionType.Factory.newInstance();
642
643
644 NegotiationOfferItemType offerItem = constraints.addNewItem();
645 offerItem.setName( constraintItemName );
646 offerItem.setLocation( constraintXpath );
647
648 ItemConstraint constraint = offerItem.addNewItemConstraint();
649
650 constraint.addNewMinInclusive().setValue( XmlDateTime.Factory.newValue( startTime ) );
651 Calendar preferredEndTime = (Calendar) startTime.clone();
652 preferredEndTime.add( Calendar.MINUTE, END_TIME_OFFSET );
653 constraint.addNewMaxInclusive().setValue( XmlDateTime.Factory.newValue( preferredEndTime ) );
654
655 return constraints;
656 }
657 }