blob: 03e1e985497cf242aa6f81cffea29764a44994b2 [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;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.core.CoreService;
26import org.onosproject.event.ListenerRegistry;
27import org.onosproject.openstacknetworking.api.Constants;
28import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
29import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupEvent;
30import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupListener;
31import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupService;
32import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStore;
33import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupStoreDelegate;
34import org.openstack4j.model.network.SecurityGroup;
35import org.openstack4j.model.network.SecurityGroupRule;
36import org.openstack4j.openstack.networking.domain.NeutronSecurityGroup;
37import org.slf4j.Logger;
38
39import java.util.List;
Hyunsun Moonae51e732017-04-25 17:46:21 +090040import java.util.Objects;
41import java.util.Set;
sangho6a9ff0d2017-03-27 11:23:37 +090042
43import static com.google.common.base.Preconditions.checkArgument;
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
Hyunsun Moonae51e732017-04-25 17:46:21 +090048 * Provides implementation of administering and interfacing OpenStack security
sangho6a9ff0d2017-03-27 11:23:37 +090049 * groups.
sangho6a9ff0d2017-03-27 11:23:37 +090050 */
51@Service
52@Component(immediate = true)
53public 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 Moonae51e732017-04-25 17:46:21 +090060 private static final String MSG_SG_RULE = "OpenStack security group rule %s %s";
sangho6a9ff0d2017-03-27 11:23:37 +090061
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 Moonae51e732017-04-25 17:46:21 +090069 private static final String ERR_NOT_FOUND = "not found";
70 private static final String ERR_DUPLICATE = "already exist";
sangho6a9ff0d2017-03-27 11:23:37 +090071
sanghoe6457a32017-08-24 14:31:19 +090072 private boolean useSecurityGroup = false;
73
sangho6a9ff0d2017-03-27 11:23:37 +090074 @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 Moonae51e732017-04-25 17:46:21 +0900105 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
sangho6a9ff0d2017-03-27 11:23:37 +0900113 public void removeSecurityGroup(String sgId) {
Jian Li43e066b2018-07-16 17:43:56 +0900114 checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900115
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 Moonae51e732017-04-25 17:46:21 +0900124 checkArgument(!Strings.isNullOrEmpty(sgRule.getSecurityGroupId()), ERR_NULL_SG_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900125
Hyunsun Moonae51e732017-04-25 17:46:21 +0900126 synchronized (this) {
sangho6a9ff0d2017-03-27 11:23:37 +0900127 SecurityGroup sg = securityGroup(sgRule.getSecurityGroupId());
Hyunsun Moonae51e732017-04-25 17:46:21 +0900128 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);
sangho6a9ff0d2017-03-27 11:23:37 +0900136 }
137
Hyunsun Moonae51e732017-04-25 17:46:21 +0900138 // 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);
sangho6a9ff0d2017-03-27 11:23:37 +0900143 }
Hyunsun Moonae51e732017-04-25 17:46:21 +0900144
145 log.info(String.format(MSG_SG_RULE, sgRule.getId(), MSG_CREATED));
sangho6a9ff0d2017-03-27 11:23:37 +0900146 }
147
148 @Override
149 public void removeSecurityGroupRule(String sgRuleId) {
Hyunsun Moonae51e732017-04-25 17:46:21 +0900150 checkArgument(!Strings.isNullOrEmpty(sgRuleId), ERR_NULL_SG_RULE_ID);
sangho6a9ff0d2017-03-27 11:23:37 +0900151
Hyunsun Moonae51e732017-04-25 17:46:21 +0900152 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
sangho6a9ff0d2017-03-27 11:23:37 +0900178 log.info(String.format(MSG_SG_RULE, sgRuleId, MSG_REMOVED));
179 }
180
181 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900182 public Set<SecurityGroup> securityGroups() {
183 return osSecurityGroupStore.securityGroups();
184 }
185
186 @Override
sangho6a9ff0d2017-03-27 11:23:37 +0900187 public SecurityGroup securityGroup(String sgId) {
188 checkArgument(!Strings.isNullOrEmpty(sgId), ERR_NULL_SG_ID);
189 return osSecurityGroupStore.securityGroup(sgId);
190 }
191
192 @Override
sanghoe6457a32017-08-24 14:31:19 +0900193 public boolean isSecurityGroupEnabled() {
194 return useSecurityGroup;
195 }
196
197 @Override
198 public void setSecurityGroupEnabled(boolean option) {
199 useSecurityGroup = option;
200 }
201
202 @Override
Hyunsun Moonae51e732017-04-25 17:46:21 +0900203 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);
sangho6a9ff0d2017-03-27 11:23:37 +0900212 }
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}