Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.api; |
| 17 | |
| 18 | import org.joda.time.LocalDateTime; |
| 19 | import org.onosproject.event.AbstractEvent; |
| 20 | import org.openstack4j.model.network.Network; |
| 21 | import org.openstack4j.model.network.Port; |
| 22 | import org.openstack4j.model.network.Subnet; |
| 23 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 24 | import java.util.Collection; |
| 25 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * Describes OpenStack network service event. |
| 30 | */ |
| 31 | public class OpenstackNetworkEvent extends AbstractEvent<OpenstackNetworkEvent.Type, Network> { |
| 32 | |
| 33 | private final Port port; |
| 34 | private final Subnet subnet; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 35 | private final Collection<String> sgRuleIds; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 36 | |
| 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 | */ |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 81 | 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 Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 92 | } |
| 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; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 103 | this.sgRuleIds = null; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 104 | } |
| 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; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 118 | this.sgRuleIds = null; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 119 | } |
| 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; |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 133 | 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 Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 148 | } |
| 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 | |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 168 | /** |
| 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 Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 177 | @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) |
sangho | 6a9ff0d | 2017-03-27 11:23:37 +0900 | [diff] [blame^] | 188 | .add("security group rules", securityGroupRuleIds()) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 189 | .toString(); |
| 190 | } |
| 191 | } |