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