blob: 678d1805d6917f53301e16f9d0610d21d17dae7d [file] [log] [blame]
sangho6a9ff0d2017-03-27 11:23:37 +09001/*
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 */
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;
41
42import static com.google.common.base.Preconditions.checkArgument;
43import static com.google.common.base.Preconditions.checkNotNull;
44import 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)
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";
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}