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