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.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   * SampleSDTMonitor
55   * 
56   * Once agreement instance is initialized, for each service description term and guarantee term in the
57   * agreement offer, a corresponding service term state and guarantee term state with a same name is created.
58   * Service term states are initially in the NOT_READY state, while guarantee term states are initially in the
59   * NOT_DETERMINED state.
60   * 
61   * The following Monitoring Handler update the RESOURCE_STD and TIME_CONSTRAINT_SDT service term states. JSDL
62   * document is appended to the RESOURCE_STD service term state and reserved host name is set. TimeConstraint
63   * document is appended to the TIME_CONSTRAINT_SDT service term state.
64   * 
65   * @author hrasheed
66   * 
67   */
68  public class SampleSDTMonitor
69      implements IServiceTermMonitoringHandler
70  {
71  
72      private static final Logger LOG = Logger.getLogger( SampleSDTMonitor.class );
73  
74      /**
75       * {@inheritDoc}
76       */
77      public void monitor( IMonitoringContext context ) throws Exception
78      {
79  
80          try
81          {
82  
83              //
84              // retrieves the Service Term State by its name
85              //
86              ServiceTermStateType resourcesServiceTerm = context.getServiceTermStateByName( "RESOURCE_STD" );
87              ResourcesType resources = loadResourcesDefinition( resourcesServiceTerm );
88              resources.set( getOfferResources( context ) );
89              //
90              // setting the target host name which has been reserved for a required time frame
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              // once reservation is finished, corresponding service term state(s) can be set to COMPLETED
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     // Initializes resources definition document in RESOURCE_STD service term state.
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     // Initializes time constraint document in TIME_CONSTRAINT_SDT service term state.
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     // retrieving agreement offer from a monitoring context,
204     // and reading resource definition from the agreement offer
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     // retrieving agreement offer from a monitoring context,
215     // and reading time constraint from the agreement offer
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 }