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.samples.actions;
36  
37  import java.util.Calendar;
38  import java.util.Map;
39  
40  import org.apache.log4j.Logger;
41  import org.apache.xmlbeans.XmlDateTime;
42  import org.apache.xmlbeans.XmlDouble;
43  import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionDocument;
44  import org.ggf.schemas.jsdl.x2005.x11.jsdl.RangeValueType;
45  import org.ggf.schemas.jsdl.x2005.x11.jsdl.ResourcesType;
46  import org.ogf.graap.wsag.api.exceptions.NegotiationException;
47  import org.ogf.graap.wsag.server.actions.AbstractNegotiationAction;
48  import org.ogf.graap.wsag.server.actions.ActionInitializationException;
49  import org.ogf.graap.wsag4j.types.configuration.ImplementationConfigurationType;
50  import org.ogf.graap.wsag4j.types.scheduling.TimeConstraintDocument;
51  import org.ogf.graap.wsag4j.types.scheduling.TimeConstraintType;
52  import org.ogf.schemas.graap.wsAgreement.OfferItemType.ItemConstraint;
53  import org.ogf.schemas.graap.wsAgreement.ServiceDescriptionTermType;
54  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationConstraintSectionType;
55  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferItemType;
56  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
57  
58  /**
59   * SampleNegotiateAction
60   * 
61   * @author hrasheed
62   * 
63   */
64  public class SampleNegotiateAction extends AbstractNegotiationAction
65  {
66  
67      /**
68       * 
69       */
70      private static final String WSAG_NEGOTIATION_INVOCATIONS = "org.wsag.negotiation.invocations";
71  
72      /**
73       * 
74       */
75      private static final int TC2_DURATION = 15;
76  
77      /**
78       * 
79       */
80      private static final int ENDTIME2_OFFSET = 30;
81  
82      /**
83       * 
84       */
85      private static final int TC1_DURATION = 20;
86  
87      /**
88       * 
89       */
90      private static final int ENDTIME1_OFFSET = 20;
91  
92      private static final Logger LOG = Logger.getLogger( SampleNegotiateAction.class );
93  
94      private static final String RESOURCE_SDT_NAME = "RESOURCE_SDT";
95  
96      private static final String TIME_CONSTRAINT_SDT_NAME = "TIME_CONSTRAINT_SDT";
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public NegotiationOfferType[]
103         negotiate( NegotiationOfferType offer, Map<String, Object> invocationContext )
104             throws NegotiationException
105     {
106         Map<String, Object> context = getNegotiationContext( invocationContext );
107 
108         if ( !context.containsKey( WSAG_NEGOTIATION_INVOCATIONS ) )
109         {
110             context.put( WSAG_NEGOTIATION_INVOCATIONS, Integer.valueOf( 1 ) );
111         }
112 
113         int negotiationIteration = ( (Integer) context.get( WSAG_NEGOTIATION_INVOCATIONS ) ).intValue();
114 
115         try
116         {
117             String offerID = offer.getOfferId();
118 
119             /******************************************************************
120              * Second Iteration of the Negotiation
121              ******************************************************************/
122 
123             if ( negotiationIteration == 2 )
124             {
125                 //
126                 // Final CounterOffer: 05 resources for 15 minutes duration with a time fame as
127                 // startTime = current + 5, endTime = startTime + 15
128                 //
129                 SampleNegotiationOffer counterOffer =
130                     new SampleNegotiationOffer( (NegotiationOfferType) offer.copy() );
131 
132                 counterOffer.setOfferId( "counterOffer_1_" + offerID );
133                 counterOffer.getNegotiationOfferContext().setCounterOfferTo( offerID );
134 
135                 ResourcesType resources = counterOffer.getResourceDefinition();
136 
137                 RangeValueType totalCountRange = RangeValueType.Factory.newInstance();
138                 totalCountRange.addNewExact().setDoubleValue( 5 );
139                 resources.setTotalResourceCount( totalCountRange );
140 
141                 TimeConstraintType timeConstraint = counterOffer.getTimeConstraint();
142                 Calendar startTime = Calendar.getInstance();
143                 startTime.set( Calendar.SECOND, 0 );
144                 startTime.set( Calendar.MILLISECOND, 0 );
145                 startTime.add( Calendar.MINUTE, 5 );
146                 Calendar endTime = (Calendar) startTime.clone();
147                 endTime.add( Calendar.MINUTE, 15 );
148                 timeConstraint.setStartTime( startTime );
149                 timeConstraint.setEndTime( endTime );
150                 timeConstraint.setDuration( 15 );
151 
152                 NegotiationConstraintSectionType constraints =
153                     addCounterOfferConstraints( 1, 5, startTime, endTime );
154                 counterOffer.setNegotiationConstraints( constraints );
155 
156                 setResourcesSDT( counterOffer, resources );
157                 setTimeConstraintSDT( counterOffer, timeConstraint );
158 
159                 return new NegotiationOfferType[] { counterOffer.getXMLObject() };
160             }
161 
162             /******************************************************************
163              * First Iteration of the Negotiation
164              ******************************************************************/
165 
166             //
167             // CounterOffer_1: 05 resources for 20 minutes duration with a time fame as
168             // startTime = current + 5, endTime = startTime + 20
169             //
170             SampleNegotiationOffer counterOffer1 =
171                 new SampleNegotiationOffer( (NegotiationOfferType) offer.copy() );
172 
173             counterOffer1.setOfferId( "counterOffer_1_" + offerID );
174             counterOffer1.getNegotiationOfferContext().setCounterOfferTo( offerID );
175 
176             ResourcesType resources1 = counterOffer1.getResourceDefinition();
177 
178             RangeValueType totalCountRange1 = RangeValueType.Factory.newInstance();
179             totalCountRange1.addNewExact().setDoubleValue( 5 );
180             resources1.setTotalResourceCount( totalCountRange1 );
181 
182             TimeConstraintType timeConstraint1 = counterOffer1.getTimeConstraint();
183             Calendar startTime1 = Calendar.getInstance();
184             startTime1.set( Calendar.SECOND, 0 );
185             startTime1.set( Calendar.MILLISECOND, 0 );
186             startTime1.add( Calendar.MINUTE, 5 );
187             Calendar endTime1 = (Calendar) startTime1.clone();
188             endTime1.add( Calendar.MINUTE, ENDTIME1_OFFSET );
189             timeConstraint1.setStartTime( startTime1 );
190             timeConstraint1.setEndTime( endTime1 );
191             timeConstraint1.setDuration( TC1_DURATION );
192 
193             //
194             // negotiation constraints
195             //
196             NegotiationConstraintSectionType constraints1 =
197                 addCounterOfferConstraints( 1, 5, startTime1, endTime1 );
198             counterOffer1.setNegotiationConstraints( constraints1 );
199 
200             setResourcesSDT( counterOffer1, resources1 );
201             setTimeConstraintSDT( counterOffer1, timeConstraint1 );
202 
203             //
204             // CounterOffer_2: 05 resources for 15 minutes duration with a time fame as
205             // startTime = current + 10, endTime = startTime + 30
206             //
207             SampleNegotiationOffer counterOffer2 =
208                 new SampleNegotiationOffer( (NegotiationOfferType) offer.copy() );
209 
210             counterOffer2.setOfferId( "counterOffer_2_" + offerID );
211             counterOffer2.getNegotiationOfferContext().setCounterOfferTo( offerID );
212 
213             ResourcesType resources2 = counterOffer2.getResourceDefinition();
214 
215             RangeValueType totalCountRange2 = RangeValueType.Factory.newInstance();
216             totalCountRange2.addNewExact().setDoubleValue( 5 );
217             resources2.setTotalResourceCount( totalCountRange2 );
218 
219             TimeConstraintType timeConstraint2 = counterOffer2.getTimeConstraint();
220 
221             Calendar startTime2 = Calendar.getInstance();
222             startTime2.set( Calendar.SECOND, 0 );
223             startTime2.set( Calendar.MILLISECOND, 0 );
224 
225             startTime2.add( Calendar.MINUTE, 10 );
226             Calendar endTime2 = (Calendar) startTime2.clone();
227             endTime2.add( Calendar.MINUTE, ENDTIME2_OFFSET );
228             timeConstraint2.setStartTime( startTime2 );
229             timeConstraint2.setEndTime( endTime2 );
230             timeConstraint2.setDuration( TC2_DURATION );
231 
232             //
233             // negotiation constraints
234             //
235             NegotiationConstraintSectionType constraints2 =
236                 addCounterOfferConstraints( 1, 5, startTime2, endTime2 );
237             counterOffer2.setNegotiationConstraints( constraints2 );
238             setResourcesSDT( counterOffer2, resources2 );
239             setTimeConstraintSDT( counterOffer2, timeConstraint2 );
240 
241             negotiationIteration++;
242 
243             return new NegotiationOfferType[] { counterOffer1.getXMLObject(), counterOffer2.getXMLObject() };
244         }
245         catch ( Exception e )
246         {
247             LOG.error( e );
248             throw new NegotiationException( e );
249         }
250         finally
251         {
252             context.put( WSAG_NEGOTIATION_INVOCATIONS, Integer.valueOf( negotiationIteration ) );
253         }
254     }
255 
256     private NegotiationConstraintSectionType addCounterOfferConstraints( double minValue, double maxValue,
257                                                                          Calendar start, Calendar end )
258     {
259 
260         //
261         // negotiation constraints are defined based on the current availability of the resources within a
262         // given time
263         // frame
264         //
265 
266         final String resourcesItemName =
267             "ResourcesSDT_JobDefinition_JobDescription_Resources_TotalResourceCount_Exact";
268         final String resourcesXPath =
269             "declare namespace jsdl='http://schemas.ggf.org/jsdl/2005/11/jsdl';"
270                 + "declare namespace wsag='http://schemas.ggf.org/graap/2007/03/ws-agreement';"
271                 + "declare namespace wsag-neg='http://schemas.ogf.org/graap/2009/11/ws-agreement-negotiation';"
272                 + "$this/wsag:Terms/wsag:All/wsag:ServiceDescriptionTerm[@wsag:Name='RESOURCE_STD']"
273                 + "/jsdl:JobDefinition/jsdl:JobDescription/jsdl:Resources/jsdl:TotalResourceCount/jsdl:Exact";
274 
275         final String tcItemName = "TimeConstraintSDT_TimeConstraint";
276         final String tcXPath =
277             "declare namespace wsag-tc='http://schemas.wsag4j.org/2009/07/wsag4j-scheduling-extensions';"
278                 + "declare namespace wsag='http://schemas.ggf.org/graap/2007/03/ws-agreement';"
279                 + "$this/wsag:Terms/wsag:All/wsag:ServiceDescriptionTerm[@wsag:Name = 'TIME_CONSTRAINT_SDT']"
280                 + "/wsag4jt:TimeConstraint";
281 
282         NegotiationConstraintSectionType constraints = NegotiationConstraintSectionType.Factory.newInstance();
283 
284         // add item constraint for resources
285         NegotiationOfferItemType resourcesItem = constraints.addNewItem();
286         resourcesItem.setName( resourcesItemName );
287         resourcesItem.setLocation( resourcesXPath );
288 
289         ItemConstraint resConstraint = resourcesItem.addNewItemConstraint();
290         XmlDouble minResources = XmlDouble.Factory.newInstance();
291         minResources.setDoubleValue( minValue );
292         resConstraint.addNewMinInclusive().setValue( minResources );
293         XmlDouble maxResources = XmlDouble.Factory.newInstance();
294         maxResources.setDoubleValue( maxValue );
295         resConstraint.addNewMaxInclusive().setValue( maxResources );
296 
297         // add item constraint for time frame
298         NegotiationOfferItemType tcItem = constraints.addNewItem();
299         tcItem.setName( tcItemName );
300         tcItem.setLocation( tcXPath );
301 
302         ItemConstraint timeConstraint = tcItem.addNewItemConstraint();
303 
304         timeConstraint.addNewMinInclusive().setValue( XmlDateTime.Factory.newValue( start ) );
305         timeConstraint.addNewMaxInclusive().setValue( XmlDateTime.Factory.newValue( end ) );
306 
307         return constraints;
308     }
309 
310     private void setResourcesSDT( SampleNegotiationOffer negotiationOffer, ResourcesType jobResources )
311         throws Exception
312     {
313 
314         ServiceDescriptionTermType resourcesSDT = null;
315 
316         ServiceDescriptionTermType[] sdts =
317             negotiationOffer.getTerms().getAll().getServiceDescriptionTermArray();
318 
319         if ( sdts != null )
320         {
321             for ( int i = 0; i < sdts.length; i++ )
322             {
323                 if ( sdts[i].getName().equals( RESOURCE_SDT_NAME ) )
324                 {
325                     resourcesSDT = sdts[i];
326                     break;
327                 }
328             }
329         }
330 
331         String name = resourcesSDT.getName();
332         String serviceName = resourcesSDT.getServiceName();
333 
334         JobDefinitionDocument resourcesDoc = JobDefinitionDocument.Factory.newInstance();
335         resourcesDoc.addNewJobDefinition().addNewJobDescription().addNewResources();
336         resourcesDoc.getJobDefinition().getJobDescription().getResources().set( jobResources );
337 
338         resourcesSDT.set( resourcesDoc );
339         resourcesSDT.setName( name );
340         resourcesSDT.setServiceName( serviceName );
341     }
342 
343     private void setTimeConstraintSDT( SampleNegotiationOffer negotiationOffer,
344                                        TimeConstraintType timeConstraint )
345     {
346 
347         ServiceDescriptionTermType timeConstraintSDT = null;
348 
349         ServiceDescriptionTermType[] sdts =
350             negotiationOffer.getTerms().getAll().getServiceDescriptionTermArray();
351 
352         if ( sdts != null )
353         {
354             for ( int i = 0; i < sdts.length; i++ )
355             {
356                 if ( sdts[i].getName().equals( TIME_CONSTRAINT_SDT_NAME ) )
357                 {
358                     timeConstraintSDT = sdts[i];
359                     break;
360                 }
361             }
362         }
363 
364         String name = timeConstraintSDT.getName();
365         String serviceName = timeConstraintSDT.getServiceName();
366 
367         TimeConstraintDocument timeConstraintDoc = TimeConstraintDocument.Factory.newInstance();
368         timeConstraintDoc.addNewTimeConstraint();
369         timeConstraintDoc.getTimeConstraint().set( timeConstraint );
370 
371         timeConstraintSDT.set( timeConstraintDoc );
372         timeConstraintSDT.setName( name );
373         timeConstraintSDT.setServiceName( serviceName );
374     }
375 
376     /**
377      * {@inheritDoc}
378      */
379     @Override
380     public void initialize() throws ActionInitializationException
381     {
382         //
383         // You can access the action configuration data of the wsag4j-engine.config
384         // by calling the getConfiguration() method. You can iterate over the configuration
385         // data by using the DOM API, or you can select child data via XPath.
386         //
387         ImplementationConfigurationType config = getHandlerContext().getHandlerConfiguration();
388         config.getDomNode();
389         //
390         // put additional code here to initialize your action
391         //
392     }
393 }