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      @Override
78      public void monitor( IMonitoringContext context ) throws Exception
79      {
80  
81          try
82          {
83  
84              //
85              // retrieves the Service Term State by its name
86              //
87              ServiceTermStateType resourcesServiceTerm = context.getServiceTermStateByName( "RESOURCE_SDT" );
88              ResourcesType resources = loadResourcesDefinition( resourcesServiceTerm );
89              resources.set( getOfferResources( context ) );
90              //
91              // setting the target host name which has been reserved for a required time frame
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             // once reservation is finished, corresponding service term state(s) can be set to COMPLETED
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     // Initializes resources definition document in RESOURCE_STD service term state.
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     // Initializes time constraint document in TIME_CONSTRAINT_SDT service term state.
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     // retrieving agreement offer from a monitoring context,
205     // and reading resource definition from the agreement offer
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     // retrieving agreement offer from a monitoring context,
216     // and reading time constraint from the agreement offer
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 }