blob: bfad9ed90bb9fdeb3b6bdba8bbece34ebb6c2a8b [file] [log] [blame]
Hyunsun Moon44aac662017-02-18 02:07:01 +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.api;
17
18import org.joda.time.LocalDateTime;
19import org.onosproject.event.AbstractEvent;
20import org.openstack4j.model.network.Network;
21import org.openstack4j.model.network.Port;
22import org.openstack4j.model.network.Subnet;
23
sangho6a9ff0d2017-03-27 11:23:37 +090024import java.util.Collection;
25
Hyunsun Moon44aac662017-02-18 02:07:01 +090026import static com.google.common.base.MoreObjects.toStringHelper;
27
28/**
29 * Describes OpenStack network service event.
30 */
31public class OpenstackNetworkEvent extends AbstractEvent<OpenstackNetworkEvent.Type, Network> {
32
33 private final Port port;
34 private final Subnet subnet;
sangho6a9ff0d2017-03-27 11:23:37 +090035 private final Collection<String> sgRuleIds;
Hyunsun Moon44aac662017-02-18 02:07:01 +090036
37 public enum Type {
38 /**
39 * Signifies that a new OpenStack network is created.
40 */
41 OPENSTACK_NETWORK_CREATED,
42
43 /**
44 * Signifies that the OpenStack network is updated.
45 */
46 OPENSTACK_NETWORK_UPDATED,
47
48 /**
49 * Signifies that the OpenStack network is removed.
50 */
51 OPENSTACK_NETWORK_REMOVED,
52
53 /**
54 * Signifies that a new OpenStack subnet is created.
55 */
56 OPENSTACK_SUBNET_CREATED,
57
58 /**
59 * Signifies that the OpenStack subnet is updated.
60 */
61 OPENSTACK_SUBNET_UPDATED,
62
63 /**
64 * Signifies that the OpenStack subnet is removed.
65 */
66 OPENSTACK_SUBNET_REMOVED,
67
68 /**
69 * Signifies that a new OpenStack port is created.
70 */
71 OPENSTACK_PORT_CREATED,
72
73 /**
74 * Signifies that the OpenStack port is updated.
75 */
76 OPENSTACK_PORT_UPDATED,
77
78 /**
79 * Signifies that the OpenStack port is removed.
80 */
sangho6a9ff0d2017-03-27 11:23:37 +090081 OPENSTACK_PORT_REMOVED,
82
83 /**
84 * Signifies that the OpenStack security group rule is added to a specific port.
85 */
86 OPENSTACK_SECURITY_GROUP_ADDED_TO_PORT,
87
88 /**
89 * Signifies that the OpenStack security group rule is removed from a specific port.
90 */
91 OPENSTACK_SECURITY_GROUP_REMOVED_FROM_PORT
Hyunsun Moon44aac662017-02-18 02:07:01 +090092 }
93
94 /**
95 * Creates an event of a given type for the specified network and the current time.
96 * @param type openstack network event type
97 * @param network openstack network
98 */
99 public OpenstackNetworkEvent(Type type, Network network) {
100 super(type, network);
101 this.port = null;
102 this.subnet = null;
sangho6a9ff0d2017-03-27 11:23:37 +0900103 this.sgRuleIds = null;
Hyunsun Moon44aac662017-02-18 02:07:01 +0900104 }
105
106 /**
107 * Creates an event of a given type for the specified network, port and the
108 * current time.
109 *
110 * @param type openstack network event type
111 * @param network openstack network
112 * @param port openstack port
113 */
114 public OpenstackNetworkEvent(Type type, Network network, Port port) {
115 super(type, network);
116 this.port = port;
117 this.subnet = null;
sangho6a9ff0d2017-03-27 11:23:37 +0900118 this.sgRuleIds = null;
Hyunsun Moon44aac662017-02-18 02:07:01 +0900119 }
120
121 /**
122 * Creates an event of a given type for the specified network, subnet and the
123 * current time.
124 *
125 * @param type openstack network event type
126 * @param network openstack network
127 * @param subnet openstack subnet
128 */
129 public OpenstackNetworkEvent(Type type, Network network, Subnet subnet) {
130 super(type, network);
131 this.port = null;
132 this.subnet = subnet;
sangho6a9ff0d2017-03-27 11:23:37 +0900133 this.sgRuleIds = null;
134 }
135
136 /**
137 * Creates an event of a given type for the specified port and security groups.
138 *
139 * @param type openstack network event type
140 * @param sgRuleIds openstack security group rules
141 * @param port openstack port
142 */
143 public OpenstackNetworkEvent(Type type, Collection<String> sgRuleIds, Port port) {
144 super(type, null);
145 this.port = port;
146 this.sgRuleIds = sgRuleIds;
147 this.subnet = null;
Hyunsun Moon44aac662017-02-18 02:07:01 +0900148 }
149
150 /**
151 * Returns the port of the network event.
152 *
153 * @return openstack port; null if the event is not port specific
154 */
155 public Port port() {
156 return port;
157 }
158
159 /**
160 * Returns the subnet of the network event.
161 *
162 * @return openstack subnet; null if the event is not subnet specific
163 */
164 public Subnet subnet() {
165 return subnet;
166 }
167
sangho6a9ff0d2017-03-27 11:23:37 +0900168 /**
169 * Returns the security group rule IDs updated.
170 *
171 * @return collection of security group rule ID
172 */
173 public Collection<String> securityGroupRuleIds() {
174 return sgRuleIds;
175 }
176
Hyunsun Moon44aac662017-02-18 02:07:01 +0900177 @Override
178 public String toString() {
179 if (port == null && subnet == null) {
180 return super.toString();
181 }
182 return toStringHelper(this)
183 .add("time", new LocalDateTime(time()))
184 .add("type", type())
185 .add("network", subject())
186 .add("port", port)
187 .add("subnet", subnet)
sangho6a9ff0d2017-03-27 11:23:37 +0900188 .add("security group rules", securityGroupRuleIds())
Hyunsun Moon44aac662017-02-18 02:07:01 +0900189 .toString();
190 }
191}