sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 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 | package org.onosproject.openstacknetworking.impl; |
| 17 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 19 | import org.onosproject.core.CoreService; |
| 20 | import org.onosproject.event.ListenerRegistry; |
| 21 | import org.onosproject.openstacknetworking.api.Constants; |
| 22 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService; |
| 23 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent; |
| 24 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener; |
| 25 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService; |
| 26 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStore; |
| 27 | import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStoreDelegate; |
| 28 | import org.openstack4j.model.network.SecurityGroup; |
| 29 | import org.openstack4j.model.network.SecurityGroupRule; |
| 30 | import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.List; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 39 | import java.util.Objects; |
| 40 | import java.util.Set; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 41 | |
| 42 | import static com.google.common.base.Preconditions.checkArgument; |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 47 | * Provides implementation of administering and interfacing OpenStack security |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 48 | * groups. |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 49 | */ |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 50 | @Component( |
| 51 | immediate = true, |
| 52 | service = { OpenstackSecurityGroupAdminService.class, OpenstackSecurityGroupService.class } |
| 53 | ) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 54 | public class OpenstackSecurityGroupManager |
| 55 | extends ListenerRegistry<OpenstackSecurityGroupEvent, OpenstackSecurityGroupListener> |
| 56 | implements OpenstackSecurityGroupAdminService, OpenstackSecurityGroupService { |
| 57 | |
| 58 | protected final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | private static final String MSG_SG = "OpenStack security group %s %s"; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 61 | private static final String MSG_SG_RULE = "OpenStack security group rule %s %s"; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 62 | |
| 63 | private static final String MSG_CREATED = "created"; |
| 64 | private static final String MSG_REMOVED = "removed"; |
| 65 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 66 | private static final String ERR_NULL_SG = |
| 67 | "OpenStack security group cannot be null"; |
| 68 | private static final String ERR_NULL_SG_ID = |
| 69 | "OpenStack security group ID cannot be null"; |
| 70 | private static final String ERR_NULL_SG_RULE = |
| 71 | "OpenStack security group rule cannot be null"; |
| 72 | private static final String ERR_NULL_SG_RULE_ID = |
| 73 | "OpenStack security group rule ID cannot be null"; |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 74 | private static final String ERR_NOT_FOUND = "not found"; |
| 75 | private static final String ERR_DUPLICATE = "already exist"; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 76 | |
sangho | e6457a3 | 2017-08-24 14:31:19 +0900 | [diff] [blame] | 77 | private boolean useSecurityGroup = false; |
| 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 80 | protected CoreService coreService; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 83 | protected OpenstackSecurityGroupStore osSecurityGroupStore; |
| 84 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 85 | private final OpenstackSecurityGroupStoreDelegate |
| 86 | delegate = new InternalSecurityGroupStoreDelegate(); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 87 | |
| 88 | @Activate |
| 89 | protected void activate() { |
| 90 | coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 91 | osSecurityGroupStore.setDelegate(delegate); |
| 92 | log.info("Started"); |
| 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | protected void deactivate() { |
| 97 | osSecurityGroupStore.unsetDelegate(delegate); |
| 98 | log.info("Stopped"); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void createSecurityGroup(SecurityGroup sg) { |
| 103 | checkNotNull(sg, ERR_NULL_SG); |
| 104 | checkArgument(!Strings.isNullOrEmpty(sg.getId()), ERR_NULL_SG_ID); |
| 105 | |
| 106 | osSecurityGroupStore.createSecurityGroup(sg); |
| 107 | log.info(String.format(MSG_SG, sg.getId(), MSG_CREATED)); |
| 108 | } |
| 109 | |
| 110 | @Override |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 111 | public void updateSecurityGroup(SecurityGroup sg) { |
| 112 | checkNotNull(sg, ERR_NULL_SG); |
| 113 | checkArgument(!Strings.isNullOrEmpty(sg.getId()), ERR_NULL_SG_ID); |
| 114 | |
| 115 | osSecurityGroupStore.updateSecurityGroup(sg); |
| 116 | } |
| 117 | |
| 118 | @Override |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 119 | public void removeSecurityGroup(String sgId) { |
Jian Li | 43e066b | 2018-07-16 17:43:56 +0900 | [diff] [blame] | 120 | checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 121 | |
| 122 | osSecurityGroupStore.removeSecurityGroup(sgId); |
| 123 | log.info(String.format(MSG_SG, sgId, MSG_REMOVED)); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void createSecurityGroupRule(SecurityGroupRule sgRule) { |
| 128 | checkNotNull(sgRule, ERR_NULL_SG_RULE); |
| 129 | checkArgument(!Strings.isNullOrEmpty(sgRule.getId()), ERR_NULL_SG_RULE_ID); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 130 | checkArgument(!Strings.isNullOrEmpty(sgRule.getSecurityGroupId()), ERR_NULL_SG_ID); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 131 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 132 | synchronized (this) { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 133 | SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId()); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 134 | if (sg == null) { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 135 | final String error = String.format(MSG_SG, |
| 136 | sgRule.getSecurityGroupId(), ERR_NOT_FOUND); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 137 | throw new IllegalStateException(error); |
| 138 | } |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 139 | if (sg.getRules().stream().anyMatch(rule -> |
| 140 | Objects.equals(rule.getId(), sgRule.getId()))) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 141 | final String error = String.format(MSG_SG_RULE, |
| 142 | sgRule.getSecurityGroupId(), ERR_DUPLICATE); |
| 143 | throw new IllegalStateException(error); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 146 | // FIXME we cannot add element to extend list |
| 147 | List updatedSgRules = sg.getRules(); |
| 148 | updatedSgRules.add(sgRule); |
| 149 | SecurityGroup updatedSg = NeutronSecurityGroup.builder().from(sg).build(); |
| 150 | osSecurityGroupStore.updateSecurityGroup(updatedSg); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 151 | } |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 152 | |
| 153 | log.info(String.format(MSG_SG_RULE, sgRule.getId(), MSG_CREATED)); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void removeSecurityGroupRule(String sgRuleId) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 158 | checkArgument(!Strings.isNullOrEmpty(sgRuleId), ERR_NULL_SG_RULE_ID); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 159 | |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 160 | synchronized (this) { |
| 161 | SecurityGroupRule sgRule = securityGroupRule(sgRuleId); |
| 162 | if (sgRule == null) { |
| 163 | final String error = String.format(MSG_SG_RULE, sgRuleId, ERR_NOT_FOUND); |
| 164 | throw new IllegalStateException(error); |
| 165 | } |
| 166 | |
| 167 | SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId()); |
| 168 | if (sg == null) { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 169 | final String error = String.format(MSG_SG, |
| 170 | sgRule.getSecurityGroupId(), ERR_NOT_FOUND); |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 171 | throw new IllegalStateException(error); |
| 172 | } |
| 173 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 174 | if (sg.getRules().stream().noneMatch(rule -> |
| 175 | Objects.equals(rule.getId(), sgRule.getId()))) { |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 176 | final String error = String.format(MSG_SG_RULE, |
| 177 | sgRule.getSecurityGroupId(), ERR_NOT_FOUND); |
| 178 | throw new IllegalStateException(error); |
| 179 | } |
| 180 | |
| 181 | // FIXME we cannot handle the element of extend list as a specific class object |
| 182 | List updatedSgRules = sg.getRules(); |
| 183 | updatedSgRules.removeIf(r -> ((SecurityGroupRule) r).getId().equals(sgRuleId)); |
| 184 | SecurityGroup updatedSg = NeutronSecurityGroup.builder().from(sg).build(); |
| 185 | osSecurityGroupStore.updateSecurityGroup(updatedSg); |
| 186 | } |
| 187 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 188 | log.info(String.format(MSG_SG_RULE, sgRuleId, MSG_REMOVED)); |
| 189 | } |
| 190 | |
| 191 | @Override |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 192 | public Set<SecurityGroup> securityGroups() { |
| 193 | return osSecurityGroupStore.securityGroups(); |
| 194 | } |
| 195 | |
| 196 | @Override |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 197 | public SecurityGroup securityGroup(String sgId) { |
| 198 | checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID); |
| 199 | return osSecurityGroupStore.securityGroup(sgId); |
| 200 | } |
| 201 | |
| 202 | @Override |
sangho | e6457a3 | 2017-08-24 14:31:19 +0900 | [diff] [blame] | 203 | public boolean isSecurityGroupEnabled() { |
| 204 | return useSecurityGroup; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void setSecurityGroupEnabled(boolean option) { |
| 209 | useSecurityGroup = option; |
| 210 | } |
| 211 | |
| 212 | @Override |
Hyunsun Moon | ae51e73 | 2017-04-25 17:46:21 +0900 | [diff] [blame] | 213 | public void clear() { |
| 214 | osSecurityGroupStore.clear(); |
| 215 | } |
| 216 | |
| 217 | private SecurityGroupRule securityGroupRule(String sgRuleId) { |
| 218 | return osSecurityGroupStore.securityGroups().stream() |
| 219 | .flatMap(sg -> sg.getRules().stream()) |
| 220 | .filter(sgRule -> Objects.equals(sgRule.getId(), sgRuleId)) |
| 221 | .findFirst().orElse(null); |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 224 | private class InternalSecurityGroupStoreDelegate |
| 225 | implements OpenstackSecurityGroupStoreDelegate { |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame] | 226 | |
| 227 | @Override |
| 228 | public void notify(OpenstackSecurityGroupEvent event) { |
| 229 | if (event != null) { |
| 230 | log.trace("send openstack security group event {}", event); |
| 231 | process(event); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |