View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Sample Negotiator to demonstrate the Agreement Negotiation.
74   * 
75   * @author hrasheed
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      * @param name
113      *            the test case name
114      */
115     public AbstractSampleNegotiatorTest( String name )
116     {
117         super( name );
118     }
119 
120     /**
121      * Tests the negotiation of offers and counter offers.
122      * 
123      * @throws Exception
124      *             indicates an error in the test case
125      */
126     public void testSampleNegotiator() throws Exception
127     {
128 
129         NegotiationClient negotiation = null;
130 
131         //
132         // initiate the negotiation instance
133         //
134         negotiation = initiateNegotiation( negotiation );
135 
136         try
137         {
138             //
139             // retrieve the negotiable template
140             //
141             SampleAgreementTemplate negotiationTemplate = getNegotiableTemplate( negotiation );
142 
143             /******************************************************************
144              * First round of the Negotiation
145              ******************************************************************/
146             //
147             // This template includes the service description term 'RESOURCE_SDT' with a JSDL document
148             // describing the available compute resources.
149             //
150             // NegotiationOffer_1: 05 resources for 15 minutes duration with a time frame as
151             // startTime = current, endTime = startTime + 60
152             //
153             //
154             NegotiationOfferType counterOffer1 = negotiateRound1( negotiation, negotiationTemplate );
155 
156             /******************************************************************
157              * Second round of the Negotiation
158              ******************************************************************/
159             //
160             // we receive 2 counter offers with different resource availability as
161             //
162             // CounterOffer_1: 05 resources for 20 minutes duration with a time fame as
163             // startTime = current + 5, endTime = startTime + 20
164             //
165             // CounterOffer_2: 05 resources for 15 minutes duration with a time fame as
166             // startTime = current + 10, endTime = startTime + 30
167             //
168             // Say that both counter offers are not sufficient with respect to time,
169             // we create another negotiation offer with
170             //
171             // NegotiationOffer_2: 05 resources for 15 minutes duration with a time frame as
172             // startTime = current + 10, endTime = startTime + 20
173             //
174             NegotiationOfferType selectedCounterOffer = negotiateRound2( negotiation, counterOffer1 );
175 
176             //
177             // create agreement if negotiated offer satisfy the requirements
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             // finally terminate the negotiation process
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      * @param negotiation
212      * @param counterOffer1
213      * @return
214      * @throws Exception
215      * @throws NegotiationException
216      * @throws ResourceUnknownException
217      * @throws ResourceUnavailableException
218      */
219     private NegotiationOfferType negotiateRound2( NegotiationClient negotiation,
220                                                   NegotiationOfferType counterOffer1 ) throws Exception
221     {
222         /******************************************************************
223          * Second round of the Negotiation
224          ******************************************************************/
225         //
226         // we receive 2 counter offers with different resource availability as
227         //
228         // CounterOffer_1: 05 resources for 20 minutes duration with a time fame as
229         // startTime = current + 5, endTime = startTime + 20
230         //
231         // CounterOffer_2: 05 resources for 15 minutes duration with a time fame as
232         // startTime = current + 10, endTime = startTime + 30
233         //
234         // Say that both counter offers are not sufficient with respect to time,
235         // we create another negotiation offer with
236         //
237         // NegotiationOffer_2: 05 resources for 15 minutes duration with a time frame as
238         // startTime = current + 10, endTime = startTime + 20
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         // check whether the negotiation (counter) offer is rejected
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      * @param negotiation
305      * @param negotiationTemplate
306      * @return
307      * @throws Exception
308      * @throws NegotiationException
309      * @throws ResourceUnknownException
310      * @throws ResourceUnavailableException
311      */
312     private NegotiationOfferType negotiateRound1( NegotiationClient negotiation,
313                                                   SampleAgreementTemplate negotiationTemplate )
314         throws Exception
315     {
316 
317         /******************************************************************
318          * First round of the Negotiation
319          ******************************************************************/
320         //
321         // This template includes the service description term 'RESOURCE_SDT' with a JSDL document
322         // describing the available compute resources.
323         //
324         // NegotiationOffer_1: 05 resources for 15 minutes duration with a time frame as
325         // startTime = current, endTime = startTime + 60
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         // The service description term 'TIME_CONSTRAINT_SDT' defines the time frame
339         // during which the resources can be available.
340         // The start and end time specified by a user is considered to be the earliest possible start time and
341         // the
342         // deadline
343         // We need an advance reservation of the resources for 15 minutes effective duration, however,
344         // within a same time frame as received from a resource provider
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         // now we create negotiation offer from a negotiable template for the reservation of
356         // required resources to fulfill the application execution requirements.
357         //
358         SampleNegotiationOffer negotiationOffer1 = negotiationTemplate.getNegotiationOffer();
359         negotiationOffer1.setOfferId( offerID );
360 
361         //
362         // creating negotiation offer context
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 ); // a fully qualified name of the template
373                                                       // (templateID-TemplateName)
374 
375         negotiationOffer1.setNegotiationOfferContext( negOfferContext );
376 
377         //
378         // one negotiation constraint is added that basically define the preferred time frame window within
379         // which
380         // the user
381         // would like to have the reservation of resources (say within first 45 minutes)
382         //
383         NegotiationConstraintSectionType constraints1 =
384             addNeogtiationOfferConstraints( timeConstraint1.getStartTime() );
385         negotiationOffer1.setNegotiationConstraints( constraints1 );
386 
387         //
388         // Service description terms 'RESOURCE_SDT' and 'TIME_CONSTRAINT_SDT' are updated in a negotiation
389         // offer.
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         // invoking negotiate method from Negotiation instance and
405         // in return counter offers are received
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         // first check whether the negotiation (counter) offer is rejected
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      * @param negotiation
440      * @return
441      * @throws ResourceUnknownException
442      * @throws ResourceUnavailableException
443      */
444     private SampleAgreementTemplate getNegotiableTemplate( NegotiationClient negotiation )
445         throws ResourceUnknownException, ResourceUnavailableException
446     {
447         //
448         // retrieve the agreement templates for which negotiation is supported,
449         // and select the one with template name "SAMPLE-TEMPLATE".
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         // The "SAMPLE-TEMPLATE" template exposes the current availability of computing resources by a
472         // resource provider.
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      * @param factory
484      * @param negotiation
485      * @return
486      */
487     private NegotiationClient initiateNegotiation( NegotiationClient negotiation )
488     {
489         try
490         {
491 
492             AgreementFactoryClient factory = getFactoryClient();
493 
494             //
495             // Now creates a negotiation context that defines the roles and obligations
496             // of the negotiating parties and specifies the type of the negotiation process.
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             // set the nature of the negotiation process (e.g. negotiation or re-negotiation).
507             //
508             NegotiationType negotiationType = negContext.addNewNegotiationType();
509             negotiationType.addNewNegotiation();
510             //
511             // creating negotiation instance based on a negotiation context from a selected agreement factory
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      * @return
539      * @throws ResourceUnknownException
540      * @throws ResourceUnavailableException
541      */
542     private AgreementFactoryClient getFactoryClient()
543         throws ResourceUnknownException, ResourceUnavailableException
544     {
545         AgreementFactoryClient factory;
546         //
547         // First all agreement factories are loaded and then an agreement factory having
548         // "SAMPLE-INSTANCE-1" resource id is selected.
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         // add one item constraint
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 }