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.text.MessageFormat;
38
39 import org.apache.log4j.Logger;
40 import org.apache.xmlbeans.XmlObject;
41 import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionDocument;
42 import org.ggf.schemas.jsdl.x2005.x11.jsdl.JobDefinitionType;
43 import org.ggf.schemas.jsdl.x2005.x11.jsdl.ResourcesType;
44 import org.ogf.graap.wsag.server.monitoring.IMonitoringContext;
45 import org.ogf.graap.wsag.server.monitoring.IServiceTermMonitoringHandler;
46 import org.ogf.graap.wsag4j.types.scheduling.TimeConstraintDocument;
47 import org.ogf.graap.wsag4j.types.scheduling.TimeConstraintType;
48 import org.ogf.schemas.graap.wsAgreement.AgreementType;
49 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateDefinition;
50 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
51 import org.w3c.dom.Node;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class SampleSDTMonitor
69 implements IServiceTermMonitoringHandler
70 {
71
72 private static final Logger LOG = Logger.getLogger( SampleSDTMonitor.class );
73
74
75
76
77 @Override
78 public void monitor( IMonitoringContext context ) throws Exception
79 {
80
81 try
82 {
83
84
85
86
87 ServiceTermStateType resourcesServiceTerm = context.getServiceTermStateByName( "RESOURCE_SDT" );
88 ResourcesType resources = loadResourcesDefinition( resourcesServiceTerm );
89 resources.set( getOfferResources( context ) );
90
91
92
93 resources.getCandidateHosts().setHostNameArray( 0, "reserved_target_host" );
94
95 ServiceTermStateType timeServiceTerm = context.getServiceTermStateByName( "TIME_CONSTRAINT_SDT" );
96 TimeConstraintType timeFrame = loadTimeConstraint( timeServiceTerm );
97 timeFrame.set( getOfferTimeConstraint( context ) );
98
99
100
101
102 resourcesServiceTerm.setState( ServiceTermStateDefinition.COMPLETED );
103 timeServiceTerm.setState( ServiceTermStateDefinition.COMPLETED );
104
105 }
106 catch ( Exception e )
107 {
108 String message =
109 MessageFormat.format( "Failed to update service term state(s). Reason: {0}",
110 new Object[] { e.getMessage() } );
111 LOG.error( message );
112 throw new Exception( message, e );
113 }
114 }
115
116
117
118
119 private ResourcesType loadResourcesDefinition( ServiceTermStateType state )
120 {
121
122 XmlObject[] jobDefinition =
123 state.selectChildren( JobDefinitionDocument.type.getDocumentElementName() );
124
125 if ( jobDefinition.length == 0 )
126 {
127 LOG.trace( "Initialize resources definition in service term state." );
128
129 JobDefinitionDocument jobdef = JobDefinitionDocument.Factory.newInstance();
130 jobdef.addNewJobDefinition().addNewJobDescription().addNewResources();
131
132 Node imported =
133 state.getDomNode().getOwnerDocument()
134 .importNode( jobdef.getJobDefinition().getDomNode(), true );
135 state.getDomNode().appendChild( imported );
136
137 jobDefinition = state.selectChildren( JobDefinitionDocument.type.getDocumentElementName() );
138 }
139
140 if ( jobDefinition.length > 1 )
141 {
142 String msgError =
143 "Multiple resources definitions founds in service term state. "
144 + "Keeping the first, removing the others.";
145 LOG.debug( msgError );
146
147 for ( int i = 1; i < jobDefinition.length; i++ )
148 {
149 state.getDomNode().removeChild( jobDefinition[i].getDomNode() );
150 }
151
152 jobDefinition = state.selectChildren( JobDefinitionDocument.type.getDocumentElementName() );
153 }
154
155 JobDefinitionType jobDef = (JobDefinitionType) jobDefinition[0];
156
157 return jobDef.getJobDescription().getResources();
158 }
159
160
161
162
163 private TimeConstraintType loadTimeConstraint( ServiceTermStateType state )
164 {
165
166 XmlObject[] constraints = state.selectChildren( TimeConstraintDocument.type.getDocumentElementName() );
167
168 if ( constraints.length == 0 )
169 {
170 LOG.trace( "Initialize time contstraint in service term state." );
171
172 TimeConstraintDocument constraint = TimeConstraintDocument.Factory.newInstance();
173 constraint.addNewTimeConstraint();
174
175 Node imported =
176 state.getDomNode().getOwnerDocument()
177 .importNode( constraint.getTimeConstraint().getDomNode(), true );
178 state.getDomNode().appendChild( imported );
179
180 constraints = state.selectChildren( TimeConstraintDocument.type.getDocumentElementName() );
181 }
182
183 if ( constraints.length > 1 )
184 {
185 String msgError =
186 "Multiple time contstraint states founds in service term state. "
187 + "Keeping the first, removing the others.";
188 LOG.debug( msgError );
189
190 for ( int i = 1; i < constraints.length; i++ )
191 {
192 state.getDomNode().removeChild( constraints[i].getDomNode() );
193 }
194
195 constraints = state.selectChildren( TimeConstraintDocument.type.getDocumentElementName() );
196 }
197
198 TimeConstraintType timeConstraint = (TimeConstraintType) constraints[0];
199
200 return timeConstraint;
201 }
202
203
204
205
206
207 private ResourcesType getOfferResources( IMonitoringContext context )
208 {
209 AgreementType agreementOffer =
210 (AgreementType) context.getProperties().get( SampleCreateAgreementAction.SAMPLE_OFFER );
211 return new SampleAgreementOffer( agreementOffer ).getResourceDefinition();
212 }
213
214
215
216
217
218 private TimeConstraintType getOfferTimeConstraint( IMonitoringContext context )
219 {
220 AgreementType agreementOffer =
221 (AgreementType) context.getProperties().get( SampleCreateAgreementAction.SAMPLE_OFFER );
222 return new SampleAgreementOffer( agreementOffer ).getTimeConstraint();
223 }
224
225 }