blob: f6a4de7e42360c457a6418ae8670c94fc92d05db [file] [log] [blame]
sangho6a9ff0d2017-03-27 11:23:37 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
sangho6a9ff0d2017-03-27 11:23:37 +09003 *
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 */
16package org.onosproject.openstacknetworking.impl;
17
18
19import com.google.common.base.Strings;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onosproject.core.CoreService;
27import org.onosproject.event.ListenerRegistry;
28import org.onosproject.openstacknetworking.api.Constants;
29import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
30import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent;
31import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener;
32import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService;
33import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStore;
34import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStoreDelegate;
35import org.openstack4j.model.network.SecurityGroup;
36import org.openstack4j.model.network.SecurityGroupRule;
37import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
38import org.slf4j.Logger;
39
40import java.util.List;
Hyunsun Moonae51e732017-04-25 17:46:21 +090041import java.util.Objects;
42import java.util.Set;
sangho6a9ff0d2017-03-27 11:23:37 +090043
44import static com.google.common.base.Preconditions.checkArgument;
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.slf4j.LoggerFactory.getLogger;
47
48/**
Hyunsun Moonae51e732017-04-25 17:46:21 +090049 * Provides implementation of administering and interfacing OpenStack security
sangho6a9ff0d2017-03-27 11:23:37 +090050 * groups.
sangho6a9ff0d2017-03-27 11:23:37 +090051 */
52@Service
53@Component(immediate = true)
54public 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 Moonae51e732017-04-25 17:46:21 +090061 private static final String MSG_SG_RULE = "OpenStack security group rule %s %s";
sangho6a9ff0d2017-03-27 11:23:37 +090062
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 Moonae51e732017-04-25 17:46:21 +090070 private static final String ERR_NOT_FOUND = "not found";
71 private static final String ERR_DUPLICATE = "already exist";
sangho6a9ff0d2017-03-27 11:23:37 +090072
sanghoe6457a32017-08-24 14:31:19 +090073 private boolean useSecurityGroup = false;
74
sangho6a9ff0d2017-03-27 11:23:37 +090075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected CoreService coreService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected OpenstackSecurityGroupStore osSecurityGroupStore;
80
81 private final OpenstackSecurityGroupStoreDelegate delegate = new InternalSecurityGroupStoreDelegate();
82
83 @Activate
84 protected void activate() {
85 coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID);
86 osSecurityGroupStore.setDelegate(delegate);
87 log.info("Started");
88 }
89
90 @Deactivate
91 protected void deactivate() {
92 osSecurityGroupStore.unsetDelegate(delegate);
93 log.info("Stopped");
94 }
95
96 @Override
97 public void createSecurityGroup(SecurityGroup sg) {
98 checkNotNull(sg, ERR_NULL_SG);
99 checkArgument(!Strings.isNullOrEmpty(sg.getId()), ERR_NULL_SG_ID);
100
101 osSecurityGroupStore.createSecurityGroup(sg);
102 log.info(String.format(MSG_SG, sg.getId(), MSG_CREATED));
103 }
104
105 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900106 public void updateSecurityGroup(SecurityGroup sg) {
107 checkNotNull(sg, ERR_NULL_SG);
108 checkArgument(!Strings.isNullOrEmpty(sg.getId()), ERR_NULL_SG_ID);
109
110 osSecurityGroupStore.updateSecurityGroup(sg);
111 }
112
113 @Override
sangho6a9ff0d2017-03-27 11:23:37 +0900114 public void removeSecurityGroup(String sgId) {
115 checkNotNull(sgId, ERR_NULL_SG_ID);
116
117 osSecurityGroupStore.removeSecurityGroup(sgId);
118 log.info(String.format(MSG_SG, sgId, MSG_REMOVED));
119 }
120
121 @Override
122 public void createSecurityGroupRule(SecurityGroupRule sgRule) {
123 checkNotNull(sgRule, ERR_NULL_SG_RULE);
124 checkArgument(!Strings.isNullOrEmpty(sgRule.getId()), ERR_NULL_SG_RULE_ID);
Hyunsun Moonae51e732017-04-25 17:46:21 +0900125 checkArgument(!Strings.isNullOrEmpty(sgRule.getSecurityGroupId()), ERR_NULL_SG_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900126
Hyunsun Moonae51e732017-04-25 17:46:21 +0900127 synchronized (this) {
sangho6a9ff0d2017-03-27 11:23:37 +0900128 SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId());
Hyunsun Moonae51e732017-04-25 17:46:21 +0900129 if (sg == null) {
130 final String error = String.format(MSG_SG, sgRule.getSecurityGroupId(), ERR_NOT_FOUND);
131 throw new IllegalStateException(error);
132 }
133 if (sg.getRules().stream().anyMatch(rule -> Objects.equals(rule.getId(), sgRule.getId()))) {
134 final String error = String.format(MSG_SG_RULE,
135 sgRule.getSecurityGroupId(), ERR_DUPLICATE);
136 throw new IllegalStateException(error);
sangho6a9ff0d2017-03-27 11:23:37 +0900137 }
138
Hyunsun Moonae51e732017-04-25 17:46:21 +0900139 // FIXME we cannot add element to extend list
140 List updatedSgRules = sg.getRules();
141 updatedSgRules.add(sgRule);
142 SecurityGroup updatedSg = NeutronSecurityGroup.builder().from(sg).build();
143 osSecurityGroupStore.updateSecurityGroup(updatedSg);
sangho6a9ff0d2017-03-27 11:23:37 +0900144 }
Hyunsun Moonae51e732017-04-25 17:46:21 +0900145
146 log.info(String.format(MSG_SG_RULE, sgRule.getId(), MSG_CREATED));
sangho6a9ff0d2017-03-27 11:23:37 +0900147 }
148
149 @Override
150 public void removeSecurityGroupRule(String sgRuleId) {
Hyunsun Moonae51e732017-04-25 17:46:21 +0900151 checkArgument(!Strings.isNullOrEmpty(sgRuleId), ERR_NULL_SG_RULE_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900152
Hyunsun Moonae51e732017-04-25 17:46:21 +0900153 synchronized (this) {
154 SecurityGroupRule sgRule = securityGroupRule(sgRuleId);
155 if (sgRule == null) {
156 final String error = String.format(MSG_SG_RULE, sgRuleId, ERR_NOT_FOUND);
157 throw new IllegalStateException(error);
158 }
159
160 SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId());
161 if (sg == null) {
162 final String error = String.format(MSG_SG, sgRule.getSecurityGroupId(), ERR_NOT_FOUND);
163 throw new IllegalStateException(error);
164 }
165
166 if (sg.getRules().stream().noneMatch(rule -> Objects.equals(rule.getId(), sgRule.getId()))) {
167 final String error = String.format(MSG_SG_RULE,
168 sgRule.getSecurityGroupId(), ERR_NOT_FOUND);
169 throw new IllegalStateException(error);
170 }
171
172 // FIXME we cannot handle the element of extend list as a specific class object
173 List updatedSgRules = sg.getRules();
174 updatedSgRules.removeIf(r -> ((SecurityGroupRule) r).getId().equals(sgRuleId));
175 SecurityGroup updatedSg = NeutronSecurityGroup.builder().from(sg).build();
176 osSecurityGroupStore.updateSecurityGroup(updatedSg);
177 }
178
sangho6a9ff0d2017-03-27 11:23:37 +0900179 log.info(String.format(MSG_SG_RULE, sgRuleId, MSG_REMOVED));
180 }
181
182 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900183 public Set<SecurityGroup> securityGroups() {
184 return osSecurityGroupStore.securityGroups();
185 }
186
187 @Override
sangho6a9ff0d2017-03-27 11:23:37 +0900188 public SecurityGroup securityGroup(String sgId) {
189 checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID);
190 return osSecurityGroupStore.securityGroup(sgId);
191 }
192
193 @Override
sanghoe6457a32017-08-24 14:31:19 +0900194 public boolean isSecurityGroupEnabled() {
195 return useSecurityGroup;
196 }
197
198 @Override
199 public void setSecurityGroupEnabled(boolean option) {
200 useSecurityGroup = option;
201 }
202
203 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900204 public void clear() {
205 osSecurityGroupStore.clear();
206 }
207
208 private SecurityGroupRule securityGroupRule(String sgRuleId) {
209 return osSecurityGroupStore.securityGroups().stream()
210 .flatMap(sg -> sg.getRules().stream())
211 .filter(sgRule -> Objects.equals(sgRule.getId(), sgRuleId))
212 .findFirst().orElse(null);
sangho6a9ff0d2017-03-27 11:23:37 +0900213 }
214
215 private class InternalSecurityGroupStoreDelegate implements OpenstackSecurityGroupStoreDelegate {
216
217 @Override
218 public void notify(OpenstackSecurityGroupEvent event) {
219 if (event != null) {
220 log.trace("send openstack security group event {}", event);
221 process(event);
222 }
223 }
224 }
225}