-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathUpgrade42210to42300.java
More file actions
175 lines (155 loc) · 8.59 KB
/
Upgrade42210to42300.java
File metadata and controls
175 lines (155 loc) · 8.59 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package com.cloud.upgrade.dao;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import com.cloud.network.vpc.VpcOffering;
import com.cloud.offering.NetworkOffering;
import com.cloud.utils.exception.CloudRuntimeException;
public class Upgrade42210to42300 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
@Override
public String[] getUpgradableVersionRange() {
return new String[]{"4.22.1.0", "4.23.0.0"};
}
@Override
public String getUpgradedVersion() {
return "4.23.0.0";
}
@Override
public InputStream[] getPrepareScripts() {
final String scriptFile = "META-INF/db/schema-42210to42300.sql";
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
if (script == null) {
throw new CloudRuntimeException("Unable to find " + scriptFile);
}
return new InputStream[] {script};
}
@Override
public void performDataMigration(Connection conn) {
updateNetworkDefaultOfferingsForVPCWithFirewallService(conn);
updateVpcOfferingsWithFirewallService(conn);
}
private void updateNetworkDefaultOfferingsForVPCWithFirewallService(Connection conn) {
logger.debug("Updating default Network offerings for VPC to add Firewall service with VpcVirtualRouter provider");
final List<String> defaultVpcOfferingUniqueNames = Arrays.asList(
NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworks,
NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksNoLB,
NetworkOffering.DefaultIsolatedNetworkOfferingForVpcNetworksWithInternalLB,
NetworkOffering.DEFAULT_NAT_NSX_OFFERING_FOR_VPC,
NetworkOffering.DEFAULT_ROUTED_NSX_OFFERING_FOR_VPC,
NetworkOffering.DEFAULT_NAT_NSX_OFFERING_FOR_VPC_WITH_ILB,
NetworkOffering.DEFAULT_ROUTED_NETRIS_OFFERING_FOR_VPC,
NetworkOffering.DEFAULT_NAT_NETRIS_OFFERING_FOR_VPC
);
try {
for (String uniqueName : defaultVpcOfferingUniqueNames) {
try (PreparedStatement pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`network_offerings` WHERE unique_name = ?")) {
pstmt.setString(1, uniqueName);
try (ResultSet rs = pstmt.executeQuery()) {
if (!rs.next()) {
continue;
}
long offeringId = rs.getLong(1);
// Insert into ntwk_offering_service_map
try (PreparedStatement insertOfferingPstmt = conn.prepareStatement(
"INSERT IGNORE INTO `cloud`.`ntwk_offering_service_map` " +
"(network_offering_id, service, provider, created) " +
"VALUES (?, 'Firewall', 'VpcVirtualRouter', now())")) {
insertOfferingPstmt.setLong(1, offeringId);
insertOfferingPstmt.executeUpdate();
}
// Update existing networks (ntwk_service_map)
try (PreparedStatement selectNetworksPstmt = conn.prepareStatement(
"SELECT id FROM `cloud`.`networks` WHERE network_offering_id = ?")) {
selectNetworksPstmt.setLong(1, offeringId);
try (ResultSet networksRs = selectNetworksPstmt.executeQuery()) {
while (networksRs.next()) {
long networkId = networksRs.getLong(1);
try (PreparedStatement insertService = conn.prepareStatement(
"INSERT INGORE INTO `cloud`.`ntwk_service_map` " +
"(network_id, service, provider, created) " +
"VALUES (?, 'Firewall', 'VpcVirtualRouter', now())")) {
insertService.setLong(1, networkId);
insertService.executeUpdate();
}
}
}
}
}
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Exception while updating VPC default offerings with Firewall service: " + e.getMessage(), e);
}
}
private void updateVpcOfferingsWithFirewallService(Connection conn) {
logger.debug("Updating default VPC offerings to add Firewall service with VpcVirtualRouter provider");
final List<String> vpcOfferingUniqueNames = Arrays.asList(
VpcOffering.defaultVPCOfferingName,
VpcOffering.defaultVPCNSOfferingName,
VpcOffering.redundantVPCOfferingName,
VpcOffering.DEFAULT_VPC_NAT_NSX_OFFERING_NAME,
VpcOffering.DEFAULT_VPC_ROUTE_NSX_OFFERING_NAME,
VpcOffering.DEFAULT_VPC_ROUTE_NETRIS_OFFERING_NAME,
VpcOffering.DEFAULT_VPC_NAT_NETRIS_OFFERING_NAME
);
try {
for (String uniqueName : vpcOfferingUniqueNames) {
try (PreparedStatement pstmt = conn.prepareStatement("SELECT id FROM `cloud`.`vpc_offerings` WHERE unique_name = ?")) {
pstmt.setString(1, uniqueName);
try (ResultSet rs = pstmt.executeQuery()) {
if (!rs.next()) {
continue;
}
long vpcOfferingId = rs.getLong(1);
// Insert into vpc_offering_service_map
try (PreparedStatement insertOfferingPstmt = conn.prepareStatement(
"INSERT IGNORE INTO `cloud`.`vpc_offering_service_map` " +
"(vpc_offering_id, service, provider, created) " +
"VALUES (?, 'Firewall', 'VpcVirtualRouter', now())")) {
insertOfferingPstmt.setLong(1, vpcOfferingId);
insertOfferingPstmt.executeUpdate();
}
// Update existing VPCs (vpc_service_map)
try (PreparedStatement selectVpcsPstmt = conn.prepareStatement("SELECT id FROM `cloud`.`vpcs` WHERE vpc_offering_id = ?")) {
selectVpcsPstmt.setLong(1, vpcOfferingId);
try (ResultSet vpcsRs = selectVpcsPstmt.executeQuery()) {
while (vpcsRs.next()) {
long vpcId = vpcsRs.getLong(1);
try (PreparedStatement insertService = conn.prepareStatement(
"INSERT IGNORE INTO `cloud`.`vpc_service_map` " +
"(vpc_id, service, provider, created) " +
"VALUES (?, 'Firewall', 'VpcVirtualRouter', now())")) {
insertService.setLong(1, vpcId);
insertService.executeUpdate();
}
}
}
}
}
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("Exception while updating VPC offerings with Firewall service: " + e.getMessage(), e);
}
}
}