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.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
60
61
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
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
109
110
111 if ( negotiationIteration == 2 )
112 {
113
114
115
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
153
154
155
156
157
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
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
195
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
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
250
251
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
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
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
366
367 @Override
368 public void initialize() throws ActionInitializationException
369 {
370
371
372
373
374
375 ImplementationConfigurationType config = getHandlerContext().getHandlerConfiguration();
376 config.getDomNode();
377
378
379
380 }
381 }