summaryrefslogtreecommitdiffstats
path: root/tools/migrateOdlParents.py
blob: a7175b8b9067ee7ff51a9208930a3c05f190caef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/python3
import os
import argparse
import subprocess
import re
import shutil
import tempfile
from lib.pomfile import PomFile

DEFAULT_PARENT_GROUPID="org.onap.ccsdk.parent"
DEFAULT_PARENT_VERSION="2.4.0-SNAPSHOT"
DEFAULT_STRICT=True
USE_OLD_SERVLET_API=True

class OdlParentMigrator:

    def __init__(self,odlSourcePath, odlParentPath=None, groupId=DEFAULT_PARENT_GROUPID, version=DEFAULT_PARENT_VERSION, strict=DEFAULT_STRICT):
        self.odlSourcePath=odlSourcePath
        self.mvnbin = "/usr/bin/mvn"
        self.version = version
        self.groupId = groupId
        self.strict = strict
        if odlParentPath is None:
            odlParentPath = os.path.abspath(os.path.dirname(os.path.realpath(__file__))+"/../odlparent")
        self.odlParentPath=odlParentPath
        self.parentPath =os.path.abspath(odlParentPath+'/../') 


    def getMvnRepoVersion(self, groupId, artifactId):
        path="{}/system/{}/{}".format(self.odlSourcePath,groupId.replace('.','/'),artifactId)
        if not os.path.exists(path):
            return None
        folders =[f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
        if len(folders)<1:
            return None
        return folders[0]
    
    def getMvnRepoVersions(self, groupId, artifactId):
        path="{}/system/{}/{}".format(self.odlSourcePath,groupId.replace('.','/'),artifactId)
        if not os.path.exists(path):
            return None
        folders =[f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
        if len(folders)<1:
            return None
        return folders

    def migrateInstalledOdlBom(self) -> bool:
        success = True
        print("migrating installed-odl-bom")
        root=os.path.abspath(self.odlParentPath+'/..')
        self.exec(('cd {src}/system && '+
            '{root}/tools/mkbom.sh {groupId} {artifactId} {version}> '+
            '{root}/installed-odl-bom/pom.xml && cd -').format(
            root=root,src=self.odlSourcePath,
            parent=self.odlParentPath,groupId=self.groupId,
            artifactId='installed-odl-bom', version=self.version))
        if USE_OLD_SERVLET_API:
            pom = PomFile('{}/installed-odl-bom/pom.xml'.format(root))
            success = pom.setDependencyManagementVersion('javax.servlet','javax.servlet-api','3.1.0')

        print("done")
        return success

    def migrateDependenciesBom(self) -> bool:
        success = True
        print("migrating dependencies-bom")
    
        print("done" if success else "failed")
        return success

    def migrateSetupProperties(self) -> bool:
        success = True
        print("migrating setup")
        mdsalVersion=self.getMvnRepoVersion('org.opendaylight.mdsal','mdsal-binding-api')
        odlBundleVersion=self.getMvnRepoVersion('org.opendaylight.odlparent','features-odlparent')
        mdsalItVersion=self.getMvnRepoVersion('org.opendaylight.controller','features-controller')
        yangVersion = self.getMvnRepoVersion('org.opendaylight.yangtools','yang-common')
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/binding-parent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(mdsalVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/bundle-parent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/feature-repo-parent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/karaf4-parent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/mdsal-it-parent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(mdsalItVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/odlparent-lite.properties',
            'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/odlparent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))
        self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/single-feature-parent.properties',
            'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion))


        templatePom = PomFile(self.odlParentPath+'/setup/src/main/resources/pom-template.xml')
        x = templatePom.setXmlValue('/project/properties/odl.controller.mdsal.version',mdsalVersion)
        success = success and x
        x = templatePom.setXmlValue('/project/properties/odl.mdsal.version',odlBundleVersion)
        success = success and x
        x = templatePom.setXmlValue('/project/properties/odl.mdsal.model.version',odlBundleVersion)
        success = success and x
        x = templatePom.setXmlValue('/project/properties/odl.netconf.restconf.version',mdsalVersion)
        success = success and x
        x = templatePom.setXmlValue('/project/properties/odl.netconf.netconf.version',mdsalVersion)
        success = success and x
        x = templatePom.setXmlValue('/project/properties/odl.netconf.sal.rest.docgen.version',mdsalVersion)
        success = success and x
        
        x = templatePom.setXmlValue('/project/properties/commons.codec.version',
            self.getMvnRepoVersion('commons-codec','commons-codec'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/commons.lang3.version',
            self.getMvnRepoVersion('org.apache.commons','commons-lang3'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/commons.lang.version',
            self.getMvnRepoVersion('commons-lang','commons-lang'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/commons.net.version',
            self.getMvnRepoVersion('commons-net','commons-net'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/eclipse.persistence.version',
            self.getMvnRepoVersion('org.eclipse.persistence','org.eclipse.persistence.core'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/gson.version',
            self.getMvnRepoVersion('com.google.code.gson','gson'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/guava.version',
            self.getMvnRepoVersion('com.google.guava','guava'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/jackson.version',
            self.getMvnRepoVersion('com.fasterxml.jackson.core','jackson-core'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/javassist.version',
            self.getMvnRepoVersion('org.javassist','javassist'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/jersey.version',
            self.getMvnRepoVersion('org.glassfish.jersey.core','jersey-common'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/jersey.client.version',
            self.getMvnRepoVersion('org.glassfish.jersey.core','jersey-client'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/org.json.version',
            self.getMvnRepoVersion('org.json','json'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/netty.version',
            self.getMvnRepoVersion('io.netty','netty-common'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/slf4j.version',
            self.getMvnRepoVersion('org.slf4j','slf4j-api'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/derby.version',
            self.getMvnRepoVersion('org.apache.derby','derby'))
        success = success and x
        x = templatePom.setXmlValue('/project/properties/jetty.version',
            self.getMvnRepoVersion('org.eclipse.jetty','jetty-http'))        
        success = success and x
        print("done" if success else "failed")
        return success

    def migrateDependenciesOdlBom(self):
        success = True
        print("migrating dependencies-odl-bom")
        bgpVersion = self.getMvnRepoVersion('org.opendaylight.bgpcep','topology-api')
        controllerVersion = self.getMvnRepoVersion('org.opendaylight.controller', 'blueprint')
        mdsalVersion=self.getMvnRepoVersion('org.opendaylight.mdsal','mdsal-binding-api')
        netconfVersion = self.getMvnRepoVersion('org.opendaylight.netconf','netconf-impl')

        pomFile = PomFile(os.path.abspath(self.parentPath+'/dependencies-odl-bom/pom.xml'))
        x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=bgp-artifacts]/version',bgpVersion)
        success = success and x
        x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=controller-artifacts]/version',controllerVersion)
        success = success and x
        x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=mdsal-artifacts]/version',mdsalVersion)
        success = success and x
        x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=netconf-artifacts]/version',netconfVersion)
        success = success and x
        x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-binding-broker-impl]/version',netconfVersion, True)
        success = success and x
        # at the moment not possible because of dependent variable in path after value to set
        # x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-binding-broker-impl,type=test-jar]/version',netconfVersion)
        # success = success and x
        x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-test-model]/version',netconfVersion)
        success = success and x
        print("done" if success else "failed")
        return success

    def setParentValues(self):
        print("setting all other parents")
        # find all pom.xml files with parent to set
        pomfiles=[os.path.abspath(self.parentPath+'/pom.xml'),
            os.path.abspath(self.parentPath+'/dependencies-bom/pom.xml'),
            os.path.abspath(self.odlParentPath+'/pom.xml'),
            os.path.abspath(self.odlParentPath+'/setup/pom.xml'),
            os.path.abspath(self.parentPath+'/springboot/pom.xml'),
            os.path.abspath(self.parentPath+'/springboot/spring-boot-setup/pom.xml')]
        
        success=True
        for file in pomfiles:
            pomfile = PomFile(file)
            if pomfile.hasParent():
                x = pomfile.setXmlValue('/project/parent/groupId',self.groupId)
                success = success and x
                x = pomfile.setXmlValue('/project/parent/version',self.version)
                success = success and x
            
        # find all pom.xml files with groupId and version to set
        pomfiles=PomFile.findAll(os.path.abspath(self.odlParentPath+'/..'))
        for file in pomfiles:
            pomfile = PomFile(file)
            x = pomfile.setXmlValue('/project/groupId',self.groupId)
            success = success and x
            x = pomfile.setXmlValue('/project/version',self.version)
            success = success and x
        
        # set only groupId for odl template
        pomfile = PomFile(self.odlParentPath+'/setup/src/main/resources/pom-template.xml')
        x = pomfile.setXmlValue('/project/groupId',self.groupId)
        success = success and x
        print("done" if success else "failed")
        return success
        
    def execMaven(self, command):
        print(self.execToStdOut(self.mvnbin,command))


        

    '''
    Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
    `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
    '''
    def replaceInFile(self, filename, pattern, replacement):
   
        # For efficiency, precompile the passed regular expression.
        pattern_compiled = re.compile(pattern)

        # For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary
        # writing with updating). This is usually a good thing. In this case,
        # however, binary writing imposes non-trivial encoding constraints trivially
        # resolved by switching to text writing. Let's do that.
        with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
            with open(filename) as src_file:
                for line in src_file:
                    tmp_file.write(pattern_compiled.sub(replacement, line))

        # Overwrite the original file with the munged temporary file in a
        # manner preserving file attributes (e.g., permissions).
        shutil.copystat(filename, tmp_file.name)
        shutil.move(tmp_file.name, filename)

    def exec(self, bin, params=""):
        output = subprocess.Popen(
            bin+" "+params, shell=True, stdout=subprocess.PIPE).stdout.read()
        return output
    def execToStdOut(self, bin, params=""):
        process = subprocess.Popen(
            (bin+" "+params).split(' '), shell=False)
        process.communicate()
    
    def run(self):
        print("starting ONAP odl parent migration")
        print("odl src={}".format(self.odlSourcePath))
        print("target ={}".format(self.odlParentPath))
        x = self.migrateInstalledOdlBom()
        if self.strict and not x:
            exit(1)
        x = self.migrateDependenciesBom()
        if self.strict and not x:
            exit(1)
        x = self.migrateDependenciesOdlBom()
        if self.strict and not x:
            exit(1)
        x = self.migrateSetupProperties()
        if self.strict and not x:
            exit(1)
        x = self.setParentValues()
        if self.strict and not x:
            exit(1)
#        self.execMaven('clean install -f {}'.format(self.odlParentPath+'/setup'))
#        self.execMaven('clean install -f {}'.format(self.parentPath))

parser = argparse.ArgumentParser(description='ONAP odl parent migration tool')
parser.add_argument('--src', type=str, required=True, help='the source folder where odl is located')
parser.add_argument('--group-id', type=str, required=False,default=DEFAULT_PARENT_GROUPID, help='groupid for the parents')
parser.add_argument('--version', type=str, required=False,default=DEFAULT_PARENT_VERSION, help='version')
parser.add_argument('--non-strict', action='store_false' if DEFAULT_STRICT else 'store_true', help='determine stopping script if something cannot be set')
args = parser.parse_args()

migrator = OdlParentMigrator(args.src,None,args.group_id, args.version, args.non_strict)
migrator.run()