openETCS
case study for the European Train Control System developed for the authors dissertation
VMgenerator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010-2011
3  Johannes Feuser <johannes.feuser@googlemail.com>
4  This file is part of the openETCS library.
5 
6  The openETCS library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  any later version.
10 
11  The openETCS library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with the openETCS library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /*!
21  * \author Johannes Feuser <feuser@uni-bremen.de>
22  * \brief
23  */
24 
25 #include "VMgenerator.h"
26 #include "../../DSM/SyntaxTree.h"
27 
28 
29 namespace oETCS {
30 
31 namespace GEN {
32 
33 
34 bool CVMGenerator::HasPSM(::DSM::CSyntaxTree * const pSyntaxTree) throw()
35 {
36  // Bouml preserved body begin 000FFD02
37  bool bPSM(false);
38  ::GOPPRR::CProject* pProject(nullptr);
39  ::DSM::CGOPPRRSyntaxTree* pGOPPRR(nullptr);
40  decltype (::GOPPRR::CProject::m_PropertySet.begin()) p;
41 
42 
43 
44  // convert plain syntax tree into GOPPRR
45  pGOPPRR = dynamic_cast< ::DSM::CGOPPRRSyntaxTree* >(pSyntaxTree);
46 
47  // check, if tree could be converted
48  if (pGOPPRR == nullptr)
49  {
50  // throw exception
51  throw (::oETCS::GEN::Error::CParameter("unknown syntax tree format not supported"));
52 
53  } // if (pGOPPRR == nullptr)
54 
55  // get project from tree
56  pProject = pGOPPRR->GetProject();
57 
58  // proceed all properties
59  for (p = pProject->m_PropertySet.begin(); p != pProject->m_PropertySet.end() && not bPSM; p++)
60  {
61  // check, if type of current property is for external declaration (PSM)
62  if (p->second.m_Type == "IsExternal" && p->second.m_Value == "T")
63  {
64  // set found flag to true
65  bPSM = true;
66 
67  } // if (p->second.m_Type == "IsExternal" && p->second.m_Value == "T")
68 
69  } // for (p = pProject->m_PropertySet.begin(); p != pProject->m_PropertySet.end() && not bPSM; p++)
70 
71  // return flag
72  return (bPSM);
73  // Bouml preserved body end 000FFD02
74 
75 } // bool CVMGenerator::HasPSM() throw()
76 
77 
78 
79 
80 void CVMGenerator::Generate(::DSM::CSyntaxTree * const pSyntaxTree, ::std::ostream & OutStream) throw(::oETCS::GEN::Error::CException)
81 {
82  // Bouml preserved body begin 00097282
83  ::std::time_t Now = ::std::chrono::system_clock::to_time_t(::std::chrono::system_clock::now());
84  ::std::string VMID;
85  ::GOPPRR::CGraph* pRootGraph(nullptr);
86  ::DSM::CGOPPRRSyntaxTree* pGOPPRR(nullptr);
87 
88 
89 
90  // convert plain syntax tree into GOPPRR
91  pGOPPRR = dynamic_cast< ::DSM::CGOPPRRSyntaxTree* >(pSyntaxTree);
92 
93  // check, if tree could be converted
94  if (pGOPPRR == nullptr)
95  {
96  // throw exception
97  throw (::oETCS::GEN::Error::CParameter("unknown syntax tree format not supported"));
98 
99  } // if (pGOPPRR == nullptr)
100 
101  // get root graph from syntax tree
102  pRootGraph = pGOPPRR->GetRootGraph();
103 
104  // get VM ID from root graph
105  VMID = pRootGraph->m_ID;
106 
107  // remove all white spaces from VM ID
108  while (VMID.find(" ") != std::string::npos)
109  {
110  // replace current white space with _
111  VMID.replace(VMID.find(" "), 1, "_");
112 
113  } // while (VMID.find(" ") != std::string::npos)
114 
115 
116  OutStream <<
117  "# Copyright (C) 2010-2012\n"
118  "# Johannes Feuser <feuser@uni-bremen.de>\n"
119  "# This file is part of the openETCS project\n"
120  "#\n"
121  "# The openETCS library is free software: you can redistribute it and/or modify\n"
122  "# it under the terms of the GNU General Public License as published by\n"
123  "# the Free Software Foundation, either version 3 of the License, or\n"
124  "# any later version.\n"
125  "#\n"
126  "# The openETCS library is distributed in the hope that it will be useful,\n"
127  "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
128  "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
129  "# GNU General Public License for more details.\n"
130  "#\n"
131  "# You should have received a copy of the GNU General Public License\n"
132  "# along with the openETCS library. If not, see <http://www.gnu.org/licenses/>.\n"
133  "#\n"
134  "# Xen configuration file for the EVC PIM virtual machine/DomU\n"
135  "# generated for openETCS EVC by VM configuration generator\n"
136  "# time point (UTC) of generation: " << ::std::asctime(::std::gmtime(&Now)) << "\n"
137  "\n"
138  "kernel = \"/boot/<xen-kernel-image>\" # <...> must be replaced\n"
139  "ramdisk = \"/boot/<xen-kernel-initramdisk>\" # <...> must be replaced\n"
140  "memory = 512\n"
141  "name = \"" << VMID << "_PIM\"\n"
142  "vif = ['ip=192.168.53.1']\n"
143  "disk = ['file:<PIM.img,sda1>,w','file:<PIMswap.img,sda2>,w'] # <...> must be replaced\n"
144  "root = \"/dev/sda1 ro\"\n"
145  "extra = \"2\"\n";
146 
147  // Bouml preserved body end 00097282
148 
149 } // void CVMGenerator::Generate() throw(::oETCS::GEN::Error::CException)
150 
151 
152 
153 
154 void CVMGenerator::GeneratePSM(::DSM::CSyntaxTree * const pSyntaxTree, ::std::ostream & OutStream) throw(::oETCS::GEN::Error::CException)
155 {
156  // Bouml preserved body begin 000FFC82
157  ::std::time_t Now = ::std::chrono::system_clock::to_time_t(::std::chrono::system_clock::now());
158  ::std::string VMID;
159  ::GOPPRR::CGraph* pRootGraph(nullptr);
160  ::DSM::CGOPPRRSyntaxTree* pGOPPRR(nullptr);
161 
162 
163 
164  // check, if PSM is required
165  if (this->HasPSM(pSyntaxTree))
166  {
167 
168  // convert plain syntax tree into GOPPRR
169  pGOPPRR = dynamic_cast< ::DSM::CGOPPRRSyntaxTree* >(pSyntaxTree);
170 
171  // check, if tree could be converted
172  if (pGOPPRR == nullptr)
173  {
174  // throw exception
175  throw (::oETCS::GEN::Error::CParameter("unknown syntax tree format not supported"));
176 
177  } // if (pGOPPRR == nullptr)
178 
179  // get root graph from syntax tree
180  pRootGraph = pGOPPRR->GetRootGraph();
181 
182  // get VM ID from root graph
183  VMID = pRootGraph->m_ID;
184 
185  // remove all white spaces from VM ID
186  while (VMID.find(" ") != std::string::npos)
187  {
188  // replace current white space with _
189  VMID.replace(VMID.find(" "), 1, "_");
190 
191  } // while (VMID.find(" ") != std::string::npos)
192 
193 
194  OutStream <<
195  "# Copyright (C) 2010-2012\n"
196  "# Johannes Feuser <feuser@uni-bremen.de>\n"
197  "# This file is part of the openETCS project\n"
198  "#\n"
199  "# The openETCS library is free software: you can redistribute it and/or modify\n"
200  "# it under the terms of the GNU General Public License as published by\n"
201  "# the Free Software Foundation, either version 3 of the License, or\n"
202  "# any later version.\n"
203  "#\n"
204  "# The openETCS library is distributed in the hope that it will be useful,\n"
205  "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
206  "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
207  "# GNU General Public License for more details.\n"
208  "#\n"
209  "# You should have received a copy of the GNU General Public License\n"
210  "# along with the openETCS library. If not, see <http://www.gnu.org/licenses/>.\n"
211  "#\n"
212  "# Xen configuration file for the EVC PSM virtual machine/DomU\n"
213  "# generated for openETCS EVC by VM configuration generator\n"
214  "# time point (UTC) of generation: " << ::std::asctime(::std::gmtime(&Now)) << "\n"
215  "\n"
216  "kernel = \"/boot/<xen-kernel-image>\" # <...> must be replaced\n"
217  "ramdisk = \"/boot/<xen-kernel-initramdisk>\" # <...> must be replaced\n"
218  "memory = 512\n"
219  "name = \"" << VMID << "_PSM\"\n"
220  "vif = ['ip=192.168.53.1']\n"
221  "disk = ['file:<PSM.img,sda1>,w','file:<PSMswap.img,sda2>,w'] # <...> must be replaced\n"
222  "root = \"/dev/sda1 ro\"\n"
223  "extra = \"2\"\n";
224 
225  } // if (this->HasPSM(pSyntaxTree))
226 
227  // Bouml preserved body end 000FFC82
228 
229 } // void CVMGenerator::GeneratePSM() throw(::oETCS::GEN::Error::CException)
230 
231 
232 
233 
234 
235 
236 } // namespace oETCS::GEN
237 
238 } // namespace oETCS

Copyright (C) 2010-2012 Johannes Feuser (feuser@uni-bremen.de)
The openETCS library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
The openETCS library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the openETCS library. If not, see "http://www.gnu.org/licenses/.