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