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 | 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; |
| 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 | /** |
| 47 | * Provides implementation of administering and interfaceing Openstack security |
| 48 | * groups. |
| 49 | * |
| 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"; |
| 60 | private static final String MSG_SG_RULE = "OpenStack security group %s %s"; |
| 61 | |
| 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"; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected CoreService coreService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected OpenstackSecurityGroupStore osSecurityGroupStore; |
| 76 | |
| 77 | private final OpenstackSecurityGroupStoreDelegate delegate = new InternalSecurityGroupStoreDelegate(); |
| 78 | |
| 79 | @Activate |
| 80 | protected void activate() { |
| 81 | coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 82 | osSecurityGroupStore.setDelegate(delegate); |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | protected void deactivate() { |
| 88 | osSecurityGroupStore.unsetDelegate(delegate); |
| 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void createSecurityGroup(SecurityGroup sg) { |
| 94 | checkNotNull(sg, ERR_NULL_SG); |
| 95 | checkArgument(!Strings.isNullOrEmpty(sg.getId()), ERR_NULL_SG_ID); |
| 96 | |
| 97 | osSecurityGroupStore.createSecurityGroup(sg); |
| 98 | log.info(String.format(MSG_SG, sg.getId(), MSG_CREATED)); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void removeSecurityGroup(String sgId) { |
| 103 | checkNotNull(sgId, ERR_NULL_SG_ID); |
| 104 | |
| 105 | osSecurityGroupStore.removeSecurityGroup(sgId); |
| 106 | log.info(String.format(MSG_SG, sgId, MSG_REMOVED)); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void createSecurityGroupRule(SecurityGroupRule sgRule) { |
| 111 | checkNotNull(sgRule, ERR_NULL_SG_RULE); |
| 112 | checkArgument(!Strings.isNullOrEmpty(sgRule.getId()), ERR_NULL_SG_RULE_ID); |
| 113 | |
| 114 | synchronized (osSecurityGroupStore) { |
| 115 | SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId()); |
| 116 | List sgRules = sg.getRules(); |
| 117 | sgRules.add(sgRule); |
| 118 | SecurityGroup newSg = new NeutronSecurityGroup.SecurityGroupConcreteBuilder().from(sg).build(); |
| 119 | SecurityGroup oldSg = osSecurityGroupStore.updateSecurityGroup(sgRule.getSecurityGroupId(), newSg); |
| 120 | if (oldSg == null) { |
| 121 | log.warn("Failed to add the security group rule {} to security group", sgRule.getId()); |
| 122 | } |
| 123 | |
| 124 | osSecurityGroupStore.createSecurityGroupRule(sgRule); |
| 125 | log.info(String.format(MSG_SG_RULE, sgRule.getId(), MSG_CREATED)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void removeSecurityGroupRule(String sgRuleId) { |
| 131 | checkNotNull(sgRuleId, ERR_NULL_SG_RULE_ID); |
| 132 | |
| 133 | osSecurityGroupStore.removeSecurityGroupRule(sgRuleId); |
| 134 | log.info(String.format(MSG_SG_RULE, sgRuleId, MSG_REMOVED)); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public SecurityGroup securityGroup(String sgId) { |
| 139 | checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID); |
| 140 | return osSecurityGroupStore.securityGroup(sgId); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public SecurityGroupRule securityGroupRule(String sgRuleId) { |
| 145 | checkArgument(!Strings.isNullOrEmpty(sgRuleId), ERR_NULL_SG_RULE_ID); |
| 146 | return osSecurityGroupStore.securityGroupRule(sgRuleId); |
| 147 | } |
| 148 | |
| 149 | private class InternalSecurityGroupStoreDelegate implements OpenstackSecurityGroupStoreDelegate { |
| 150 | |
| 151 | @Override |
| 152 | public void notify(OpenstackSecurityGroupEvent event) { |
| 153 | if (event != null) { |
| 154 | log.trace("send openstack security group event {}", event); |
| 155 | process(event); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |