-
Notifications
You must be signed in to change notification settings - Fork 12
/
VRouterNotifier.java
199 lines (171 loc) · 7.26 KB
/
VRouterNotifier.java
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
/**
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*
* @author Andra Cismaru
*/
package net.juniper.contrail.vcenter;
import com.google.common.base.Throwables;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import net.juniper.contrail.vcenter.ContrailVRouterApi;
public class VRouterNotifier {
static volatile HashMap<String, ContrailVRouterApi> vrouterApiMap =
new HashMap<String, ContrailVRouterApi>();
static final int vrouterApiPort = 9091;
private final static Logger s_logger =
Logger.getLogger(VRouterNotifier.class);
public static Map<String, ContrailVRouterApi> getVrouterApiMap() {
return vrouterApiMap;
}
public static void created(VirtualMachineInterfaceInfo vmiInfo) {
if (vmiInfo == null) {
s_logger.error("Null vmiInfo argument, cannot perform addPort");
return;
}
if (vmiInfo.vmInfo == null) {
s_logger.error("Null vmInfo, cannot perform addPort for " + vmiInfo);
return;
}
if (vmiInfo.vnInfo == null) {
s_logger.error("Null vnInfo, cannot perform addPort for " + vmiInfo);
return;
}
if (vmiInfo.getUuid() == null) {
s_logger.error("Null uuid, cannot perform addPort for " + vmiInfo);
return;
}
String vrouterIpAddress = vmiInfo.getVmInfo().getVrouterIpAddress();
String ipAddress = vmiInfo.getIpAddress();
VirtualMachineInfo vmInfo = vmiInfo.vmInfo;
VirtualNetworkInfo vnInfo = vmiInfo.vnInfo;
if (vrouterIpAddress == null) {
s_logger.error(
"addPort notification NOT sent as vRouterIp Address not known for " + vmiInfo);
return;
}
if (!vmInfo.isPoweredOnState()) {
s_logger.info(vmInfo + " is PoweredOff. Skip AddPort now for " + vmiInfo);
return;
}
if (ipAddress == null) {
ipAddress = "0.0.0.0";
}
try {
ContrailVRouterApi vrouterApi = vrouterApiMap.get(vrouterIpAddress);
if (vrouterApi == null) {
vrouterApi = new ContrailVRouterApi(vrouterIpAddress, vrouterApiPort);
vrouterApiMap.put(vrouterIpAddress, vrouterApi);
}
boolean ret = vrouterApi.addPort(vmiInfo.getUuid(),
vmInfo.getUuid(), vmiInfo.getUuid(),
ipAddress,
vmiInfo.getMacAddress(),
vnInfo.getUuid(),
vnInfo.getPrimaryVlanId(),
vnInfo.getIsolatedVlanId(),
vmInfo.getDisplayName(),
vnInfo.getProjectUuid());
if (ret) {
s_logger.info("vRouter " + vrouterIpAddress + " addPort success for " + vmiInfo);
} else {
// log failure but don't worry. Periodic KeepAlive task will
// attempt to connect to vRouter Agent and replay AddPorts.
s_logger.warn("vRouter " + vrouterIpAddress + " addPort failed for " + vmiInfo);
}
} catch(Throwable e) {
s_logger.warn("vRouter " + vrouterIpAddress + " Exception in addPort for "
+ vmiInfo + ": " + e.getMessage());
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
public static void deleted(VirtualMachineInterfaceInfo vmiInfo) {
if (vmiInfo == null) {
s_logger.error("Null vmiInfo argument, cannot perform deletePort");
return;
}
if (vmiInfo.vmInfo == null) {
s_logger.error("Null vmInfo, cannot perform deletePort for " + vmiInfo);
return;
}
if (vmiInfo.vnInfo == null) {
s_logger.error("Null vnInfo, cannot perform deletePort for " + vmiInfo);
return;
}
if (vmiInfo.getUuid() == null) {
s_logger.error("Null uuid, cannot perform deletePort for " + vmiInfo);
return;
}
String vrouterIpAddress = vmiInfo.getVmInfo().getVrouterIpAddress();
String ipAddress = vmiInfo.getIpAddress();
if (vrouterIpAddress == null) {
s_logger.error(
"deletePort notification NOT sent as vRouterIp Address not known for " + vmiInfo);
return;
}
if (ipAddress == null) {
ipAddress = "0.0.0.0";
}
ContrailVRouterApi vrouterApi = vrouterApiMap.get(vrouterIpAddress);
if (vrouterApi == null) {
vrouterApi = new ContrailVRouterApi(vrouterIpAddress, vrouterApiPort);
vrouterApiMap.put(vrouterIpAddress, vrouterApi);
}
boolean ret = vrouterApi.deletePort(vmiInfo.getUuid());
if (ret) {
s_logger.info("vRouter " + vrouterIpAddress +
" DeletePort success for " + vmiInfo);
} else {
// log failure but don't worry. Periodic KeepAlive task will
// attempt to connect to vRouter Agent and replay DeletePorts.
s_logger.warn("vRouter " + vrouterIpAddress + " DeletePort failed for " + vmiInfo);
}
}
// KeepAlive with all active vRouter Agent Connections.
public static void vrouterAgentPeriodicConnectionCheck() {
Map<String, Boolean> vRouterActiveMap = VCenterDB.vRouterActiveMap;
for (Map.Entry<String, Boolean> entry: vRouterActiveMap.entrySet()) {
if (entry.getValue() == Boolean.FALSE) {
// host is in maintenance mode
continue;
}
String vrouterIpAddress = entry.getKey();
ContrailVRouterApi vrouterApi = vrouterApiMap.get(vrouterIpAddress);
if (vrouterApi == null) {
vrouterApi = new ContrailVRouterApi(
vrouterIpAddress,
vrouterApiPort);
vrouterApiMap.put(vrouterIpAddress, vrouterApi);
}
if (!vrouterApi.periodicCheck()) {
s_logger.warn(vrouterIpAddress + " periodic check failed");
}
}
}
public static void syncVrouterAgent() {
Map<String, Boolean> vRouterActiveMap = VCenterDB.vRouterActiveMap;
for (Map.Entry<String, Boolean> entry: vRouterActiveMap.entrySet()) {
if (entry.getValue() == Boolean.FALSE) {
// host is in maintenance mode
continue;
}
String vrouterIpAddress = entry.getKey();
ContrailVRouterApi vrouterApi = vrouterApiMap.get(vrouterIpAddress);
if (vrouterApi == null) {
vrouterApi = new ContrailVRouterApi(
vrouterIpAddress,
vrouterApiPort);
vrouterApiMap.put(vrouterIpAddress, vrouterApi);
}
boolean ret = vrouterApi.sync();
if (ret) {
s_logger.info("vRouter " + vrouterIpAddress +
" sync request success");
} else {
// log failure but don't worry. Periodic task will
// attempt to connect to vRouter Agent and replay sync.
s_logger.warn("vRouter " + vrouterIpAddress + " sync request failed");
}
}
}
}