blob: 2e5891dc78fc9ccbe4d457ca0a257a3ed4229cc0 [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
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 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) {
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 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
Hyunsun Moonae51e732017-04-25 17:46:21 +0900192 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);
sangho6a9ff0d2017-03-27 11:23:37 +0900201 }
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}