blob: e59841336cd891913c55017bd51c6acad5198166 [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
sangho6a9ff0d2017-03-27 11:23:37 +090018import com.google.common.base.Strings;
sangho6a9ff0d2017-03-27 11:23:37 +090019import org.onosproject.core.CoreService;
20import org.onosproject.event.ListenerRegistry;
21import org.onosproject.openstacknetworking.api.Constants;
22import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
23import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent;
24import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener;
25import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService;
26import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStore;
27import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStoreDelegate;
28import org.openstack4j.model.network.SecurityGroup;
29import org.openstack4j.model.network.SecurityGroupRule;
30import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
sangho6a9ff0d2017-03-27 11:23:37 +090036import org.slf4j.Logger;
37
38import java.util.List;
Hyunsun Moonae51e732017-04-25 17:46:21 +090039import java.util.Objects;
40import java.util.Set;
sangho6a9ff0d2017-03-27 11:23:37 +090041
42import static com.google.common.base.Preconditions.checkArgument;
43import static com.google.common.base.Preconditions.checkNotNull;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
Hyunsun Moonae51e732017-04-25 17:46:21 +090047 * Provides implementation of administering and interfacing OpenStack security
sangho6a9ff0d2017-03-27 11:23:37 +090048 * groups.
sangho6a9ff0d2017-03-27 11:23:37 +090049 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050@Component(immediate = true,
51 service = { OpenstackSecurityGroupAdminService.class, OpenstackSecurityGroupService.class })
sangho6a9ff0d2017-03-27 11:23:37 +090052public class OpenstackSecurityGroupManager
53 extends ListenerRegistry<OpenstackSecurityGroupEvent, OpenstackSecurityGroupListener>
54 implements OpenstackSecurityGroupAdminService, OpenstackSecurityGroupService {
55
56 protected final Logger log = getLogger(getClass());
57
58 private static final String MSG_SG = "OpenStack security group %s %s";
Hyunsun Moonae51e732017-04-25 17:46:21 +090059 private static final String MSG_SG_RULE = "OpenStack security group rule %s %s";
sangho6a9ff0d2017-03-27 11:23:37 +090060
61 private static final String MSG_CREATED = "created";
62 private static final String MSG_REMOVED = "removed";
63
64 private static final String ERR_NULL_SG = "OpenStack security group cannot be null";
65 private static final String ERR_NULL_SG_ID = "OpenStack security group ID cannot be null";
66 private static final String ERR_NULL_SG_RULE = "OpenStack security group rule cannot be null";
67 private static final String ERR_NULL_SG_RULE_ID = "OpenStack security group rule ID cannot be null";
Hyunsun Moonae51e732017-04-25 17:46:21 +090068 private static final String ERR_NOT_FOUND = "not found";
69 private static final String ERR_DUPLICATE = "already exist";
sangho6a9ff0d2017-03-27 11:23:37 +090070
sanghoe6457a32017-08-24 14:31:19 +090071 private boolean useSecurityGroup = false;
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangho6a9ff0d2017-03-27 11:23:37 +090074 protected CoreService coreService;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sangho6a9ff0d2017-03-27 11:23:37 +090077 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 Moonae51e732017-04-25 17:46:21 +0900104 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
sangho6a9ff0d2017-03-27 11:23:37 +0900112 public void removeSecurityGroup(String sgId) {
Jian Li43e066b2018-07-16 17:43:56 +0900113 checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900114
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 Moonae51e732017-04-25 17:46:21 +0900123 checkArgument(!Strings.isNullOrEmpty(sgRule.getSecurityGroupId()), ERR_NULL_SG_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900124
Hyunsun Moonae51e732017-04-25 17:46:21 +0900125 synchronized (this) {
sangho6a9ff0d2017-03-27 11:23:37 +0900126 SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId());
Hyunsun Moonae51e732017-04-25 17:46:21 +0900127 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);
sangho6a9ff0d2017-03-27 11:23:37 +0900135 }
136
Hyunsun Moonae51e732017-04-25 17:46:21 +0900137 // 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);
sangho6a9ff0d2017-03-27 11:23:37 +0900142 }
Hyunsun Moonae51e732017-04-25 17:46:21 +0900143
144 log.info(String.format(MSG_SG_RULE, sgRule.getId(), MSG_CREATED));
sangho6a9ff0d2017-03-27 11:23:37 +0900145 }
146
147 @Override
148 public void removeSecurityGroupRule(String sgRuleId) {
Hyunsun Moonae51e732017-04-25 17:46:21 +0900149 checkArgument(!Strings.isNullOrEmpty(sgRuleId), ERR_NULL_SG_RULE_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900150
Hyunsun Moonae51e732017-04-25 17:46:21 +0900151 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
sangho6a9ff0d2017-03-27 11:23:37 +0900177 log.info(String.format(MSG_SG_RULE, sgRuleId, MSG_REMOVED));
178 }
179
180 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900181 public Set<SecurityGroup> securityGroups() {
182 return osSecurityGroupStore.securityGroups();
183 }
184
185 @Override
sangho6a9ff0d2017-03-27 11:23:37 +0900186 public SecurityGroup securityGroup(String sgId) {
187 checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID);
188 return osSecurityGroupStore.securityGroup(sgId);
189 }
190
191 @Override
sanghoe6457a32017-08-24 14:31:19 +0900192 public boolean isSecurityGroupEnabled() {
193 return useSecurityGroup;
194 }
195
196 @Override
197 public void setSecurityGroupEnabled(boolean option) {
198 useSecurityGroup = option;
199 }
200
201 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900202 public void clear() {
203 osSecurityGroupStore.clear();
204 }
205
206 private SecurityGroupRule securityGroupRule(String sgRuleId) {
207 return osSecurityGroupStore.securityGroups().stream()
208 .flatMap(sg -> sg.getRules().stream())
209 .filter(sgRule -> Objects.equals(sgRule.getId(), sgRuleId))
210 .findFirst().orElse(null);
sangho6a9ff0d2017-03-27 11:23:37 +0900211 }
212
213 private class InternalSecurityGroupStoreDelegate implements OpenstackSecurityGroupStoreDelegate {
214
215 @Override
216 public void notify(OpenstackSecurityGroupEvent event) {
217 if (event != null) {
218 log.trace("send openstack security group event {}", event);
219 process(event);
220 }
221 }
222 }
223}