blob: 124234ac27d6e027ace57c872f7d00b893ec50c7 [file] [log] [blame]
Michael DÜrre7bb133e2022-01-18 10:06:31 +01001#!/usr/bin/python3
2import os
3import argparse
4import subprocess
5import re
6import shutil
7import tempfile
8from lib.pomfile import PomFile
9
10DEFAULT_PARENT_GROUPID="org.onap.ccsdk.parent"
Michael Dürred4313f32024-03-14 07:48:48 +010011DEFAULT_PARENT_VERSION="2.7.0-SNAPSHOT"
Michael DÜrre7bb133e2022-01-18 10:06:31 +010012DEFAULT_STRICT=True
13USE_OLD_SERVLET_API=True
14
15class OdlParentMigrator:
16
17 def __init__(self,odlSourcePath, odlParentPath=None, groupId=DEFAULT_PARENT_GROUPID, version=DEFAULT_PARENT_VERSION, strict=DEFAULT_STRICT):
18 self.odlSourcePath=odlSourcePath
19 self.mvnbin = "/usr/bin/mvn"
20 self.version = version
21 self.groupId = groupId
22 self.strict = strict
23 if odlParentPath is None:
24 odlParentPath = os.path.abspath(os.path.dirname(os.path.realpath(__file__))+"/../odlparent")
25 self.odlParentPath=odlParentPath
26 self.parentPath =os.path.abspath(odlParentPath+'/../')
27
28
29 def getMvnRepoVersion(self, groupId, artifactId):
30 path="{}/system/{}/{}".format(self.odlSourcePath,groupId.replace('.','/'),artifactId)
31 if not os.path.exists(path):
32 return None
33 folders =[f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
34 if len(folders)<1:
35 return None
36 return folders[0]
37
38 def getMvnRepoVersions(self, groupId, artifactId):
39 path="{}/system/{}/{}".format(self.odlSourcePath,groupId.replace('.','/'),artifactId)
40 if not os.path.exists(path):
41 return None
42 folders =[f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
43 if len(folders)<1:
44 return None
45 return folders
46
47 def migrateInstalledOdlBom(self) -> bool:
48 success = True
49 print("migrating installed-odl-bom")
50 root=os.path.abspath(self.odlParentPath+'/..')
51 self.exec(('cd {src}/system && '+
52 '{root}/tools/mkbom.sh {groupId} {artifactId} {version}> '+
53 '{root}/installed-odl-bom/pom.xml && cd -').format(
54 root=root,src=self.odlSourcePath,
55 parent=self.odlParentPath,groupId=self.groupId,
56 artifactId='installed-odl-bom', version=self.version))
57 if USE_OLD_SERVLET_API:
58 pom = PomFile('{}/installed-odl-bom/pom.xml'.format(root))
59 success = pom.setDependencyManagementVersion('javax.servlet','javax.servlet-api','3.1.0')
60
61 print("done")
62 return success
63
64 def migrateDependenciesBom(self) -> bool:
65 success = True
66 print("migrating dependencies-bom")
67
68 print("done" if success else "failed")
69 return success
70
71 def migrateSetupProperties(self) -> bool:
72 success = True
73 print("migrating setup")
74 mdsalVersion=self.getMvnRepoVersion('org.opendaylight.mdsal','mdsal-binding-api')
75 odlBundleVersion=self.getMvnRepoVersion('org.opendaylight.odlparent','features-odlparent')
76 mdsalItVersion=self.getMvnRepoVersion('org.opendaylight.controller','features-controller')
77 yangVersion = self.getMvnRepoVersion('org.opendaylight.yangtools','yang-common')
78 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/binding-parent.properties',
79 'odlparent.version=.*','odlparent.version={}'.format(mdsalVersion))
80 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/bundle-parent.properties',
81 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
82 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/feature-repo-parent.properties',
83 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
84 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/karaf4-parent.properties',
85 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
86 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/mdsal-it-parent.properties',
87 'odlparent.version=.*','odlparent.version={}'.format(mdsalItVersion))
88 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/odlparent-lite.properties',
89 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
90 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/odlparent.properties',
91 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
92 self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/single-feature-parent.properties',
93 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
94
95
96 templatePom = PomFile(self.odlParentPath+'/setup/src/main/resources/pom-template.xml')
Michael Dürred4313f32024-03-14 07:48:48 +010097 # x = templatePom.setXmlValue('/project/properties/odl.controller.mdsal.version',mdsalVersion)
98 # success = success and x
Michael DÜrre7bb133e2022-01-18 10:06:31 +010099 x = templatePom.setXmlValue('/project/properties/odl.mdsal.version',odlBundleVersion)
100 success = success and x
101 x = templatePom.setXmlValue('/project/properties/odl.mdsal.model.version',odlBundleVersion)
102 success = success and x
103 x = templatePom.setXmlValue('/project/properties/odl.netconf.restconf.version',mdsalVersion)
104 success = success and x
105 x = templatePom.setXmlValue('/project/properties/odl.netconf.netconf.version',mdsalVersion)
106 success = success and x
107 x = templatePom.setXmlValue('/project/properties/odl.netconf.sal.rest.docgen.version',mdsalVersion)
108 success = success and x
109
110 x = templatePom.setXmlValue('/project/properties/commons.codec.version',
111 self.getMvnRepoVersion('commons-codec','commons-codec'))
112 success = success and x
113 x = templatePom.setXmlValue('/project/properties/commons.lang3.version',
114 self.getMvnRepoVersion('org.apache.commons','commons-lang3'))
115 success = success and x
116 x = templatePom.setXmlValue('/project/properties/commons.lang.version',
117 self.getMvnRepoVersion('commons-lang','commons-lang'))
118 success = success and x
119 x = templatePom.setXmlValue('/project/properties/commons.net.version',
120 self.getMvnRepoVersion('commons-net','commons-net'))
121 success = success and x
122 x = templatePom.setXmlValue('/project/properties/eclipse.persistence.version',
123 self.getMvnRepoVersion('org.eclipse.persistence','org.eclipse.persistence.core'))
124 success = success and x
125 x = templatePom.setXmlValue('/project/properties/gson.version',
126 self.getMvnRepoVersion('com.google.code.gson','gson'))
127 success = success and x
128 x = templatePom.setXmlValue('/project/properties/guava.version',
129 self.getMvnRepoVersion('com.google.guava','guava'))
130 success = success and x
131 x = templatePom.setXmlValue('/project/properties/jackson.version',
132 self.getMvnRepoVersion('com.fasterxml.jackson.core','jackson-core'))
133 success = success and x
134 x = templatePom.setXmlValue('/project/properties/javassist.version',
135 self.getMvnRepoVersion('org.javassist','javassist'))
136 success = success and x
137 x = templatePom.setXmlValue('/project/properties/jersey.version',
138 self.getMvnRepoVersion('org.glassfish.jersey.core','jersey-common'))
139 success = success and x
140 x = templatePom.setXmlValue('/project/properties/jersey.client.version',
141 self.getMvnRepoVersion('org.glassfish.jersey.core','jersey-client'))
142 success = success and x
143 x = templatePom.setXmlValue('/project/properties/org.json.version',
144 self.getMvnRepoVersion('org.json','json'))
145 success = success and x
146 x = templatePom.setXmlValue('/project/properties/netty.version',
147 self.getMvnRepoVersion('io.netty','netty-common'))
148 success = success and x
149 x = templatePom.setXmlValue('/project/properties/slf4j.version',
150 self.getMvnRepoVersion('org.slf4j','slf4j-api'))
151 success = success and x
152 x = templatePom.setXmlValue('/project/properties/derby.version',
153 self.getMvnRepoVersion('org.apache.derby','derby'))
154 success = success and x
155 x = templatePom.setXmlValue('/project/properties/jetty.version',
156 self.getMvnRepoVersion('org.eclipse.jetty','jetty-http'))
157 success = success and x
158 print("done" if success else "failed")
159 return success
160
161 def migrateDependenciesOdlBom(self):
162 success = True
163 print("migrating dependencies-odl-bom")
164 bgpVersion = self.getMvnRepoVersion('org.opendaylight.bgpcep','topology-api')
165 controllerVersion = self.getMvnRepoVersion('org.opendaylight.controller', 'blueprint')
166 mdsalVersion=self.getMvnRepoVersion('org.opendaylight.mdsal','mdsal-binding-api')
Michael Dürred4313f32024-03-14 07:48:48 +0100167 netconfVersion = self.getMvnRepoVersion('org.opendaylight.netconf','netconf-api')
Michael DÜrre7bb133e2022-01-18 10:06:31 +0100168
169 pomFile = PomFile(os.path.abspath(self.parentPath+'/dependencies-odl-bom/pom.xml'))
Michael Dürred4313f32024-03-14 07:48:48 +0100170 x = pomFile.setXmlValue('/project/version',self.version)
171 success = success and x
172 x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=bgpcep-artifacts]/version',bgpVersion)
Michael DÜrre7bb133e2022-01-18 10:06:31 +0100173 success = success and x
174 x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=controller-artifacts]/version',controllerVersion)
175 success = success and x
176 x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=mdsal-artifacts]/version',mdsalVersion)
177 success = success and x
Michael Dürred4313f32024-03-14 07:48:48 +0100178 # x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=netconf-artifacts]/version',netconfVersion)
179 # success = success and x
180 # x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-binding-broker-impl]/version',netconfVersion, True)
181 # success = success and x
Michael DÜrre7bb133e2022-01-18 10:06:31 +0100182 # at the moment not possible because of dependent variable in path after value to set
183 # x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-binding-broker-impl,type=test-jar]/version',netconfVersion)
184 # success = success and x
Michael Dürred4313f32024-03-14 07:48:48 +0100185 # x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-test-model]/version',netconfVersion)
186 # success = success and x
Michael DÜrre7bb133e2022-01-18 10:06:31 +0100187 print("done" if success else "failed")
188 return success
189
190 def setParentValues(self):
191 print("setting all other parents")
192 # find all pom.xml files with parent to set
193 pomfiles=[os.path.abspath(self.parentPath+'/pom.xml'),
194 os.path.abspath(self.parentPath+'/dependencies-bom/pom.xml'),
195 os.path.abspath(self.odlParentPath+'/pom.xml'),
196 os.path.abspath(self.odlParentPath+'/setup/pom.xml'),
197 os.path.abspath(self.parentPath+'/springboot/pom.xml'),
198 os.path.abspath(self.parentPath+'/springboot/spring-boot-setup/pom.xml')]
199
200 success=True
201 for file in pomfiles:
202 pomfile = PomFile(file)
203 if pomfile.hasParent():
204 x = pomfile.setXmlValue('/project/parent/groupId',self.groupId)
205 success = success and x
206 x = pomfile.setXmlValue('/project/parent/version',self.version)
207 success = success and x
208
209 # find all pom.xml files with groupId and version to set
210 pomfiles=PomFile.findAll(os.path.abspath(self.odlParentPath+'/..'))
211 for file in pomfiles:
212 pomfile = PomFile(file)
213 x = pomfile.setXmlValue('/project/groupId',self.groupId)
214 success = success and x
215 x = pomfile.setXmlValue('/project/version',self.version)
216 success = success and x
217
218 # set only groupId for odl template
219 pomfile = PomFile(self.odlParentPath+'/setup/src/main/resources/pom-template.xml')
220 x = pomfile.setXmlValue('/project/groupId',self.groupId)
221 success = success and x
222 print("done" if success else "failed")
223 return success
224
225 def execMaven(self, command):
226 print(self.execToStdOut(self.mvnbin,command))
227
228
229
230
231 '''
232 Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
233 `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
234 '''
235 def replaceInFile(self, filename, pattern, replacement):
236
237 # For efficiency, precompile the passed regular expression.
238 pattern_compiled = re.compile(pattern)
239
240 # For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary
241 # writing with updating). This is usually a good thing. In this case,
242 # however, binary writing imposes non-trivial encoding constraints trivially
243 # resolved by switching to text writing. Let's do that.
244 with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
245 with open(filename) as src_file:
246 for line in src_file:
247 tmp_file.write(pattern_compiled.sub(replacement, line))
248
249 # Overwrite the original file with the munged temporary file in a
250 # manner preserving file attributes (e.g., permissions).
251 shutil.copystat(filename, tmp_file.name)
252 shutil.move(tmp_file.name, filename)
253
254 def exec(self, bin, params=""):
255 output = subprocess.Popen(
256 bin+" "+params, shell=True, stdout=subprocess.PIPE).stdout.read()
257 return output
258 def execToStdOut(self, bin, params=""):
259 process = subprocess.Popen(
260 (bin+" "+params).split(' '), shell=False)
261 process.communicate()
262
263 def run(self):
264 print("starting ONAP odl parent migration")
265 print("odl src={}".format(self.odlSourcePath))
266 print("target ={}".format(self.odlParentPath))
267 x = self.migrateInstalledOdlBom()
268 if self.strict and not x:
269 exit(1)
270 x = self.migrateDependenciesBom()
271 if self.strict and not x:
272 exit(1)
273 x = self.migrateDependenciesOdlBom()
274 if self.strict and not x:
275 exit(1)
276 x = self.migrateSetupProperties()
277 if self.strict and not x:
278 exit(1)
279 x = self.setParentValues()
280 if self.strict and not x:
281 exit(1)
282# self.execMaven('clean install -f {}'.format(self.odlParentPath+'/setup'))
283# self.execMaven('clean install -f {}'.format(self.parentPath))
284
285parser = argparse.ArgumentParser(description='ONAP odl parent migration tool')
286parser.add_argument('--src', type=str, required=True, help='the source folder where odl is located')
287parser.add_argument('--group-id', type=str, required=False,default=DEFAULT_PARENT_GROUPID, help='groupid for the parents')
288parser.add_argument('--version', type=str, required=False,default=DEFAULT_PARENT_VERSION, help='version')
289parser.add_argument('--non-strict', action='store_false' if DEFAULT_STRICT else 'store_true', help='determine stopping script if something cannot be set')
290args = parser.parse_args()
291
292migrator = OdlParentMigrator(args.src,None,args.group_id, args.version, args.non_strict)
293migrator.run()