diff options
Diffstat (limited to 'dcae-controller-core/dcae-controller-operation-utils/src')
3 files changed, 2180 insertions, 0 deletions
diff --git a/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DcaePlatformConfiguration.groovy b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DcaePlatformConfiguration.groovy new file mode 100644 index 0000000..234e98a --- /dev/null +++ b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DcaePlatformConfiguration.groovy @@ -0,0 +1,1115 @@ + +/*- + * ============LICENSE_START========================================== + * OPENECOMP - DCAE + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + */ + +package org.openecomp.dcae.controller.operation.utils + +import static org.openecomp.ncomp.sirius.manager.console.Utils.* +import static org.openecomp.ncomp.utils.Base64.decode64; +import static org.openecomp.dcae.controller.operation.utils.DocUtils.* + +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.spec.KeySpec; +import java.security.spec.PKCS8EncodedKeySpec; + +import javax.crypto.Cipher; + +import org.json.JSONArray +import org.junit.After; + +import groovy.text.SimpleTemplateEngine + +import org.openecomp.ncomp.utils.CryptoUtils; +import org.openecomp.ncomp.webservice.utils.IpUtils + +class DcaePlatformConfiguration { + def env + def release + String envName + def tenants + def locationTypes + def serviceName + def instanceName + def tenant + def locationName + def streams + def networking + def ipAssignments + def passwords + Map deploymentType = [:] + Map types = [:] + def vms // for now use old setup. + def dockers // for now use old setup. + + public DcaePlatformConfiguration(String release, String envName, String root, String destRoot) { + DocUtils docUtils = new DocUtils(release,envName,root,destRoot) + this.envName = envName + this.release = release + vms = docUtils.vms() + dockers = docUtils.dockers() + env = file2object("$release-$envName/env.yaml", false) ?: [:] + tenants = file2object("$release-$envName/tenants.yaml", false) + ipAssignments = file2object("$release-$envName/ip-assignment.yaml", false) ?: [:] + locationTypes = file2object("$release/location-types.yaml", false) + streams = file2object("$release/streams.yaml", false) + networking = file2object("$release/networking.yaml") + passwords = file2object("$release-$envName/certificates/passwords.yaml", false) ?: [:] + tenants.each { n, v -> + v.tenantName = n + if (locationTypes[v.type].tenantNames == null) + locationTypes[v.type].tenantNames = [] + locationTypes[v.type].tenantNames += n + } + locationTypes.each { n, v -> + def deployments = [] + [ + 'vm-deployments', + 'docker-deployments', + 'cdap-deployments', + 'other-deployments' + ].each { t -> + def typeDir = t.replace("deployments","templates") + def typeName = t.replace("deployments","type") +// println "n=$n v=$v t=$t" + v[t].each { n1, v1 -> + deployments += n1 + // println "n1=$n1 v1=$v1 $release/$typeDir/${v1[typeName]}.yaml" + deploymentType[n1] = v1 + v1.type = file2object("$release/$typeDir/${v1[typeName]}.yaml", false) + if (v1.type == null) { + println "n1=$n1 v1=$v1" + System.err.println "Unknown type: $release/$typeDir/${v1[typeName]}.yaml" + System.exit(3) + } + types[v1[typeName]] = v1.type + } + } + locationTypes[n].deployments = deployments +// System.err.println "n=$n v=${locationTypes[n].deployments} $deployments" + } + deploymentType.each { n,v -> + addInstances(n) + } + } + + def init(s,i) { + serviceName = s + instanceName = i + tenant = tenants[i] + locationName = i + } + + def p(o) { + object2json(o).toString(2) + } + + def addInstances(String n) { + def instances = [:] + deploymentType[n].instances = instances + deploymentType[n].tenantNames = [] + tenants.each { tenantName, tenant -> + // System.err.println "tenantName $tenantName $tenant.type ${locationTypes[tenant.type].deployments}" + locationTypes[tenant.type].deployments.each { n1 -> + // println "$n $tenantName $n1" + if (n != n1) return + deploymentType[n].tenantNames += tenantName + def instance = [:] + instances[tenantName] = instance + if (n.startsWith("vm-")) { + // println "$n $tenantName $n1 ${p(vms[n])}" + instance.servers = vms[n].locations["$n1-$tenantName"].vms + if (instance.servers == null) { + println "No servers for: $n $tenantName $n1 ${p(vms[n])}" + System.exit(3) + } + instance.servers.each { n2, v2 -> + v2.ip = ipAssignments[n2] ?: "TBD - ip-assignment.yaml@$n2" + v2['dns-cnames'] = [] + v2['dns-gss'] = [] + v2['dns-arecords'] = [ "${n2}.${tenant['dns-surfix']}"] + def envDns = env.get("dns") ?: [:] + def shortName = deploymentType[n].type['short-name'] + def shortNameGlobal = "$shortName${envDns['global-fix'] ?: ''}" + def globalDns = envDns.get("service-suffix") ?: "TBD - env.yaml@dns.service-suffix" + def tenantDns = tenant.get("dns-surfix") ?: "TBD - tenants.yaml@${tenantName}.dns-surfix" + (deploymentType[n].type['dns-cnames'] ?: []).each { dns1 -> + if (dns1=="central" && tenant.type != "opendcae-central") return + if (dns1=="central-site" && tenant.type != "opendcae-central") return + v2['dns-cnames'] += dnsName(n,tenant,dns1) + } + (deploymentType[n].type['dns-gss'] ?: []).each { dns1 -> + if (dns1=="central" && tenant.type != "opendcae-central") return + v2['dns-gss'] += dnsName(n,tenant,dns1) + } + (deploymentType[n].type['dns-arecords'] ?: []).each { dns1 -> + if (dns1=="central" && tenant.type != "opendcae-central") return + v2['dns-arecords'] += dnsName(n,tenant,dns1) + } + // dns-service: [ drsub: [ 'dns-type' : 'arecords': 'cluster-type' : 'site' ] ] + (deploymentType[n].type['dns-service'] ?: [:]).each { n3, v3 -> + v2["dns-${v3['dns-type']}"] += dnsName(n,tenant,v3['cluster-type'],n3) + switch (v3['cluster-type']) { + case "gss" : + v2["dns-cnames"] += dnsName(n,tenant,"global",n3) + break + case "gss-edge" : + v2["dns-cnames"] += dnsName(n,tenant,"site",n3) +// println "$n ${p(v2)}" +// System.exit(3) + break + } + } +// println "$n ${p(v2)}" +// System.exit(3) + } + return + } +// println "$n $tenantName $n1 ${p(deploymentType[n])}" + def path = substitute(deploymentType[n].container,[:],n,tenantName) + def a = path.split("/") + instance.hostService = a[2] + instance.hostServiceInstance = a[4] + } + } +// println "$n ${p(instances)}" +// println "$n ${p(deploymentType[n])}" +// System.exit(0) + } + def dnsName(n,tenant,dnsType, extra = null) { + def tenant1 = tenant ?: [tenantName:"NA"] + def ee = envName.toLowerCase() + def envDns = env.get("dns") ?: [:] + if (deploymentType[n] == null) { + throw new RuntimeException("Unknown deploymentType: $n") + } + def shortName = deploymentType[n].type['short-name'] + def shortNameGlobal = "$shortName${envDns['global-fix'] ?: ''}" + def globalDns = envDns.get("service-suffix") ?: "TBD - env.yaml@dns.service-suffix" + def gssDns = env("dns.gss-idns-suffix") + def tenantDns = tenant1.get("dns-surfix") ?: "TBD - tenants.yaml@${tenant1.tenantName}.dns-surfix" + extra = (extra != null) ? "-$extra" : "" + switch (dnsType) { + case "gss": return "dcae-${shortNameGlobal}$extra-${ee}.${gssDns}" + case "gss-edge": return "dcae-${shortName}$extra-$tenant.tenantName-${ee}.${gssDns}" + case "global": + case "central": + case "central-site": + case "global-site": return "dcae-${shortNameGlobal}$extra-${ee}.${globalDns}" + case "site": return "dcae-${shortName}$extra-$tenant.tenantName-${ee}.${tenantDns}" + } + } + + def aEnv() { + switch (release) { + case "1607": return ! envName.contains("FTL"); break; + case "1610": return ! envName.equals("FTL2"); break; + default: return true; + } + } + + def genVmType(n) { + def o = types[n]['openstack-vm-type'] + def m = [:] + m.diskSizeGB = o.diskSizeGB ?: 0 + m.volumeSizeGB = o.volumeSizeGB ?: 0 + m.memorySizeMB = o.memorySizeGB ?: 4 + m.memorySizeMB *= 1024 + m.needPublicIp = "${o.needPublicIp ?: true}" + m.numberOfCores = o.numberOfCores ?: 2 + m.rootDiskSizeGB = o.rootDiskSizeGB ?: 80 + m.imageName = o.imageName ?: "m1.large" + if (! aEnv()) { + if (o['dev-diskSizeGB'] != null) m.diskSizeGB = o['dev-diskSizeGB'] + if (o['dev-volumeSizeGB'] != null) m.volumeSizeGB = o['dev-volumeSizeGB'] + if (o['dev-memorySizeMB'] != null) m.memorySizeMB = o['dev-memorySizeMB'] + } + m.incomingSecurityRules = [ + "manager": [ + "ipProtocol": "tcp", + "portRangeStart": "1", + "portRangeEnd": "59996", + ] + ] + if (n == "vm-docker-host") { + m.incomingSecurityRules['udp'] = [ + "ipProtocol": "udp", + "portRangeStart": "162", + "portRangeEnd": "6162", + ] + } + // println object2json(m).toString(2) + return m + } + + def serviceFqdn(deploymentTypeName = null, extra = null) { + if (deploymentTypeName == null) deploymentTypeName = serviceName + return dnsName(deploymentTypeName,tenant,"global",extra) + } + + def httpProtocol(deploymentTypeName = null) { + if (deploymentTypeName == null) deploymentTypeName = serviceName + // TODO + return "http" + } + + + def siteServiceFqdn(deploymentTypeName = null, extra = null) { + if (deploymentTypeName == null) deploymentTypeName = serviceName + return dnsName(deploymentTypeName,tenant,"site",extra) + } + + def serversFqdn(deploymentTypeName = null, tName = instanceName) { + def res = [] + servers(deploymentTypeName,tName).each { n -> res += "$n.${tenants[tName]['dns-surfix']}" } + return res + } + + def privateKey = null + def vmKey = null + def vmName = null + def password(s) { + def o = env.passwords ?: [:] + boolean error = false + s.split('\\.').each { n -> + if (error) return + if (o[n] == null) { + o = "TBD: env.yaml@passwords.$s" + error = true + } + else + o = o[n] + } + if (privateKey != null && vmKey != null && o.startsWith("rsa:")) { + def key = verifyKey(privateKey) + o = CryptoUtils.decryptPrivate(key, o.substring(4)); + def k = vmKey + if (vmKey.startsWith("rsa:")) { + k = CryptoUtils.decryptPrivate(key, vmKey.substring(4)); + } +// System.err.println "+++++ s=$serviceName i=$instanceName vm=$vmName s=$s k=$k o=$o" + o = "%{vm.decrypt('${CryptoUtils.encrypt(k,o)}')}" + } + return o.trim() + } + + public static String verifyKey(String privateKey) { + try { + def key = CryptoUtils.getKey(privateKey) + if (decode64(key) == null) { + System.err.println "ERROR: BAD private key: bad base64 encoding: $privateKey" + System.exit(1) + } + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + KeySpec keySpec = new PKCS8EncodedKeySpec(decode64(key)); + PrivateKey key1 = keyFactory.generatePrivate(keySpec); + return key + } catch (Exception e) { + System.err.println "ERROR: BAD private key: $privateKey" + System.exit(1) + } + + } + + + def streams(s) { + def o = env.streams ?: [:] + boolean error = false + s.split('\\.').each { n -> + if (error) return + if (o[n] == null) { + o = "TBD: env.yaml@streams.$s" + error = true + } + else + o = o[n] + } + return o + } + + def env(s) { + def o = env + boolean error = false + s.split('\\.').each { n -> + if (error) return + if (o[n] == null) { + o = "TBD:env.yaml@$s" + error = true + } + else + o = o[n] + } + return o + } + + def vmEnv(s) { + def o = (env['vm-templates'] ?: [:])[serviceName] ?: [:] + boolean error = false + s.split('\\.').each { n -> + if (error) return + if (o[n] == null) { + o = "TBD:env.yaml@vm-templates.${serviceName}.$s" + error = true + } + else + o = o[n] + } + return o + } + + def allServersFqdn(deploymentTypeName = null) { + if (deploymentTypeName == null) deploymentTypeName = serviceName + def d = deploymentType[deploymentTypeName] + def res = [] + if (d == null) return [ + "NO DeploymentType $deploymentTypeName" + ] + if (d.instances == null) return [ + "NO instances DeploymentType $deploymentTypeName" + ] + d.instances.each { instanceName, instance -> + instance.servers.each { n, v -> + res += "$n.${tenants[instanceName]['dns-surfix']}" + } + } + return res + } + + def servers(deploymentTypeName = null, tName = instanceName) { + if (deploymentTypeName == null) deploymentTypeName = serviceName + def d = deploymentType[deploymentTypeName] + def res = [] + if (d == null) return [ + "NO DeploymentType $deploymentTypeName" + ] + if (d.instances == null) return [ + "NO instances DeploymentType $deploymentTypeName" + ] + if (d.instances[tName] == null) return [ + "NO instance DeploymentType $deploymentTypeName with name $instanceName" + ] + d.instances[tName].servers.each { n, v -> + res += n + } + return res + } + def allServers(deploymentTypeName = null) { + if (deploymentTypeName == null) deploymentTypeName = serviceName + def d = deploymentType[deploymentTypeName] + def res = [] + if (d == null) return [ + "NO DeploymentType $deploymentTypeName" + ] + if (d.instances == null) return [ + "NO instances DeploymentType $deploymentTypeName" + ] + d.instances.each { n1, v1 -> + v1.servers.each { n, v -> + res += n + } + } + return res + } + + def eforc() { + def res = [:] + def list = [] + def d = [:] + deploymentType.each { n, v -> + d[n]=v + } + d['vm-all'] = [ + type : ['openstack-vm-type': networking ] + ] + d.each { n, v -> +// println "n=$n v=${p(v)}" + def x = [outbound:[:],inbound:[:]] + res[n] = x + def out + if (n.startsWith('vm-')) { + def vm = v.type['openstack-vm-type'] ?: [:] +// println "n=$n vm=$vm" + out = vm['application-outbound-security-rules'] ?: [:] + } + else { + out = v.type['application-outbound-security-rules'] ?: [:] + } +// println "n=$n out=$out" + out.each { n1, v1 -> +// println "n1=$n1 v1=$v1 dest=${destination2deployment(n1,v1)}" + if (v1['only-local']) return + x.outbound[n1] = [] + findEndpoints(n).each { from -> + destination2deployment(n1,v1).each { n2 -> +// println "n1=$n1 v1=$v1 endpoints=${findEndpoints(n2,v1)}" + findEndpoints(n2,v1).each { to -> + if (from == to) return + if (both_local(from,to)) return + def ip1 = findIps(n,from) + def ip2 = findIps(n2,to,v1) + def ports = v1.ports ?: ( v1.port ?: 'TBD') + def protocol = v1.protocol ?: 'tcp' +// println "out=$n1 from=$from to=$to $ip1 $ip2" + x.outbound[n1] += [ + from: "$n@$from($ip1)", + to: "$n2@$to($ip2) ${protocol}:$ports", + ] + ip1.each { y1 -> + def subnet1 = ip2subnet(y1,from) + ip2.each { y2 -> + def subnet2 = ip2subnet(y2,to) + list += [[n,"in",n1,n,n2,from,to,y1,y2,subnet1,subnet2,protocol,ports]] + } + } + } + } + } + } + def inn + if (n.startsWith('vm-')) { + def vm = v.type['openstack-vm-type'] ?: [:] +// println "n=$n vm=$vm" + inn = vm['application-inbound-security-rules'] ?: [:] + } + else { + inn = v.type['application-inbound-security-rules'] ?: [:] + } +// println "n=$n in=$in" + inn.each { n1, v1 -> +// println "n1=$n1 v1=$v1" + if (v1['only-local']) return + x.inbound[n1] = [] + destination2deployment(n1,v1).each { n2 -> + findEndpoints(n2,v1).each { from -> + findEndpoints(n).each { to -> + if (from == to) return + if (both_local(from,to)) return + def ip1 = findIps(n2,from,v1) + def ip2 = findIps(n,to) + def ports = v1.ports ?: [ ( v1.port ?: 'TBD') ] + def protocol = v1.protocol ?: 'tcp' + // println "in=$n1 from=$from to=$to $ip1 $ip2" + x.inbound[n1] += [ + from: "$n2@$from($ip1)", + to: "$n@$to($ip2) $protocol:$ports", + ] + ip1.each { y1 -> + def subnet1 = ip2subnet(y1,from) + ip2.each { y2 -> + def subnet2 = ip2subnet(y2,to) + list += [[n,"out",n1,n2,n,from,to,y1,y2,subnet1,subnet2,protocol,ports]] + } + } + } + } + } + } +// println p(x) +// println p(res) +// System.exit(3) + } + def res1 = [:] + res.each { n,v -> + if (v.inbound.size()==0) v.inbound=null + if (v.outbound.size()==0) v.outbound=null + if (v.inbound == null && v.outbound ==null) return + res1[n] = v + } + def m2 = [:] + list.each { l -> + def siteA = l[5] + def siteB = l[6] + def sA = l[9] + def sB = l[10] + def protocol = l[11] + def ports = l[12] + def k = "$siteA:$siteB:$sA:$sB:$protocol" + if (m2[k] == null) { + m2[k] = [ + siteA:siteA, + siteB:siteB, + subnetA:sA, + subnetB:sB, + protocol:protocol, + ports : [] + ] + } + ports.each { port -> + if ( ! m2[k].ports.contains(port)) + m2[k].ports.add(port) + } + } + def list2 = [] + m2.each { n, v -> + list2 += [ [ v.siteA, v.siteB, v.subnetA, v.subnetB, v.protocol, v.ports.join(":") ]] + } + return [m:res1,list:list,m2:m2,list2:list2] + } + def site2subnet = [:] + def ip2subnet(ip,site) { + if (tenants[site]== null) return ip + if (! IpUtils.isIpv4(ip) ) return ip + def subnets = tenants[site].subnets ?: [] + def res = null + subnets.each { subnet -> + if (res != null) return + String[] a = subnet.split("/") + def ip2 = a[0] + def len = a[1] + if (! IpUtils.isIpv4(ip2) || ! len.isInteger()) { + println "Bad subnet: $subnet at $site" + System.exit(2) + } + if (IpUtils.containedIn(IpUtils.toInetAddress(ip),IpUtils.toInetAddress(ip2),len.toInteger())) res = subnet + } + if (res != null) return res + if (site2subnet[site] == null) site2subnet[site] = [] as Set + site2subnet[site] += "${IpUtils.mask(IpUtils.toInetAddress(ip),29)}/29".substring(1) + return "$ip not in any $site subnets" + } + def both_local(from,to) { + if (tenants[from] == null || tenants[from].type != "opendcae-local-ntc") return false + if (tenants[to] == null || tenants[to].type != "opendcae-local-ntc") return false + return true + } + + def dns() { + def cnames = [:] + def arecords = [:] + def gss = [:] + def currentCnames = (env['dns'] ?: [:])['current-cnames'] ?: [:] + def currentGss = (env['dns'] ?: [:])['current-gss'] ?: [:] + def res = ['dns-arecords':arecords, 'dns-cnames':cnames, 'dns-gss':gss] + deploymentType.each { n, v -> + if (!n.startsWith('vm-')) return + v.instances.each { n1, v1 -> + v1.servers.each { n2, v2 -> + (v2['dns-cnames'] ?: []).each { d -> + if (cnames[d] == null) cnames[d] = [value :"", values:[],status:dnsLookup(d)] + cnames[d].values += v2['dns-name'] ?: "$n2 missing DNS" + cnames[d].value = currentCnames[d] ?: cnames[d].values[0] + } + (v2['dns-gss'] ?: []).each { d -> + if (gss[d] == null) gss[d] = [value :"", values:[],status:dnsLookup(d,false)] + gss[d].values += v2['dns-name'] ?: "$n2 missing DNS" + gss[d].value = currentGss[d] ?: gss[d].values[0] + } + (v2['dns-arecords'] ?: []).each { d -> + if (arecords[d] == null) arecords[d] = [values:[],status:dnsLookup(d)] + def dns = v2['dns-name'] ?: "$n2 missing DNS" + def ip = ipAssignments[n2] ?: "TBD - ip-assignment.yaml@$n2" + arecords[d].values += "$dns $ip" + } + } + } + } + deploymentType.each { n, v -> + if (!n.startsWith('vm-')) return + (v.type['dns-gss'] ?: []).each { gssType -> +// println "n=$n gssType=$gssType" + switch (gssType) { + case "gss": + def d = dnsName(n,null,'global') + cnames[d] = [value:dnsName(n,null,'gss'),status:dnsLookup(d,false)] + break; + case "gss-edge": + v.instances.each { n1, v1 -> +// println "n=$n gssType=$gssType n1=$n1 v1=$v1" + def v2 = tenants[n1] + def d = dnsName(n,v2,'site') + cnames[d] = [value:dnsName(n,v2,'gss-edge'),status:dnsLookup(d,false)] + } + break; + } + } + (v.type['dns-cnames'] ?: []).each { cnameType -> +// println "n=$n cnameType=$cnameType" + switch (cnameType) { + case "central-site": + def d = dnsName(n,null,'global') + cnames[d] = [values:[],status:dnsLookup(d,false)] + v.instances.each { n1, v1 -> + def v2 = tenants[n1] + if (v2.type != "opendcae-central") return + def d1 = dnsName(n,v2,'site') + if (cnames[d].value == null) cnames[d].value = d1 + cnames[d].values += d1 + } +// println "n=$n cnameType=$cnameType ${p(cnames[d])}" +// System.exit(3) + break; + } + } + (deploymentType[n].type['dns-service'] ?: [:]).each { n3, v3 -> + switch (v3['cluster-type']) { + case "gss" : + def d = dnsName(n,null,'global',n3) + cnames[d] = [value:dnsName(n,null,'gss',n3),status:dnsLookup(d)] + break + case "gss-edge" : + v.instances.each { n1, v1 -> + def v2 = tenants[n1] + def d = dnsName(n,v2,'site',n3) + cnames[d] = [value:dnsName(n,v2,'gss-edge',n3),status:dnsLookup(d)] + } +// println "n=$n n3=$n3 ${p(cnames)}" +// System.exit(3) + break + } + } + } + return res + } + + def dnsLookup(n,boolean showDetails = true) { + def x = "nslookup $n".execute().text + List res = [] + if (x.contains("server can't find")) return "Not registered" + def first = true + x.split('\n').each { line -> + if (line.startsWith("Address:")) { + if (first) { + first = false + return + } + def ip = line.substring(9) + res += "${ip2hostname(ip)} $ip" + } + } + return showDetails ? res.sort() : "Registered" + } + def ip2hostname(ip) { + def res = null + deploymentType.each { n, v -> + if (!n.startsWith('vm-')) return + v.instances.each { n1, v1 -> + v1.servers.each { n2, v2 -> + if (ip == v2.ip) res = n2 + } + } + } + return res ?: "Unknown server" + } + def ssl() { + def res = [:] + deploymentType.each { n, v -> + if (!n.startsWith('vm-')) return + v.instances.each { n1, v1 -> + v1.servers.each { n2, v2 -> + res[n2] = [ names: v2['dns-arecords']] + res[n2].names += v2['dns-cnames'] + res[n2].names += v2['dns-gss'] + File f = new File("$root/$release-$envName/certificates/${n2}.pkcs12") + if (!f.exists()) + res[n2].status = "No certificate" + else + res[n2].status = passwords[n2] ? "Certificate Exists" : "No Password" + } + } + } + return res + } + + def destination2deployment(n,extra) { + if (extra['destination-entity-type']) { + def res = [] + deploymentType.each { n1, v -> + if (n1.startsWith(extra['destination-entity-type'])) res += n1 + } + return res + } + return [ extra['destination-entity'] ?: n ] + } + + def findEndpoints(n, extra = [:]) { + if (n == "vm-all") { + return tenants.collect { n1, t -> n1 } + } + n = extra['destination-entity'] ?: n + if (deploymentType[n]) + return deploymentType[n].tenantNames + // external entity + return [ n ] + } + def findIps(String n, String t, extra = [:]) { + n = extra['destination-entity'] ?: n + if (n == "vm-all") { + def res = [] + deploymentType.each { n1, v1 -> + if (! n1.startsWith("vm-")) return + res += findIps(n1,t,extra) + } + return res + } + if (deploymentType[n] == null) { + // external entity + def v = ((env.networking ?: [:])['external-entities'] ?: [:])[n] + if (v == null) return ["TBD - env.yaml@networking.external-entities.$n" ] + if (v.ips != null) return v.ips + return v.collect {n1,v1 -> v1.ip} + } + def v = deploymentType[n].instances[t] +// println "n=$n v=${p(deploymentType[n].instances[t])}" + if (n.startsWith("vm-")) { +// println "n=$n v=${p(v.servers)}" + if (v==null) return [] + return v.servers.collect { n1,v1 -> v1.ip } + } +// println "n=$n v=${p(deploymentType.collect {n1,v1 -> n1})}" +// println p(dockers) + return findIps(v.hostService,v.hostServiceInstance) + } + def streams2bus() { + def res = [:] + def otherFeeds = [:] + streams.streams.each { n, v -> +// println "n=$n v=${object2json(v).toString(2)}" + def streamNames = [] + def location = [:] + def auth = [:] + switch (v['databus-type']) { + case "topic": + streamNames += "topic:$n" + auth["topic:$n"] = "aaf" + break + case "topic-public": + streamNames += "topic:$n" + auth["topic:$n"] = "none" + break + case "ecomp": + streamNames += "ecomp:$n" + break + case "local": + tenants.each { n1, v1 -> streamNames += "local:$n:$n1" } + break + case "local-ecomp": + streamNames += "ecomp:$n" + tenants.each { n1, v1 -> + streamNames += "local:$n:$n1" + otherFeeds["local:$n:$n1"] = [ "ecomp:$n" ] + } + break + default: println "Unknown databus type: ${v['databus-type']}" + } + streamNames.each { streamName -> + switch (v['stream-type']) { + case "data-router": + def stream = [ + '$class' : "org.openecomp.dcae.controller.core.stream.DatabusStreamFeed", + publishers : [:], + subscribers : [:], + feedDescription : v['description'], + asprClassification : v['aspr-classification'], + ] + if (v['databus-type'] == "local-ecomp") { + stream.publishers['forward'] = [:] + } + res[streamName] = stream + v.publishers.each { n1, v1 -> + switch (v1['publisher-type']) { + default: + if (streamName.startsWith("local")) { + def loc = streamName.split(':')[2] + def n2 = "$n1:$loc" + stream.publishers[n2] = [ + localStreamId : v1['local-name'], + dcaeLocationName: loc + ] + } + if (streamName.startsWith("ecomp")) { + locationTypes['opendcae-central'].tenantNames.each { n3 -> +// System.err.println "n1=$n1 n3=$n3 ${deploymentType[n1].instances[n3]}" + if (deploymentType[n1].instances[n3] == null || deploymentType[n1].instances[n3].size() == 0) return + def n2 = "$n1:$n3" +// System.err.println "n2=$n2" + stream.publishers[n2] = [ localStreamId : v1['local-name'] ] + } + } + } + } + v.subscribers.each { n1, v1 -> + switch (v1['subscriber-type']) { + case "external": + if (streamName.startsWith("ecomp")) { + def n2 = "external:$n1" + stream.subscribers[n2] = [:] + stream.subscribers[n2].deliveryURL = substitute(v1['subscriber-url'],[:],null,null) + stream.subscribers[n2].username = substitute(v1['subscriber-user'],[:],null,null) + stream.subscribers[n2].userpwd = substitute(v1['subscriber-password'],[:],null,null) + } + break + default: + if (streamName.startsWith("local")) { + def loc = streamName.split(':')[2] + def n2 = "$n1:${loc}" + stream.subscribers[n2] = [ + dcaeLocationName : loc, + localStreamId : v1['local-name'], + deliveryURL : substitute(v1['subscriber-url'],[:],n2,loc) + ] + } + } + } + if (otherFeeds[streamName]) { + // Add additional subscriber for forwarding to that feed + otherFeeds[streamName].each { n3 -> + def n2 = "forward:$n3" + stream.subscribers[n2] = [ + otherFeedName : n3 + ] + } + } + break + case "message-router": + def stream = [ + '$class' : "org.openecomp.dcae.controller.core.stream.DatabusStreamTopic", + clients : [:], + topicDescription : v['description'], + asprClassification : v['aspr-classification'], + authenticationMethod : auth[streamName], + ] + res[streamName] = stream + tenants.each { loc, w1 -> + v.publishers.each { n1, v1 -> + def n2 = "$n1:${loc}" + stream.clients[n2] = [ + localStreamId : v1['local-name'], + clientRole : v1['role'], + username : v1['user'] ? substitute(v1['user'],[:],n1,loc) : null, + userpwd : v1['password'] ? substitute(v1['password'],[:],n1,loc) : null, + action : ["pub","view"], + dcaeLocationName: loc, + ] + } + v.subscribers.each { n1, v1 -> + def n2 = "$n1:${loc}" + stream.clients[n2] = [ + localStreamId : v1['local-name'], + clientRole : v1['role'], + username : v1['user'] ? substitute(v1['user'],[:],n1,loc) : null, + userpwd : v1['password'] ? substitute(v1['password'],[:],n1,loc) : null, + action : ["sub","view"], + dcaeLocationName: loc, + ] + } + } + break + } + } + } +// println "res=${object2json(res).toString(2)}" + return res + } + def substitute(o, env1, s, i) { + SimpleTemplateEngine engine = new SimpleTemplateEngine() + String res = o + env1.dcae = this + env1.passwords = env.passwords + init(s, i) + switch (o) { + case List: + def l = [] + o.each { e -> + l += substitute(e, env1, s, i) + } + return l + case Map: + def m = [:] + o.each { n,v -> + m[n] = substitute(v, env1, s, i) + } + return m + } + try { + res = engine.createTemplate("$o").make(env1) + if (res.startsWith("_LIST_:") && res.endsWith(":_LIST_")) { + res = res.replace("_LIST_:","").replace(":_LIST_","") + JSONArray a = new JSONArray(res) + return json2object(a) + } + } catch (e) { + System.out.println "$o" + System.err.println "ERROR $e" + System.exit(2) + } + return res + } + + def orchOld(format = null) { + def res = [ + central : [ + postgresURL: "jdbc:postgresql://${serviceFqdn('vm-postgres-sql')}:5432/XXX", + postgresUser: "YYYYY", + postgresPassword: "\${env.passwords.postgres.XXX.YYYYY}", + dmaapDcaeLocation: "????", + dmaapMRHosts: allServersFqdn("vm-databus-mr-node") + ], + locations:[:] + ] + tenants.each { n, tenant -> + res[n] = [ + locationType: tenant.type, + dmaapDcaeLocation : n, + dmaapMrService : dnsName("vm-databus-mr-node",tenant,"site"), + aaiLocationName: tenant['aai-location-name'] ?: "TBD tenant.yaml#${n}.aai-location-name", + clli: tenant['clli'] ?: "TBD tenant.yaml#${n}.clli", + docker_host_service : " ??? PLACEHOLDER_FOR_DOCKER_HOST_SERVICE_INSTANCE_THIS_EDGE", + docker_registry : "??? PLACEHOLDER_FOR_DOCKER_REGISTRY_HOST_PORT_THIS_EDGE", + ] + } + + /* + "locations" : { + "central" : { + "postgresURL? : PLACEHOLDER_FOR_POSTGRES_URL, + ?postgresUser? : PLACEHOLDER_FOR_POSTGRES_USER, + ?postgresPassword?: PLACEHOLDER_FOR_POSTGRESS_PASSWORD, + ?dmaapDcaeLocation?: PLACEHOLDER_FOR_DMAAP_DCAE_LOCATION_CENTRAL, + ?dmaapMRHosts? : PLACEHOLDER_FOR_DMAAP_MR_HOSTS_CENTRAL + }, + "PLACEHOLDER_FOR_CLLI_CODE_FOR_EDGE_LOCATION_1" : { + "cdap_cluster_service" : PLACEHOLDER_FOR_CDAP_CLUSTER_SERVICE_INSTANCE_THIS_EDGE, + ?docker_host_service? : PLACEHOLDER_FOR_DOCKER_HOST_SERVICE_INSTANCE_THIS_EDGE, + ?docker_registry? : PLACEHOLDER_FOR_DOCKER_REGISTRY_HOST_PORT_THIS_EDGE, + "dmaapDcaeLocation" : PLACEHOLDER_FOR_DMAAP_DCAE_LOCATION_THIS_EDGE, + ?dmaapMRHosts? : [ PLACEHOLDER_FOR_LIST_OF_MR_HOSTS_THIS_EDGE + }, + "PLACEHOLDER_FOR_CLLI_CODE_FOR_EDGE_LOCATION_2" : { + Similar to above + } + /* Repeat for all edge locations in the environment + * Could use a looping construct if your template language has one + */ + switch (format) { + case "base64" : return "'" + object2json(res).toString(2).bytes.encodeBase64(true) + "'" + default: return res + } + } + + def orch(format = null) { + def postgres = release == "1607" ? 'vm-postgres-sql' : 'vm-postgresql-central' + def res = [ + central : [ + postgresHost: "${serviceFqdn(postgres)}", + postgresDTIPassword: "${password('postgres.dti.viewer')}" + ] + ] + tenants.each { n, tenant -> + res[tenant['aai-location-name'] ?: "TBD - tenants.yaml@${n}.aai-location-name"] = [ + aaiLocationName:tenant['aai-location-name'], + locationType: tenant.type, + dcaeLocationName : n, + dmaapMrService : dnsName("vm-databus-mr-node",tenant,"site"), + dockerHost1: serversFqdn("vm-docker-host-1", n)[0], + dockerHost2: serversFqdn("vm-docker-host-2", n)[0], + dockerHost3: serversFqdn("vm-docker-host-3", n)[0], + ] + } + + switch (format) { + case "base64" : return "'${object2json(res).toString(2).bytes.encodeBase64(true)}'".replace('\n','\n\n') + case "json" : return object2json(res).toString(2) + default: return res + } + } + + + def controllerCluster() { + def res = [servers:[:]] + def ee = (env['vm-templates'] ?: [:])['vm-controller'] ?: [:] + def roles = ee['server-roles'] ?: [:] + deploymentType['vm-controller'].instances.each { n, v -> + v.servers.each { n1, v1 -> + + println "$n1 ${roles[n1] ?: 'UNKNOWN'}" + res.servers[n1] = [ + role : roles[n1] ?: 'UNKNOWN', + data : [ + state :[:] + ] + ] + } + } + return res + } + def chefTopo() { + def topo = "$release-$envName" + def chef = [ + id: topo, + name: topo, + chef_environment: "_default", + tags: [], + strategy: "via_cookbook", + strategy_data: [ cookbook: topo, filename: "topology"], + nodes : [] + ] + deploymentType.each { n, v -> + if (! n.startsWith("vm-")) return + println "$n" + v.instances.each { n1, v1 -> + v1.servers.each { n2, v2 -> +// println "$n $n1 $n2 $v2" + chef.nodes += [ + name : v2['dns-name'], + tags : [], + chef_environment: "_default", + run_list: [ "recipe[$release]","recipe[dcae-nagios]" ], + normal: [ tags: [] ] + ] + } + } + } + return chef + } + + def flavor(n,t) { + def tt = tenants[t] + if (tt == null) return "NONE $n $t" + def v = deploymentType[n] + if (!aEnv()) + return v['vm-type'].replace('vm-','dcae-') + def vmType = v.type['openstack-vm-type'] ?: [:] + def e = (env['vm-templates'] ?: [:])[v['vm-type']] ?:[:] + def d = e['openstack-flavor-name'] ? e['openstack-flavor-name'] : vmType['openstack-flavor-name'] + switch (d) { + case String: return d + case Map: return d[tt['type']] + } + } + + def showFlavors() { + def res = [:] + deploymentType.each { n, v -> + if (!n.startsWith('vm')) return + res[n] = [:] + v.instances.each { n1, v1 -> + res[n][n1] = flavor(n,n1) + } + } + return res + } + def list(x) { + return "_LIST_:${object2json(x)}:_LIST_" + } + + static main(args) { + + } +} diff --git a/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java new file mode 100644 index 0000000..7b30cf5 --- /dev/null +++ b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/DocUtils.java @@ -0,0 +1,309 @@ + +/*- + * ============LICENSE_START========================================== + * OPENECOMP - DCAE + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + */ + +package org.openecomp.dcae.controller.operation.utils; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +import org.openecomp.ncomp.utils.maps.CounterMap; +import org.openecomp.ncomp.webservice.utils.JsonUtils; + +import static org.openecomp.ncomp.sirius.manager.console.Utils.*; + +public class DocUtils { + protected String env; + protected static String root = "/home/vagrant/git/org.openecomp.dcae.controller.operation"; + protected String release; + +// public DocUtils(String release, String env) { +// super(); +// this.release = release; +// this.env = env; +// } + + public DocUtils(String release, String env, String root1, String root2) { + super(); + this.release = release; + this.env = env; + if (root1 != null) root = root1; + } + + + @SuppressWarnings("rawtypes") + public Map tenants() { + String fileName = release + "-" + env + "/tenants.yaml"; + Map o = (Map) file2object(fileName); + return o; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public Map vms() { + LinkedHashMap<String,LinkedHashMap<String,Object>> s = new LinkedHashMap<String,LinkedHashMap<String,Object>>(); + LinkedHashMap<String,LinkedHashMap<String,Object>> ss = new LinkedHashMap<String,LinkedHashMap<String,Object>>(); + CounterMap<String> index = new CounterMap<String>(); + Map<String,Object> locationTypes = locationTypes(); + Map<String,Object> vmTypes = (Map<String, Object>) types("vm"); + Map<String,Object> tenants = tenants(); + for (String t : locationTypes.keySet()) { + for (String tenantKey : tenants.keySet()) { + Map<String,Object> tenant = (Map<String, Object>) tenants.get(tenantKey); + if (! t.equals(tenant.get("type"))) continue; + Map<String,Object> locationType = (Map<String, Object>) locationTypes.get(t); + Map<String,Object> vmDeployments = (Map<String, Object>) locationType.get("vm-deployments"); + for (String vmDeploymentKey : vmDeployments.keySet()) { + Map<String,Object> vmDeployment = (Map<String, Object>) vmDeployments.get(vmDeploymentKey); + Map<String,Object> vmType = (Map<String, Object>) vmTypes.get(vmDeployment.get("vm-type")); + if (s.get(vmDeploymentKey) == null) { + LinkedHashMap<String,Object> s1 = new LinkedHashMap<String,Object>(); + s.put(vmDeploymentKey, s1); + s1.put("vm-type", vmDeployment.get("vm-type")); + s1.put("release", vmDeployment.get("release")); + s1.put("milestone", vmDeployment.get("milestone")); + boolean useController = (Boolean) get(vmType,"lifecycle-management", "use-dcae-manager",new Boolean(false)); + s1.put("use-controller", useController); + LinkedHashMap<String,Object> s2 = new LinkedHashMap<String,Object>(); + s1.put("locations", s2); + ss.put(vmDeploymentKey, s2); + } +// System.err.println (" XXx:" + t + " " + vmDeploymentKey + " " + vmTypes.keySet()+ " " + vmDeployment.get("vm-type")); +// if (vmType.containsKey("error")) continue; + if (vmType == null) { + + } + Integer numVms = (Integer) vmType.get("number-vms-per-location"); + if (numVms == null) numVms = 2; +// System.err.println (" XXx:" + t + " " + vmDeploymentKey + " " + vmType.keySet()+ " " + tenant.keySet()); + LinkedHashMap<String,Object> s1 = new LinkedHashMap<String,Object>(); + LinkedHashMap<String,Object> sss = new LinkedHashMap<String,Object>(); + s1.put("dcae-service", vmDeploymentKey); + s1.put("dcae-service-instance", tenantKey); + s1.put("dcae-container", "/locations/" + tenantKey); + LinkedHashMap<String,Object> s2 = new LinkedHashMap<String,Object>(); + s1.put("vms", s2); + String shortName = short_name(vmDeployment.get("vm-type")); + String indexKey = tenant.get("name-short") + ":" + shortName; + numVms = (Integer) vmType.get("number-vms-per-location"); + if (numVms == null) numVms = 2; + for (int i = 0 ; i < numVms; i++) { + String name = vmname(tenant,vmType,index.get(indexKey)); +// System.err.println ("new VM: " + name + " " + index.get(indexKey)); + index.increment(indexKey); + LinkedHashMap<String,Object> s3 = new LinkedHashMap<String,Object>(); + s3.put("dns-name", name + "." + tenant.get("dns-surfix")); + s2.put(name, s3); + } + ss.get(vmDeploymentKey).put(vmDeploymentKey+"-"+tenantKey, s1); + } + } + } + return s; + } + + private String short_name(Object object) { + String fileName = release + "/vm-templates/" + object + ".yaml"; + Map o = (Map) file2object(fileName); + return (String) o.get("short-name"); + } + + Map<String,Object> locationTypes() { + String fileName = release + "/location-types.yaml"; + @SuppressWarnings("unchecked") + Map<String,Object> locationTypes = (Map) file2object(fileName); + return locationTypes; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public Map dockers() { + LinkedHashMap<String,Object> s = new LinkedHashMap<String,Object>(); + Map<String,Object> locationTypes = locationTypes(); + Map<String,Object> dockerTypes = (Map<String, Object>) types("docker"); + Map<String,Object> tenants = tenants(); + for (String t : locationTypes.keySet()) { + for (String tenantKey : tenants.keySet()) { + Map<String,Object> tenant = (Map<String, Object>) tenants.get(tenantKey); + if (! t.equals(tenant.get("type"))) continue; + Map<String,Object> locationType = (Map<String, Object>) locationTypes.get(t); + Map<String,Object> dockerDeployments = (Map<String, Object>) locationType.get("docker-deployments"); + if (dockerDeployments == null) continue; + for (String dockerDeploymentKey : dockerDeployments.keySet()) { + Map<String,Object> dockerDeployment = (Map<String, Object>) dockerDeployments.get(dockerDeploymentKey); + Map<String,Object> dockerType = (Map<String, Object>) dockerTypes.get(dockerDeployment.get("docker-type")); +// if (dockerType.containsKey("error")) continue; +// System.err.println (" XXx:" + t + " " + vmDeploymentKey + " " + vmType.keySet()+ " " + container.keySet()); + LinkedHashMap<String,Object> s1 = new LinkedHashMap<String,Object>(); + s1.put("docker-type", dockerDeployment.get("docker-type")); + s1.put("release", dockerDeployment.get("release")); + s1.put("milestone", dockerDeployment.get("milestone")); + s1.put("use-controller", true); + s1.put("dcae-service", dockerDeploymentKey); + s1.put("dcae-service-instance", tenantKey); + String container = (String) dockerDeployment.get("container"); +// container = container.replace("${location-name}", tenantKey); + s1.put("dcae-container", container); + s1.put("location-type", t); + s.put(dockerDeploymentKey, s1); + } + } + } + return s; + } + + private Map<String, Object> containers() { + // TODO Auto-generated method stub + return null; + } + + private Object get(Map<String, Object> m, String k1, String k2, Object def) { + Object o = m.get(k1); + if (o instanceof Map<?, ?>) { + @SuppressWarnings("unchecked") + Map<String, Object> m1 = (Map<String, Object>) o; + return get(m1,k2,def); + } + return def; + } + + private Object get(Map<String, Object> m, String k1, Object def) { + Object o = m.get(k1); + return o == null ? def : o; + } + + static LinkedHashMap<String,String> env2char = new LinkedHashMap<String,String>(); + + static { + env2char.put("DEV", "zldc"); + env2char.put("FTL", "zldc"); + env2char.put("FTL2", "zldc"); + env2char.put("IST", "zltc"); + env2char.put("ISTFTL", "zltc"); + env2char.put("ETE", "zlsc"); + env2char.put("PROD", "zlpc"); + } + + + private String vmname(Map<String, Object> tenant, Map<String, Object> vmType, int i) { + String env1 = (String) tenant.get("environment-type"); + if (env1 == null) env1 = env; + String s = env2char.get(env1); + if (s == null) s = env2char.get("DEV"); + return s + tenant.get("name-short") + vmType.get("short-name") + String.format("%02d", i); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public Map types(String dType) { + LinkedHashMap<String,Object> s = new LinkedHashMap<String,Object>(); + Map<String,Object> locationTypes = locationTypes(); + for (String locationType : locationTypes.keySet()) { + Map<String,Object> o1 = (Map<String, Object>) locationTypes.get(locationType); + Map<String,Object> o2 = (Map<String, Object>) o1.get(dType + "-deployments"); + if (o2 == null) continue; + for (String type : o2.keySet()) { + Map<String,Object> o3 = (Map<String, Object>) o2.get(type); + if (o3.containsKey(dType + "-type")) + type = (String) o3.get(dType + "-type"); + if (s.containsKey(type)) continue; + String path = release + "/" + dType + "-templates/" + type + ".yaml"; + s.put(type,file2object(path)); + } + } + return s; + } + + protected static Object file2object(String path) { + return file2object(path, true); + } + protected static Object file2object(String path, boolean required) { +// System.err.println (root + "/" + path); + try { + if (path.endsWith(".json")) { +// System.err.println (root + "/" + path); + return json2object(JsonUtils.file2json(root + "/" + path)); + } + else { + Yaml yaml = new Yaml(); + String s = new String(Files.readAllBytes(Paths.get(root + "/" + path)), StandardCharsets.UTF_8); + Object o = yaml.load(s); + if (o != null) return o; + } + } catch (org.yaml.snakeyaml.parser.ParserException e) { + System.err.println("YAML exception: " + path + " " + e); + System.exit(2); + } catch (Exception e) { + System.err.println("exception: " + path + " " + e.getClass()); + if (required) + System.err.println("exception: " + path + " " + e); + else { +// System.err.println("exception: " + path + " " + e); + return null; + } + } + HashMap<String, String> m = new HashMap<String, String>(); + m.put("error", "No information about: " + path); + return m; + } + + private static String object2yaml(Object object, List<String> l) { + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + options.setSplitLines(true); + options.setWidth(100000); + Yaml y = new Yaml(options); + Object o = object2json(object); + if (o instanceof JSONArray) { + JSONObject j = new JSONObject(); + j.put("ARRAY", object); + o = j; + } + JSONObject json = (JSONObject) o; + if (l != null) { + for (String s : l) { + if (json.has(s)) + json.remove(s); + } + } + // the replace is due to JSONObject changes "</" to "<\/" + Object data = y.load(json.toString().replace("<\\/", "</")); + return y.dump(data); + } + + public static void main(String[] args) { + DocUtils d = new DocUtils("1607", "ISTFLT",null,null); +// System.err.println(object2yaml(d.tenants().get("local-willows"),null)); +// System.err.println(object2yaml(d.types("vm"),null)); +// System.err.println(object2yaml(d.types("docker"),null)); +// System.err.println(object2yaml(d.types("cdap"),null)); +// System.err.println(object2yaml(d.vms(),null)); + System.err.println(object2yaml(d.dockers(),null)); + } + +} diff --git a/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/GenControllerConfiguration.groovy b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/GenControllerConfiguration.groovy new file mode 100644 index 0000000..38f48b0 --- /dev/null +++ b/dcae-controller-core/dcae-controller-operation-utils/src/main/java/org/openecomp/dcae/controller/operation/utils/GenControllerConfiguration.groovy @@ -0,0 +1,756 @@ + +/*- + * ============LICENSE_START========================================== + * OPENECOMP - DCAE + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + */ + +package org.openecomp.dcae.controller.operation.utils + +import groovy.json.JsonOutput; +import groovy.text.SimpleTemplateEngine + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +import org.codehaus.groovy.ast.stmt.CatchStatement; +import groovy.io.FileType +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject +import org.json.JSONArray +import org.json.JSONObject; + +import org.openecomp.dcae.controller.core.service.DcaeLocation; +import org.openecomp.dcae.controller.core.service.ServiceFactory; +import org.openecomp.dcae.controller.platform.controller.ControllerFactory; +import org.openecomp.dcae.controller.platform.controller.DcaePlatformController; +import org.openecomp.ncomp.openstack.OpenstackFactory; +import org.openecomp.ncomp.openstack.location.LocationFactory; +import org.openecomp.ncomp.openstack.location.LocationPackage; +import org.openecomp.ncomp.openstack.location.OpenStackLocation +import org.openecomp.ncomp.openstack.location.OpenStackProject; +import org.openecomp.ncomp.openstack.util.OpenstackAdapterFactory; +import org.openecomp.ncomp.sirius.manager.ManagementServer; +import org.openecomp.ncomp.utils.StringUtil; +import org.openecomp.ncomp.webservice.utils.FileUtils; +import org.openecomp.ncomp.sirius.manager.console.Utils + +import static org.openecomp.ncomp.sirius.manager.console.Utils.* + +class GenControllerConfiguration extends DocUtils { + + String destinationRoot + ManagementServer server + DcaePlatformController o + DcaePlatformConfiguration platformConfig + String destRoot + String release + String env + + + public GenControllerConfiguration(String release, String env, String root1, String root2) { + super(release,env,root1,root2); + this.release = release + this.env = env + destinationRoot = (root2 ?: root) + "/" + release + "-" + env + "/controller-gen" + destRoot = root2 ?: root + File d = new File(destinationRoot) + server = new ManagementServer() + o = ControllerFactory.eINSTANCE.createDcaePlatformController() + platformConfig = new DcaePlatformConfiguration(release,env,root1,root2) + server.setObject(o) + ["controller-gen","chef-repo-gen","cloudify-gen","general-gen"].each { dd -> + d = new File("${root2 ?: root}/$release-$env/$dd") + if (d.exists()) + FileUtils.deleteDirectory(d) + } + println "destinationRoot=$destinationRoot" + } + + void copyFile(File source, File destination, boolean doSub = false) { + destination.getParentFile().mkdirs() + if (doSub && source.bytes == source.text.bytes) { + // TEXT file + destination << substitute(source.text,[:],"???","???") + } + else + destination.bytes = source.bytes + } + + void copyDirectory(File sourceDir, File destDir, boolean doSub = false) throws IOException { + if (!destDir.exists()) { + destDir.mkdirs(); + } + for (File f : sourceDir.listFiles()) { + File dest = new File(destDir, f.getName()); + if (f.isDirectory()) { + copyDirectory(f, dest, doSub); + continue; + } + if (f.isFile()) { + copyFile(f, dest, doSub); + } + } + } + void copyFileFromDirectory(File sourceDir, File destDir) throws IOException { + if (!destDir.exists()) { + destDir.mkdirs(); + } + for (File f : sourceDir.listFiles()) { + File dest = new File(destDir, f.getName()); + if (f.isDirectory()) { + copyDirectory(f, dest); + continue; + } + if (f.isFile()) { + copyFile(f, dest); + } + } + } + + def void createChefConfiguration() { + save2("/topologies/${release}-${env}", platformConfig.chefTopo(), "chef") + if (release == "1607") return + platformConfig.deploymentType.each { n, v -> + if (v.type == null || ! n.startsWith("vm-")) return + def ooo = v.type['vm-setup'] + def oo = [ + 'nagios-configuration' : ooo['nagios-configuration'], + 'splunk-configuration' : ooo['splunk-configuration'], + 'configuration-files':[:] + ] + def xx = oo['configuration-files'] + v.instances.each { i, instance -> + def n2 = v['vm-type'] + [ + [dirname : "$root/$release/vm-config/$n2", nameType : "a"], + [dirname : "$root/$release/vm-config-chef/$n2", nameType : "a"], + [dirname : "$root/$release/other-config/$n2", nameType : "b"], + [dirname : "$root/$release/monitoring-config", nameType: "c"] + ].each { v3 -> + def dir = new File(v3.dirname) + if (dir.exists()) { + recurseFiles(dir).each { File file -> + def nn + switch (v3.nameType) { + case "a": nn = "$n2-$file.name"; break; + case "b": nn = "$file.name"; break; + case "c": nn = "monitoring-agent-$file.name"; break; + } + if (nn.endsWith(".base64")) { + nn = nn.replace(".base64","") + xx[nn] = [ destination: "/var/config/DCAE/chef/$nn", encoding:"base64", content: file.text] + } + else + xx[nn] = [ destination: "/var/config/DCAE/chef/$nn", content: file.text] + } + } + } + def o = [] + String str + str = object2json(oo) + str = DocUtils.object2yaml(oo,[]) +// println str +// str = str.replace("\\n", "\\\\n").replace("\\\\", "\\") +// println str +// def ENV = ((envMapGlobal['vm-templates'] ?: [:])[n2]) ?: [:] +// println "ENV $n2 $ENV" +// str = substitute(str,ENV,n,i) +// f.getParentFile().mkdirs() +// println str +// f << str +// System.exit(0) + instance.servers.each { n3, v3 -> + def ENV = ((envMapGlobal['vm-templates'] ?: [:])[n2]) ?: [:] +// println "ENV $n2 $ENV" + platformConfig.vmKey = certificatePassword(n3) + platformConfig.vmName = n3 +// println "n3=$n3 v3=$v3 vmKey=$platformConfig.vmKey" +// System.exit(0) + def str1 = substitute(str,ENV,n,i) + saveYaml("files/${n3}",str1,"chef") + platformConfig.vmKey = null + } + } + } + } + + def recurseFiles(File dir) { + def l = [] + dir.eachFileRecurse (FileType.FILES) { file -> l += file } + return l.sort { it } + } + + def void createConfiguration() { + def envMap = file2object("$release-$env/env.yaml", false) + save("/configuration", new JSONObject()) + File dir = new File(root + "/" + release + "/controller-template") + File dir2 = new File(destinationRoot + "/resources") + File dir3 = new File(destinationRoot + "/resources/descriptors") + copyDirectory(dir, dir2); + (platformConfig.env['users'] ?: [:]).each { n, v -> + save("/users/$n",v) + } + ["vm","docker","cdap","other"].each { t -> + File dir4 = new File("$root/$release/${t}-descriptors") + dir4.listFiles().each { f -> + copyFile(f, new File(dir3,f.name)); + } + } + File config1 = new File(root + "/" + release + "/vm-config/vm-controller") + File config2 = new File(destinationRoot + "/config") + copyDirectory(config1, config2, true); + File config11 = new File(root + "/" + release + "-" + env + "/config") + File config21 = new File(destinationRoot + "/config") + copyDirectory(config11, config21); + File cert1 = new File(root + "/" + release + "-" + env + "/certificates") + File cert2 = new File(destinationRoot + "/config") + copyDirectory(cert1, cert2); + // TODO remove this old stuff +// println platformConfig.types.collect { String n, v -> n } + platformConfig.types.each { String n, v -> + if (!n.startsWith("vm")) return + if (!platformConfig.types[n]['openstack-vm-type']) { + System.err.println "No vm type for $n" + return + } + def n1 = n.replace("vm-", "dcae-") + save("/openstack/vmTypes/$n1",platformConfig.genVmType(n)) + } + def chef = file2object("$release-$env/chef.yaml", false) + def maven = file2object("$release-$env/maven.yaml", false) + def ssl = file2object("$release-$env/ssl.yaml", false) + platformConfig.deploymentType.each { n, v -> + if (v.type == null || ! n.startsWith("vm-")) return + if (v.type['vm-setup'] == null) v.type['vm-setup'] = ['configuration-files' :[:]] + def oo = v.type['vm-setup'] + if (oo['configuration-files'] == null) oo['configuration-files'] = [:] + def xx = oo['configuration-files'] + v.instances.each { i, instance -> + File f = new File("$destinationRoot/config/instances/$n/${i}.yaml") +// println platformConfig.p(oo) + if (v.type['openstack-vm-type'] && v.type['openstack-vm-type']['disk-partitions']) { + xx.mount_config = [ + destination: "/tmp/dcae-mount.conf", + content: DocUtils.object2yaml(v.type['openstack-vm-type']['disk-partitions'],[]) + ] + } + if (chef) { + chef['pem-files'].each { n1, v1 -> + xx["chef-$n1"] = v1 + } + } + if (maven && maven.settings) { + xx["maven-settings"] = maven.settings + } + if (ssl && ssl.certificates) { + ssl.certificates.each { n4, v4 -> + xx[n4] = v4 + } + } + def aa = file2object("$release-$env/vm-instances/${n}.yaml", false) ?: [:] + def aaa = aa['configuration-files'] ?: [:] + aaa.each { n4, v4 -> + xx[n4] = v4 + } + def n2 = v['vm-type'] + def vmConfigDir = new File("$root/$release/vm-config/$n2") + if (vmConfigDir.exists()) { + recurseFiles(vmConfigDir).each { file -> + def nn = "$n2-$file.name" + if (nn.endsWith(".base64")) { + nn = nn.replace(".base64","") + xx[nn] = [ destination: "/tmp/$nn", encoding:"base64", content: file.text] + } + else + xx[nn] = [ destination: "/tmp/$nn", content: file.text] + } + } + def vmConfigDir2 = new File("$root/$release-$env/vm-config/$n2") + System.err.println "XXXXX: $vmConfigDir2" + if (vmConfigDir2.exists()) { + System.err.println "XXXXX: $vmConfigDir2" + recurseFiles(vmConfigDir2).each { file -> + System.err.println "XXXXX: $file" + def nn = "$n2-$file.name" + if (nn.endsWith(".base64")) { + nn = nn.replace(".base64","") + xx[nn] = [ destination: "/tmp/$nn", encoding:"base64", content: file.text] + } + else + xx[nn] = [ destination: "/tmp/$nn", content: file.text] + } + } + def otherConfigDir = new File("$root/$release/other-config/$n2") + if (otherConfigDir.exists()) { + recurseFiles(otherConfigDir).each { file -> + def nn = "$file.name" + xx[nn] = [ + destination: "/tmp/$nn", + content: file.text + ] + } + } + recurseFiles(new File("$root/$release/monitoring-config")).each { file -> + def nn = "monitoring-agent-$file.name" + xx[nn] = [ + destination: "/tmp/$nn", + content: file.text + ] + } + def o = [] + String str + str = object2json(oo) + str = DocUtils.object2yaml(oo,[]) +// println str + str = str.replace("\\n", "\\\\n").replace("\\\\", "\\") +// println str + def ENV = ((envMapGlobal['vm-templates'] ?: [:])[n2]) ?: [:] +// println "ENV $n2 $ENV" + str = substitute(str,ENV,n,i) + f.getParentFile().mkdirs() +// println str + f << str +// System.exit(0) + if (release == "1607") { + instance.servers.each { n3, v3 -> + saveYaml("files/${n3}",str,"chef") + } + } + } + } + save("/databus",[streams : platformConfig.streams2bus()]) + save("/cluster",platformConfig.controllerCluster()) + } + + void createLocations() { + Map tenants = tenants(); + o.openstack = OpenstackFactory.eINSTANCE.createOpenStackController() + save("/openstack", new JSONObject()) + tenants.each { n, tenant -> + DcaeLocation loc = ServiceFactory.eINSTANCE.createDcaeLocation() + o.getLocations().add(loc) + loc.name = n + loc.locationType = tenant.type + if (tenant.openstack != null) { + OpenStackLocation l = genEcore(LocationPackage.eINSTANCE.openStackLocation, tenant.openstack) + l.name = n + o.openstack.locations += l + OpenStackProject p = genEcore(LocationPackage.eINSTANCE.openStackProject, tenant.openstack.tenant) + l.projects += p + loc.openstackProject = p + l.allowCreateFlavor = (! platformConfig.aEnv() ) + save("/openstack/locations/$n",l) + } + save("/locations/$n",loc) + } + } + + def throw1(s) { + throw new RuntimeException(s) + } + + void createServiceInstances() { + def envMap = file2object("$release-$env/env.yaml", false) ?: ['vm-templates':[:]] + vms().each { n1, vmDeployment -> +// System.err.println "$n1 $vmDeployment" + def t = vmDeployment['vm-type'] + vmDeployment.locations.each { n , vm -> +// System.err.println "$n $vm" + def m = [:] + def s = vm['dcae-service'] + def i = vm['dcae-service-instance'] + def descriptor = file2object("$release/vm-descriptors/${s}.json", false) + if (descriptor == null) { + System.err.println "No descriptor for $s" + return + } + def template = file2object("$release/vm-service-instance-templates/${n}.yaml", false) + def vmTemplate = file2object("$release/vm-templates/${t}.yaml") +// System.err.println "$n descript=$descriptor" +// System.err.println "$n template=$template" + m['$class'] = "${descriptor.servicePackage}.${descriptor.serviceInstanceClass}" + m.vmType = [ '$ref' : "/openstack/vmTypes/${t.replace('vm-','dcae-')}" ] + m.location = [ '$ref' : vm['dcae-container'] ] + m.numberOfServers = vmTemplate['number-vms-per-location'] + m.iedsAdmin = (envMap['ieds'] ?: [:])['admin-user'] ?: "TBD - env.yaml@ieds.admin-user" + m.chefUser = (envMap['chef'] ?: [:])['user'] ?: "TBD - env.yaml@chef.user" + m.chefOrg = (envMap['chef'] ?: [:])['org'] ?: "TBD - env.yaml@chef.group" + m.chefTopology = "$release-$env" + m.openstackFlavor = platformConfig.flavor(s,i) + if ((vmTemplate['dns-arecords'] ?: []).contains('site')) { + platformConfig.init(s,i) + m.serviceFqdn = platformConfig.siteServiceFqdn() + } + ["users","adminUsers"].each { xx -> + def xx1 = envMap['vm-templates'] ?: [:] + if (xx1 == null) { throw1 "No vm-templates in $release-$env/env.yaml" } + def xx2 = xx1[t] + if (xx2 == null) { xx2 = xx1['default'] ?: [users:[],adminUsers:[]] } + if (xx2 == null) { throw1 "No vm-template for $t in $release-$env/env.yaml" } + def xx3 = xx.replace("U", "-u") +// System.err.println "$xx1 $t" + m[xx] = [] + xx2[xx3].each { u -> + m[xx] += [ '$ref' : "/users/$u" ] + } + } + m.servers = [:] + vm.vms.each { n2, vm2 -> + m.servers[n2] = [ + certificatePassword: certificatePassword(n2), + networks : [ public : [ + dnsName:vm2['dns-name'], + ip:ipAssignment(n2), + ]] + ] + } +// System.err.println "$n m=$m" + m.applicationIds = vmTemplate['application-ids'] ?: ["dcae"] + m.adminId = vmTemplate['admin-id'] ?: "attcloud" + def iConf = vmTemplate['controller-instance-configuration'] ?: [:] + iConf.each { nn, v -> m[nn] = substitute(v,envMap['vm-templates'][t],s,i) } + // manager port + def x = vmTemplate['lifecycle-management'] ?: ['use-dcae-manager': false] + if (! x['use-dcae-manager']) { + m.managerPortNumber = -1 + } + else { + m.managerPortNumber = x['manager-port'] ?: 9999 + } + def installSteps = vmTemplate['installation-steps'] ?: [] + m.steps = [:] + def stepIndex = 0 + def pp = "org.openecomp.dcae.controller.service.vm" + installSteps.each { step -> + def stepName = "step-${stepIndex++}" + switch (step.step) { + case "shell": + m.steps[stepName] = [ + '$class' : "${pp}.ShellInstallationStep", + command : substitute(step.command,[:],s,i) + ] + break + case "maven-artifact": + m.steps[stepName] = [ + '$class' : "${pp}.MavenArtifactInstallationStep", + groupId : step['group-id'], + artifactId : step['artifact-id'], + version : substitute(step['version'],[:],s,i), + type : step['type'], + assemblyId : step['assembly-id'], + applicationId : step['application-id'], + ] + break + case "http": + m.steps[stepName] = [ + '$class' : "${pp}.HttpInstallationStep", + url : substitute(step['url'],[:],s,i), + userName : substitute(step['user-name'],[:],s,i), + password : substitute(step['password'],[:],s,i), + type : step['type'], + artifactId : step['artifact-id'], + applicationId : step['application-id'], + ] + break + default: + System.err.println "Unknown step: $s $i: $step.step" + } + } + save("/services/$s/instances/$i",m,true) + def m1 = [:] + def sConf = vmTemplate['controller-configuration'] ?: [:] + sConf.each { nn, v -> m1[nn] = v } + m1['$class'] = "${descriptor.servicePackage}.${descriptor.serviceClass}" + save("/services/$s",m1,true) + // System.exit(0) + } + } + } + void createDockerServiceInstances() { + def envMap = file2object("$release-$env/env.yaml", false) + dockers().each { s, dockerDeployment -> + def t = dockerDeployment['docker-type'] + Map tenants = tenants(); + def dockerTemplate = file2object("$release/docker-templates/${t}.yaml") + def dockerInstances = file2object("$release-$env/docker-instances/${s}.yaml",false) + def descriptor = file2object("$release/docker-descriptors/${s}.json", false) + if (descriptor == null) { + System.err.println "no docker descriptor for $s" + return + } +// println dockerDeployment +// println dockerTemplate +// println dockerInstances + tenants.each { i, tenant -> + if (tenant.type != dockerDeployment['location-type']) return + def m = [:] + m.hostService = ['$ref' : substitute(dockerDeployment['dcae-container'],[:],s,i) ] + def iConf = dockerTemplate['controller-instance-configuration'] ?: [:] + iConf.each { nn, v -> m[nn] = substitute(v,(envMap['docker-templates'] ?: [:])[t],s,i) } + if (dockerInstances != null && dockerInstances.instances != null) { + def iConf2 = dockerInstances.instances[i] ?: [:] + iConf2.each { nn, v -> m[nn] = v } + } + def x = dockerTemplate['lifecycle-management'] ?: ['use-dcae-manager': true] + if (! x['use-dcae-manager']) { + m.managerPortNumber = -1 + } + else { + m.managerPortNumber = x['manager-port'] ?: 9999 + } + save("/services/$s/instances/$i",m,true) +// createCloudifyBlueprint("docker",s,i,m) +// println m +// System.exit(3) + } + def m1 = [:] + def sConf = dockerTemplate['controller-configuration'] ?: [:] + sConf.each { nn, v -> m1[nn] = v } + m1['$class'] = "${descriptor.servicePackage}.${descriptor.serviceClass}" + save("/services/$s",m1,true) + } + } + + def p(o) { + object2json(o).toString(2) + } + + void createGeneralServiceInstances(t) { + def envMap = file2object("$release-$env/env.yaml", false) + platformConfig.deploymentType.each { s, v -> + if (! s.startsWith(t)) return + def instances = file2object("$release-$env/$t-instances/${s}.yaml",false) ?: [:] + def descriptor = file2object("$release/$t-descriptors/${s}.json", false) + if (descriptor == null) { + System.err.println "no $t descriptor for $s" + return + } + v.instances.each { i, v2 -> + def m = [:] + m.clusterService = ['$ref' : "/services/$v2.hostService/instances/$v2.hostServiceInstance" ] + def iConf = v.type['controller-instance-configuration'] ?: [:] + iConf.each { nn, vv -> m[nn] = substitute(vv,[:],s,i) } + def iConf2 = instances[i] ?: [:] + iConf2.each { nn, vv -> m[nn] = vv } + save("/services/$s/instances/$i",m,true) +// createCloudifyBlueprint("docker",s,i,m) +// println p(m) +// println p(v) +// System.exit(3) + } +// def m1 = [:] +// def sConf = dockerTemplate['controller-configuration'] ?: [:] +// sConf.each { nn, vv -> m1[nn] = vv } +// m1['$class'] = "${descriptor.servicePackage}.${descriptor.serviceClass}" +// save("/services/$s",m1,true) + } + } + + def createCloudifyBlueprint(t,s,i,m) { + def blue = file2object("$release/cloudify-templates/${t}.yaml", false) + def str = object2json(m).toString(2) +// println blue + blue.node_templates.docker.properties = [ + deployment_JSON : str, + service_name : s, + ] + saveYaml("/blueprints/$s-$i",blue,"cloudify") + } + + def createEforc() { + def x = platformConfig.eforc() + saveYaml("/eforc",x.m,"general") + saveYaml("/dns",platformConfig.dns(),"general") + saveYaml("/ssl",platformConfig.ssl(),"general") + saveTable("/eforc-table",x.list,"general") + saveTable("/eforc-subnet-table",x.list2,"general") + saveYaml("/flavors",platformConfig.showFlavors(),"general") + } + + def envMapGlobal = file2object("$release-$env/env.yaml", false) ?: [:] + def substitute(o, env, s, i) { + SimpleTemplateEngine engine = new SimpleTemplateEngine() + String res = o + env = env ?: [:] + env.dcae = platformConfig + env.passwords = envMapGlobal.passwords ?: [:] + env.ENV = env +// env['class'] = '$class' + platformConfig.init(s, i) + switch (o) { + case List: + def l = [] + o.each { e -> + l += substitute(e, env, s, i) + } + return l + case Map: + def m = [:] + o.each { n,v -> + m[n] = substitute(v, env, s, i) + } + return m + } + try { + res = engine.createTemplate("$o").make(env) + if (res.startsWith("_LIST_:") && res.endsWith(":_LIST_")) { + res = res.replace("_LIST_:","").replace(":_LIST_","") + JSONArray a = new JSONArray(res) + return json2object(a) + } + } catch (e) { + System.out.println "$o" + System.out.println "$env" + System.err.println "ERROR $e" + System.err.println "ERROR ${e.printStackTrace()}" + System.exit(2) + } + return res + } + + String ipAssignment(n) { + def m = file2object("$release-$env/ip-assignment.yaml") + return m[n] + } + String certificatePassword(n) { + def m = file2object("$release-$env/certificates/passwords.yaml") + return m[n] + } + + def genEcore(EClass eclass,m) { + if (m == null) return null + JSONObject json = object2json(yamlNames2ecore(m)) +// System.err.println json.toString(2) + return server.json2ecore(eclass, json) + + } + + String yamlNames2ecore2(String n) { + String r = "" + boolean first = true + n.split("-").each { n1 -> + if (first) { + first = false + r+=n1 + } else { + r += StringUtil.capitalize(n1) + } + } +// System.err.println "$n $r" + return r + } + def yamlNames2ecore(o) { + switch (o) { + case List: + def l = [] + o.each { l = yamlNames2ecore(it) } + return l + case Map: + def m1 = [:] + o.each { k,v -> m1[yamlNames2ecore2(k)] = yamlNames2ecore(v) } + return m1 + case boolean: + case long: + case int: + case double: + case Boolean: + case Long: + case Integer: + case String: return o; break + case GString: return o.toString(); break + case null: break; + default: println "Unable to convert: $o ${o.getClass().name}" + } + + } + + void save(String path, o, isService = false ) { + File f = new File(destinationRoot + "/${isService ? 'services-' : ''}resources/${path}.json") + f.getParentFile().mkdirs() + if (f.exists()) f.delete() + switch (o) { + case EObject: f << ManagementServer.ecore2json(o, 1000, null, false).toString(2); break + case JSONObject : f << o.toString(2); break + case Map : f << object2json(o).toString(2); break + default: System.err.println "Unable to save object: path=$path $o" + } +// println "Wrote $f" + } + + void save2(String path, o, String type ) { + File f + switch (type) { + case "chef": f = new File("$destRoot/$release-$env/chef-repo-gen/${path}.json") + } + f.getParentFile().mkdirs() + if (f.exists()) f.delete() + switch (o) { + case EObject: f << ManagementServer.ecore2json(o, 1000, null, false).toString(2); break + case JSONObject : f << o.toString(2); break + case Map : f << object2json(o).toString(2); break + default: System.err.println "Unable to save2 object: path=$path $o" + } + } + + void saveYaml(String path, o, String type ) { + File f + switch (type) { + case "cloudify": f = new File("$destRoot/$release-$env/cloudify-gen/${path}.yaml"); break + case "general": f = new File("$destRoot/$release-$env/general-gen/${path}.yaml"); break + case "chef": f = new File("$destRoot/$release-$env/chef-repo-gen/${path}.yaml"); break + } + f.getParentFile().mkdirs() + if (f.exists()) f.delete() + switch (o) { + case String : f << o; break + case Map : f << object2yaml(o, []); break + default: System.err.println "Unable to save yaml object: path=$path $o" + } + } + void saveTable(String path, l, String type ) { + File f + switch (type) { + case "cloudify": f = new File("$destRoot/$release-$env/cloudify-gen/${path}.csv");break + case "general": f = new File("$destRoot/$release-$env/general-gen/${path}.csv");break + case "chef": f = new File("$destRoot/$release-$env/chef-repo-gen/${path}.csv");break + } + f.getParentFile().mkdirs() + if (f.exists()) f.delete() + l.each { row -> f << "${row.join(",")}\n" } + } + + static main(args) { +// println object2yaml(file2object("1607/dcae_admin_db_pgaas.json"), []) + String root = args.length > 1 ? args[1] : null + String destRoot = args.length > 2 ? args[2] : null + String rel = args.length > 3 ? args[3] : "1607" + GenControllerConfiguration d = new GenControllerConfiguration(rel, args[0], root, destRoot); + d.platformConfig.privateKey = args.length > 4 ? args[4] : null + d.createGeneralServiceInstances('cdap'); + d.createGeneralServiceInstances('other'); + d.createDockerServiceInstances(); + d.createConfiguration(); + d.createLocations(); + d.createServiceInstances(); + d.createChefConfiguration(); + d.createEforc() + System.exit(0); + } + +} |