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 public void monitor( IMonitoringContext context ) throws Exception
78 {
79
80 try
81 {
82
83
84
85
86 ServiceTermStateType resourcesServiceTerm = context.getServiceTermStateByName( "RESOURCE_STD" );
87 ResourcesType resources = loadResourcesDefinition( resourcesServiceTerm );
88 resources.set( getOfferResources( context ) );
89
90
91
92 resources.getCandidateHosts().setHostNameArray( 0, "reserved_target_host" );
93
94 ServiceTermStateType timeServiceTerm = context.getServiceTermStateByName( "TIME_CONSTRAINT_SDT" );
95 TimeConstraintType timeFrame = loadTimeConstraint( timeServiceTerm );
96 timeFrame.set( getOfferTimeConstraint( context ) );
97
98
99
100
101 resourcesServiceTerm.setState( ServiceTermStateDefinition.COMPLETED );
102 timeServiceTerm.setState( ServiceTermStateDefinition.COMPLETED );
103
104 }
105 catch ( Exception e )
106 {
107 String message =
108 MessageFormat.format( "Failed to update service term state(s). Reason: {0}",
109 new Object[] { e.getMessage() } );
110 LOG.error( message );
111 throw new Exception( message, e );
112 }
113 }
114
115
116
117
118 private ResourcesType loadResourcesDefinition( ServiceTermStateType state )
119 {
120
121 XmlObject[] jobDefinition =
122 state.selectChildren( JobDefinitionDocument.type.getDocumentElementName() );
123
124 if ( jobDefinition.length == 0 )
125 {
126 LOG.trace( "Initialize resources definition in service term state." );
127
128 JobDefinitionDocument jobdef = JobDefinitionDocument.Factory.newInstance();
129 jobdef.addNewJobDefinition().addNewJobDescription().addNewResources();
130
131 Node imported =
132 state.getDomNode().getOwnerDocument()
133 .importNode( jobdef.getJobDefinition().getDomNode(), true );
134 state.getDomNode().appendChild( imported );
135
136 jobDefinition = state.selectChildren( JobDefinitionDocument.type.getDocumentElementName() );
137 }
138
139 if ( jobDefinition.length > 1 )
140 {
141 String msgError =
142 "Multiple resources definitions founds in service term state. "
143 + "Keeping the first, removing the others.";
144 LOG.debug( msgError );
145
146 for ( int i = 1; i < jobDefinition.length; i++ )
147 {
148 state.getDomNode().removeChild( jobDefinition[i].getDomNode() );
149 }
150
151 jobDefinition = state.selectChildren( JobDefinitionDocument.type.getDocumentElementName() );
152 }
153
154 JobDefinitionType jobDef = (JobDefinitionType) jobDefinition[0];
155
156 return jobDef.getJobDescription().getResources();
157 }
158
159
160
161
162 private TimeConstraintType loadTimeConstraint( ServiceTermStateType state )
163 {
164
165 XmlObject[] constraints = state.selectChildren( TimeConstraintDocument.type.getDocumentElementName() );
166
167 if ( constraints.length == 0 )
168 {
169 LOG.trace( "Initialize time contstraint in service term state." );
170
171 TimeConstraintDocument constraint = TimeConstraintDocument.Factory.newInstance();
172 constraint.addNewTimeConstraint();
173
174 Node imported =
175 state.getDomNode().getOwnerDocument()
176 .importNode( constraint.getTimeConstraint().getDomNode(), true );
177 state.getDomNode().appendChild( imported );
178
179 constraints = state.selectChildren( TimeConstraintDocument.type.getDocumentElementName() );
180 }
181
182 if ( constraints.length > 1 )
183 {
184 String msgError =
185 "Multiple time contstraint states founds in service term state. "
186 + "Keeping the first, removing the others.";
187 LOG.debug( msgError );
188
189 for ( int i = 1; i < constraints.length; i++ )
190 {
191 state.getDomNode().removeChild( constraints[i].getDomNode() );
192 }
193
194 constraints = state.selectChildren( TimeConstraintDocument.type.getDocumentElementName() );
195 }
196
197 TimeConstraintType timeConstraint = (TimeConstraintType) constraints[0];
198
199 return timeConstraint;
200 }
201
202
203
204
205
206 private ResourcesType getOfferResources( IMonitoringContext context )
207 {
208 AgreementType agreementOffer =
209 (AgreementType) context.getProperties().get( SampleCreateAgreementAction.SAMPLE_OFFER );
210 return new SampleAgreementOffer( agreementOffer ).getResourceDefinition();
211 }
212
213
214
215
216
217 private TimeConstraintType getOfferTimeConstraint( IMonitoringContext context )
218 {
219 AgreementType agreementOffer =
220 (AgreementType) context.getProperties().get( SampleCreateAgreementAction.SAMPLE_OFFER );
221 return new SampleAgreementOffer( agreementOffer ).getTimeConstraint();
222 }
223
224 }