sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.openstacknetworking.impl; |
| 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.IPv4; |
| 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.Ip4Prefix; |
| 31 | import org.onlab.packet.IpPrefix; |
| 32 | import org.onlab.packet.TpPort; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 33 | import org.onlab.util.Tools; |
| 34 | import org.onosproject.cfg.ComponentConfigService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 35 | import org.onosproject.core.ApplicationId; |
| 36 | import org.onosproject.core.CoreService; |
| 37 | import org.onosproject.mastership.MastershipService; |
| 38 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 39 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 41 | import org.onosproject.net.flow.TrafficTreatment; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 42 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 43 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 44 | import org.onosproject.openstacknetworking.api.InstancePortListener; |
| 45 | import org.onosproject.openstacknetworking.api.InstancePortService; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 46 | import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 47 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent; |
| 48 | import org.onosproject.openstacknetworking.api.OpenstackNetworkListener; |
| 49 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 50 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent; |
| 51 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener; |
| 52 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService; |
| 53 | import org.openstack4j.model.network.Port; |
| 54 | import org.openstack4j.model.network.SecurityGroup; |
| 55 | import org.openstack4j.model.network.SecurityGroupRule; |
| 56 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroupRule; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 57 | import org.osgi.service.component.ComponentContext; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 60 | import java.util.Collections; |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 61 | import java.util.Dictionary; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 62 | import java.util.Objects; |
| 63 | import java.util.Set; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.stream.Collectors; |
| 66 | |
| 67 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 68 | import static org.onlab.util.Tools.groupedThreads; |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 69 | import static org.onosproject.openstacknetworking.api.Constants.ACL_TABLE; |
| 70 | import static org.onosproject.openstacknetworking.api.Constants.JUMP_TABLE; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 71 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
| 72 | import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_ACL_RULE; |
| 73 | import static org.slf4j.LoggerFactory.getLogger; |
| 74 | |
| 75 | /** |
| 76 | * Populates flow rules to handle OpenStack SecurityGroups. |
| 77 | */ |
| 78 | @Component(immediate = true) |
| 79 | public class OpenstackSecurityGroupHandler { |
| 80 | |
| 81 | private final Logger log = getLogger(getClass()); |
| 82 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 83 | private static final boolean USE_SECURITY_GROUP = false; |
| 84 | |
| 85 | @Property(name = "useSecurityGroup", boolValue = USE_SECURITY_GROUP, |
| 86 | label = "Apply OpenStack security group rule for VM traffic") |
| 87 | private boolean useSecurityGroup = USE_SECURITY_GROUP; |
| 88 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected CoreService coreService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected InstancePortService instancePortService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected MastershipService mastershipService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 99 | protected OpenstackNetworkService osNetService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected OpenstackSecurityGroupService securityGroupService; |
| 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 105 | protected OpenstackFlowRuleService osFlowRuleService; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 106 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 108 | protected ComponentConfigService configService; |
| 109 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 110 | private final InstancePortListener instancePortListener = new InternalInstancePortListener(); |
| 111 | private final OpenstackNetworkListener portListener = new InternalOpenstackPortListener(); |
| 112 | private final OpenstackSecurityGroupListener securityGroupListener = new InternalSecurityGroupListener(); |
| 113 | private ApplicationId appId; |
| 114 | |
| 115 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 116 | groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
| 117 | |
| 118 | private static final String PROTO_ICMP = "ICMP"; |
| 119 | private static final String PROTO_TCP = "TCP"; |
| 120 | private static final String PROTO_UDP = "UDP"; |
| 121 | private static final String ETHTYPE_IPV4 = "IPV4"; |
| 122 | private static final String EGRESS = "EGRESS"; |
| 123 | private static final String INGRESS = "INGRESS"; |
| 124 | private static final IpPrefix IP_PREFIX_ANY = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 125 | |
| 126 | @Activate |
| 127 | protected void activate() { |
| 128 | appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 129 | instancePortService.addListener(instancePortListener); |
| 130 | securityGroupService.addListener(securityGroupListener); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 131 | osNetService.addListener(portListener); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 132 | configService.registerProperties(getClass()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 133 | |
| 134 | log.info("Started"); |
| 135 | } |
| 136 | |
| 137 | @Deactivate |
| 138 | protected void deactivate() { |
| 139 | instancePortService.removeListener(instancePortListener); |
| 140 | securityGroupService.removeListener(securityGroupListener); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 141 | osNetService.removeListener(portListener); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 142 | configService.unregisterProperties(getClass(), false); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 143 | eventExecutor.shutdown(); |
| 144 | |
| 145 | log.info("Stopped"); |
| 146 | } |
| 147 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 148 | @Modified |
| 149 | protected void modified(ComponentContext context) { |
| 150 | Dictionary<?, ?> properties = context.getProperties(); |
| 151 | Boolean flag; |
| 152 | |
| 153 | flag = Tools.isPropertyEnabled(properties, "useSecurityGroup"); |
| 154 | if (flag == null) { |
| 155 | log.info("useSecurityGroup is not configured, " + |
| 156 | "using current value of {}", useSecurityGroup); |
| 157 | } else { |
| 158 | useSecurityGroup = flag; |
| 159 | log.info("Configured. useSecurityGroup is {}", |
| 160 | useSecurityGroup ? "enabled" : "disabled"); |
| 161 | } |
| 162 | |
| 163 | resetSecurityGroupRules(); |
| 164 | } |
| 165 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 166 | private void setSecurityGroupRules(InstancePort instPort, Port port, boolean install) { |
| 167 | port.getSecurityGroups().forEach(sgId -> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 168 | SecurityGroup sg = securityGroupService.securityGroup(sgId); |
| 169 | if (sg == null) { |
| 170 | log.error("Security Group Not Found : {}", sgId); |
| 171 | return; |
| 172 | } |
| 173 | sg.getRules().forEach(sgRule -> updateSecurityGroupRule(instPort, port, sgRule, install)); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 174 | final String action = install ? "Installed " : "Removed "; |
| 175 | log.debug(action + "security group rule ID : " + sgId); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 176 | }); |
| 177 | } |
| 178 | |
| 179 | private void updateSecurityGroupRule(InstancePort instPort, Port port, SecurityGroupRule sgRule, boolean install) { |
| 180 | if (sgRule.getRemoteGroupId() != null && !sgRule.getRemoteGroupId().isEmpty()) { |
| 181 | getRemoteInstPorts(port.getTenantId(), sgRule.getRemoteGroupId()) |
| 182 | .forEach(rInstPort -> { |
| 183 | populateSecurityGroupRule(sgRule, instPort, rInstPort.ipAddress().toIpPrefix(), install); |
| 184 | populateSecurityGroupRule(sgRule, rInstPort, instPort.ipAddress().toIpPrefix(), install); |
| 185 | |
| 186 | SecurityGroupRule rSgRule = new NeutronSecurityGroupRule.SecurityGroupRuleConcreteBuilder() |
| 187 | .from(sgRule) |
| 188 | .direction(sgRule.getDirection().toUpperCase().equals(EGRESS) ? INGRESS : EGRESS).build(); |
| 189 | populateSecurityGroupRule(rSgRule, instPort, rInstPort.ipAddress().toIpPrefix(), install); |
| 190 | populateSecurityGroupRule(rSgRule, rInstPort, instPort.ipAddress().toIpPrefix(), install); |
| 191 | }); |
| 192 | } else { |
| 193 | populateSecurityGroupRule(sgRule, instPort, sgRule.getRemoteIpPrefix() == null ? IP_PREFIX_ANY : |
| 194 | IpPrefix.valueOf(sgRule.getRemoteIpPrefix()), install); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private void populateSecurityGroupRule(SecurityGroupRule sgRule, InstancePort instPort, |
| 199 | IpPrefix remoteIp, boolean install) { |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 200 | Ip4Address vmIp = Ip4Address.valueOf(instPort.ipAddress().toInetAddress()); |
| 201 | if (remoteIp != null && remoteIp.equals(IpPrefix.valueOf(vmIp, 32))) { |
| 202 | // do nothing if the remote IP is my IP |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
sangho | dc37537 | 2017-06-08 10:41:30 +0900 | [diff] [blame] | 206 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 207 | buildMatchs(sBuilder, sgRule, vmIp, remoteIp); |
| 208 | |
| 209 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().transition(JUMP_TABLE).build(); |
| 210 | |
| 211 | osFlowRuleService.setRule(appId, |
| 212 | instPort.deviceId(), |
| 213 | sBuilder.build(), |
| 214 | treatment, |
| 215 | PRIORITY_ACL_RULE, |
| 216 | ACL_TABLE, |
| 217 | install); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns a set of host IP addresses engaged with supplied security group ID. |
| 222 | * It only searches a VM in the same tenant boundary. |
| 223 | * |
| 224 | * @param tenantId tenant id |
| 225 | * @param sgId security group id |
| 226 | * @return set of ip addresses |
| 227 | */ |
| 228 | private Set<InstancePort> getRemoteInstPorts(String tenantId, String sgId) { |
| 229 | Set<InstancePort> remoteInstPorts; |
| 230 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 231 | remoteInstPorts = osNetService.ports().stream() |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 232 | .filter(port -> port.getTenantId().equals(tenantId)) |
| 233 | .filter(port -> port.getSecurityGroups().contains(sgId)) |
| 234 | .map(port -> instancePortService.instancePort(port.getId())) |
| 235 | .filter(instPort -> instPort != null && instPort.ipAddress() != null) |
| 236 | .collect(Collectors.toSet()); |
| 237 | |
| 238 | return Collections.unmodifiableSet(remoteInstPorts); |
| 239 | } |
| 240 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 241 | private void buildMatchs(TrafficSelector.Builder sBuilder, SecurityGroupRule sgRule, |
| 242 | Ip4Address vmIp, IpPrefix remoteIp) { |
| 243 | buildMatchEthType(sBuilder, sgRule.getEtherType()); |
| 244 | buildMatchDirection(sBuilder, sgRule.getDirection(), vmIp); |
| 245 | buildMatchProto(sBuilder, sgRule.getProtocol()); |
| 246 | buildMatchPort(sBuilder, sgRule.getProtocol(), sgRule.getDirection(), |
| 247 | sgRule.getPortRangeMax() == null ? 0 : sgRule.getPortRangeMax(), |
| 248 | sgRule.getPortRangeMin() == null ? 0 : sgRule.getPortRangeMin()); |
| 249 | buildMatchRemoteIp(sBuilder, remoteIp, sgRule.getDirection()); |
| 250 | if (sgRule.getRemoteGroupId() != null && sgRule.getRemoteGroupId().isEmpty()) { |
| 251 | buildMatchRemoteIp(sBuilder, remoteIp, sgRule.getDirection()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private void buildMatchDirection(TrafficSelector.Builder sBuilder, |
| 256 | String direction, |
| 257 | Ip4Address vmIp) { |
| 258 | if (direction.toUpperCase().equals(EGRESS)) { |
| 259 | sBuilder.matchIPSrc(IpPrefix.valueOf(vmIp, 32)); |
| 260 | } else { |
| 261 | sBuilder.matchIPDst(IpPrefix.valueOf(vmIp, 32)); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private void buildMatchEthType(TrafficSelector.Builder sBuilder, String etherType) { |
| 266 | // Either IpSrc or IpDst (or both) is set by default, and we need to set EthType as IPv4. |
| 267 | sBuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 268 | if (etherType != null && !Objects.equals(etherType, "null") && |
| 269 | !etherType.toUpperCase().equals(ETHTYPE_IPV4)) { |
| 270 | log.debug("EthType {} is not supported yet in Security Group", etherType); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private void buildMatchRemoteIp(TrafficSelector.Builder sBuilder, IpPrefix remoteIpPrefix, String direction) { |
| 275 | if (remoteIpPrefix != null && !remoteIpPrefix.getIp4Prefix().equals(IP_PREFIX_ANY)) { |
| 276 | if (direction.toUpperCase().equals(EGRESS)) { |
| 277 | sBuilder.matchIPDst(remoteIpPrefix); |
| 278 | } else { |
| 279 | sBuilder.matchIPSrc(remoteIpPrefix); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | private void buildMatchProto(TrafficSelector.Builder sBuilder, String protocol) { |
| 285 | if (protocol != null) { |
| 286 | switch (protocol.toUpperCase()) { |
| 287 | case PROTO_ICMP: |
| 288 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_ICMP); |
| 289 | break; |
| 290 | case PROTO_TCP: |
| 291 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP); |
| 292 | break; |
| 293 | case PROTO_UDP: |
| 294 | sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP); |
| 295 | break; |
| 296 | default: |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | private void buildMatchPort(TrafficSelector.Builder sBuilder, String protocol, String direction, |
| 302 | int portMin, int portMax) { |
| 303 | if (portMin > 0 && portMax > 0 && portMin == portMax) { |
| 304 | if (protocol.toUpperCase().equals(PROTO_TCP)) { |
| 305 | if (direction.toUpperCase().equals(EGRESS)) { |
| 306 | sBuilder.matchTcpSrc(TpPort.tpPort(portMax)); |
| 307 | } else { |
| 308 | sBuilder.matchTcpDst(TpPort.tpPort(portMax)); |
| 309 | } |
| 310 | } else if (protocol.toUpperCase().equals(PROTO_UDP)) { |
| 311 | if (direction.toUpperCase().equals(EGRESS)) { |
| 312 | sBuilder.matchUdpSrc(TpPort.tpPort(portMax)); |
| 313 | } else { |
| 314 | sBuilder.matchUdpDst(TpPort.tpPort(portMax)); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 320 | private void resetSecurityGroupRules() { |
| 321 | |
| 322 | if (useSecurityGroup) { |
| 323 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 324 | securityGroup.getRules().forEach(this::securityGroupRuleAdded)); |
| 325 | } else { |
| 326 | securityGroupService.securityGroups().forEach(securityGroup -> |
| 327 | securityGroup.getRules().forEach(this::securityGroupRuleRemoved)); |
| 328 | } |
| 329 | |
| 330 | log.info("Reset security group info " + (useSecurityGroup ? " with " : " without") + " Security Group"); |
| 331 | } |
| 332 | |
| 333 | private void securityGroupRuleAdded(SecurityGroupRule sgRule) { |
| 334 | osNetService.ports().stream() |
| 335 | .filter(port -> port.getSecurityGroups().contains(sgRule.getSecurityGroupId())) |
| 336 | .forEach(port -> { |
| 337 | updateSecurityGroupRule( |
| 338 | instancePortService.instancePort(port.getId()), |
| 339 | port, sgRule, true); |
| 340 | log.debug("Applied security group rule {} to port {}", |
| 341 | sgRule.getId(), port.getId()); |
| 342 | }); |
| 343 | } |
| 344 | |
| 345 | private void securityGroupRuleRemoved(SecurityGroupRule sgRule) { |
| 346 | osNetService.ports().stream() |
| 347 | .filter(port -> port.getSecurityGroups().contains(sgRule.getSecurityGroupId())) |
| 348 | .forEach(port -> { |
| 349 | updateSecurityGroupRule( |
| 350 | instancePortService.instancePort(port.getId()), |
| 351 | port, sgRule, false); |
| 352 | log.debug("Removed security group rule {} from port {}", |
| 353 | sgRule.getId(), port.getId()); |
| 354 | }); |
| 355 | } |
| 356 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 357 | private class InternalInstancePortListener implements InstancePortListener { |
| 358 | |
| 359 | @Override |
| 360 | public boolean isRelevant(InstancePortEvent event) { |
| 361 | InstancePort instPort = event.subject(); |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 362 | if (!useSecurityGroup) { |
| 363 | return false; |
| 364 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 365 | return mastershipService.isLocalMaster(instPort.deviceId()); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public void event(InstancePortEvent event) { |
| 370 | InstancePort instPort = event.subject(); |
| 371 | switch (event.type()) { |
| 372 | case OPENSTACK_INSTANCE_PORT_UPDATED: |
| 373 | case OPENSTACK_INSTANCE_PORT_DETECTED: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 374 | log.debug("Instance port detected MAC:{} IP:{}", |
| 375 | instPort.macAddress(), |
| 376 | instPort.ipAddress()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 377 | eventExecutor.execute(() -> { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 378 | setSecurityGroupRules(instPort, |
| 379 | osNetService.port(event.subject().portId()), |
| 380 | true); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 381 | }); |
| 382 | break; |
| 383 | case OPENSTACK_INSTANCE_PORT_VANISHED: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 384 | log.debug("Instance port vanished MAC:{} IP:{}", |
| 385 | instPort.macAddress(), |
| 386 | instPort.ipAddress()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 387 | eventExecutor.execute(() -> { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 388 | setSecurityGroupRules(instPort, |
| 389 | osNetService.port(event.subject().portId()), |
| 390 | false); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 391 | }); |
| 392 | break; |
| 393 | default: |
| 394 | break; |
| 395 | } |
| 396 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | private class InternalOpenstackPortListener implements OpenstackNetworkListener { |
| 400 | |
| 401 | @Override |
| 402 | public boolean isRelevant(OpenstackNetworkEvent event) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 403 | if (event.port() == null || !Strings.isNullOrEmpty(event.port().getId())) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 404 | return false; |
| 405 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 406 | if (event.securityGroupId() == null || |
| 407 | securityGroupService.securityGroup(event.securityGroupId()) == null) { |
| 408 | return false; |
| 409 | } |
| 410 | if (instancePortService.instancePort(event.port().getId()) == null) { |
| 411 | return false; |
| 412 | } |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 413 | if (!useSecurityGroup) { |
| 414 | return false; |
| 415 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 416 | return true; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void event(OpenstackNetworkEvent event) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 421 | Port osPort = event.port(); |
| 422 | InstancePort instPort = instancePortService.instancePort(osPort.getId()); |
| 423 | SecurityGroup osSg = securityGroupService.securityGroup(event.securityGroupId()); |
| 424 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 425 | switch (event.type()) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 426 | case OPENSTACK_PORT_SECURITY_GROUP_ADDED: |
| 427 | eventExecutor.execute(() -> { |
| 428 | osSg.getRules().forEach(sgRule -> { |
| 429 | updateSecurityGroupRule(instPort, osPort, sgRule, true); |
| 430 | }); |
| 431 | log.info("Added security group {} to port {}", |
| 432 | event.securityGroupId(), event.port().getId()); |
| 433 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 434 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 435 | case OPENSTACK_PORT_SECURITY_GROUP_REMOVED: |
| 436 | eventExecutor.execute(() -> { |
| 437 | osSg.getRules().forEach(sgRule -> { |
| 438 | updateSecurityGroupRule(instPort, osPort, sgRule, false); |
| 439 | }); |
| 440 | log.info("Removed security group {} from port {}", |
| 441 | event.securityGroupId(), event.port().getId()); |
| 442 | }); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 443 | break; |
| 444 | default: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 445 | // do nothing for the other events |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | private class InternalSecurityGroupListener implements OpenstackSecurityGroupListener { |
| 452 | |
| 453 | @Override |
sangho | 0248ca2 | 2017-05-31 13:22:47 +0900 | [diff] [blame] | 454 | public boolean isRelevant(OpenstackSecurityGroupEvent event) { |
| 455 | if (!useSecurityGroup) { |
| 456 | return false; |
| 457 | } |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | @Override |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 462 | public void event(OpenstackSecurityGroupEvent event) { |
| 463 | switch (event.type()) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 464 | case OPENSTACK_SECURITY_GROUP_RULE_CREATED: |
| 465 | SecurityGroupRule securityGroupRuleToAdd = event.securityGroupRule(); |
| 466 | eventExecutor.execute(() -> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 467 | securityGroupRuleAdded(securityGroupRuleToAdd); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 468 | log.info("Applied new security group rule {} to ports", |
| 469 | securityGroupRuleToAdd.getId()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 470 | }); |
| 471 | break; |
| 472 | |
| 473 | case OPENSTACK_SECURITY_GROUP_RULE_REMOVED: |
| 474 | SecurityGroupRule securityGroupRuleToRemove = event.securityGroupRule(); |
| 475 | eventExecutor.execute(() -> { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 476 | securityGroupRuleRemoved(securityGroupRuleToRemove); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 477 | log.info("Removed security group rule {} from ports", |
| 478 | securityGroupRuleToRemove.getId()); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 479 | }); |
| 480 | break; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 481 | case OPENSTACK_SECURITY_GROUP_CREATED: |
| 482 | case OPENSTACK_SECURITY_GROUP_REMOVED: |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 483 | default: |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 484 | // do nothing |
| 485 | break; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 486 | } |
| 487 | } |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 488 | } |
| 489 | } |