View Javadoc

1   package org.lsst.ccs.config;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.logging.Level;
10  import java.util.logging.Logger;
11  
12  /**
13   * Creates a dummy file-based Configuration database.
14   * all history operations are unsupported (as are SQL requests).
15   * The numbering scheme for ID are negative values and so can be detected
16   *
17   * @author bamade
18   */
19  // Date: 14/06/12
20  /*
21  implementation:
22   - directories: one for description (.groo, .groovy, .ser) other for configuration (.properties)
23   other for generated mix(.ser)
24   - two local hashMap
25  
26   names for descriptions are of the form subsystem__<tag>
27   names for configurations and prepared mixes are of the form subsystem_configuration_<tag>
28  
29   when saved in files the description/parameterDescription are stripped of their id!
30  
31  
32      */
33  
34  
35  public class FIleBasedDAO  implements DBInterface{
36      long seed = - System.currentTimeMillis();
37      long idForSubsystems = seed;
38      long idForDescriptions = seed ;
39      long idForProfiles = seed ;
40      long idForParmConfig = seed ;
41  
42      Map<String,SubsystemDescription> descriptionMap = new HashMap<String, SubsystemDescription>() ;
43  
44      Map<String,ConfigProfile> configProfileMap = new HashMap<String, ConfigProfile>() ;
45  
46      Logger CURLOG = Logger.getLogger("org.lsst.ccs.config.FIlebasedDAO") ;
47  
48      public FIleBasedDAO() {
49          // reads the file in a directory
50          //prepares the way data is saved .... should be done by a facade client
51          //  shutdown hook should calls close() if not called
52      }
53  
54      /**
55       * dummy constructor used by MemoryDAO
56       * @param notFromFile
57       */
58      public FIleBasedDAO( boolean notFromFile) {
59      }
60  
61      @Override
62      public void begin() {
63          //
64      }
65  
66      @Override
67      public void end() {
68      }
69  
70      @Override
71      public void fail() {
72          // log
73      }
74  
75      @Override
76      public void fail(Throwable th) {
77          CURLOG.log(Level.SEVERE, "file DAO" , th) ;
78          // log
79      }
80  
81      @Override
82      public void saveSubsystemDescription(ASubsystemDescription newDescription) {
83         //TODO
84          String subsystemName = newDescription.getSubsystemName() ;
85          String tag = newDescription.getTag() ;
86          newDescription.setId(idForSubsystems++);
87          for(AParameterDescription parameterDescription : newDescription.getParamDescriptions()) {
88              parameterDescription.setId(idForDescriptions++);
89          }
90          descriptionMap.put(subsystemName+"__"+tag, newDescription) ;
91      }
92  
93      @Override
94      public void saveGhostDescriptions(GhostSubsystemDescription ghosts) {
95          String subsystemName = ghosts.getSubsystemName() ;
96          String tag = ghosts.getTag() ;
97          descriptionMap.put("#"+ subsystemName+"__"+tag, ghosts) ;
98      }
99  
100     @Override
101     public ASubsystemDescription getActiveSubsystemDescription(String name, String tag) {
102         return (ASubsystemDescription) descriptionMap.get(name+"__"+tag);  //ToDO
103     }
104 
105     @Override
106     public ASubsystemDescription getActiveSubsystemDescription(long id) {
107         for(String key : descriptionMap.keySet()) {
108             if(! key.startsWith("#")) {
109                 SubsystemDescription description = descriptionMap.get(key)  ;
110                 if(description.getId() == id) {
111                     return (ASubsystemDescription) description ;
112                 }
113             }
114         }
115         return null;
116     }
117 
118     @Override
119     public GhostSubsystemDescription getGhostDescription(long id) {
120         for(String key : descriptionMap.keySet()) {
121             if(key.startsWith("#")) {
122                 SubsystemDescription description = descriptionMap.get(key)  ;
123                 if(description.getId() == id) {
124                     return (GhostSubsystemDescription) description ;
125                 }
126             }
127         }
128         return null;
129     }
130 
131 
132     @Override
133     public void deleteActiveSubsystemDescription(ASubsystemDescription oldDescription) {
134         String key = oldDescription.getSubsystemName()+"__"+ oldDescription.getTag() ;
135         descriptionMap.remove(key) ;
136     }
137 
138     @Override
139     public void saveConfigProfile(AConfigProfile newProfile) {
140         //
141         String key = "_"+newProfile.getName()+"_"+newProfile.getTag() ;
142         newProfile.setId(idForProfiles++);
143         for(AParameterConfiguration configuration : newProfile.getParameterConfigurations()) {
144             configuration.setId(idForParmConfig++);
145         }
146         configProfileMap.put(key, newProfile) ;
147     }
148 
149     @Override
150     public void savePastProfile(PastConfigProfile deprecatedProfile) {
151         String key = "#" + "_"+
152                 deprecatedProfile.getName()+"_"+deprecatedProfile.getTag() ;
153         configProfileMap.put(key, deprecatedProfile) ;
154     }
155 
156     @Override
157     public AConfigProfile getActiveConfigProfile(String name, String tag) {
158         return (AConfigProfile) configProfileMap.get("_"+name+"_"+tag);
159     }
160 
161     @Override
162     public void deleteActiveConfigProfile(AConfigProfile oldProfile) {
163         String key = "_"+
164                 oldProfile.getName()+"_"+oldProfile.getTag() ;
165         configProfileMap.remove(key) ;
166     }
167 
168     @Override
169     public void modifyParmConfig(AParameterConfiguration config) {
170         //searches for the parm in configProfile Map
171         // use fetch + id comparison
172         // then modifies it ? HOW?
173         //not implemented
174         //TODO
175         CURLOG.log(Level.WARNING, "file DAO: engineering mode not implemented" ) ;
176     }
177 
178     @Override
179     public Collection<AConfigProfile> getActiveProfilesForSubsystem(ASubsystemDescription description) {
180         ArrayList<AConfigProfile> res = new ArrayList<AConfigProfile>() ;
181         long requestedId = description.getId() ;
182         for(String key : configProfileMap.keySet()) {
183             if(! key.startsWith("#")) {
184                 ConfigProfile profile = configProfileMap.get(key)  ;
185                 if(profile.getSubsystemDescription().getId() == requestedId) {
186                     res.add((AConfigProfile)profile) ;
187                 }
188             }
189         }
190         return res;
191     }
192 
193     @Override
194     public Collection<PastConfigProfile> getProfilesForSubsystem(GhostSubsystemDescription description) {
195         ArrayList<PastConfigProfile> res = new ArrayList<PastConfigProfile>() ;
196         long requestedId = description.getId() ;
197         for(String key : configProfileMap.keySet()) {
198             if(key.startsWith("#")) {
199                 ConfigProfile profile = configProfileMap.get(key)  ;
200                 if(profile.getSubsystemDescription().getId() == requestedId) {
201                     res.add((PastConfigProfile)profile) ;
202                 }
203             }
204         }
205         return res;
206     }
207 
208     @Override
209     public void saveRun(RunHistory runHistory) {
210         CURLOG.log(Level.WARNING, "file DAO: run history not implemented" ) ;
211         //Log
212     }
213 
214     @Override
215     public void savePreparedConfiguration(PreparedConfiguration preparedConfiguration) {
216         //todo : do it!and save the corresponding file
217         CURLOG.log(Level.WARNING, "file DAO: prepared configuration not implemented" ) ;
218 
219     }
220 
221     @Override
222     public PreparedConfiguration getPreparedConfiguration(String subsystemName, String configName, String tag) {
223         //todo : do it!
224         CURLOG.log(Level.WARNING, "file DAO: prepared configuration not implemented" ) ;
225         return null;
226     }
227 
228     @Override
229     public void saveMachineConfiguration(MachineConfiguration machineConfiguration) {
230         CURLOG.log(Level.WARNING, "file DAO: machine configuration not implemented" ) ;
231 
232     }
233 
234     @Override
235     public MachineConfiguration getMachineConfiguration(String macAddress) {
236         CURLOG.log(Level.WARNING, "file DAO: machine configuration not implemented" ) ;
237         return null;
238     }
239 
240     @Override
241     public List<?> simpleHQLRequest(String hqlString) {
242         CURLOG.log(Level.WARNING, "file DAO: hql not implemented" ) ;
243         return Collections.emptyList();
244     }
245 
246     @Override
247     public void close() {
248         // todo saves in files
249     }
250 }