Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +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.opencord.cordvtn.api.core; |
| 17 | |
| 18 | import org.joda.time.LocalDateTime; |
| 19 | import org.onosproject.event.AbstractEvent; |
| 20 | import org.opencord.cordvtn.api.net.Provider; |
| 21 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 22 | import org.opencord.cordvtn.api.net.ServicePort; |
| 23 | |
| 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 25 | |
| 26 | /** |
| 27 | * Describes service network event. |
| 28 | */ |
| 29 | public class ServiceNetworkEvent extends AbstractEvent<ServiceNetworkEvent.Type, ServiceNetwork> { |
| 30 | |
| 31 | private final ServicePort servicePort; |
| 32 | private final Provider provider; |
| 33 | |
| 34 | /** |
| 35 | * Type of service network event. |
| 36 | */ |
| 37 | public enum Type { |
| 38 | /** |
| 39 | * Signifies that a new service network has been created. |
| 40 | */ |
| 41 | SERVICE_NETWORK_CREATED, |
| 42 | |
| 43 | /** |
| 44 | * Signifies that some service network attributes have changed. |
| 45 | */ |
| 46 | SERVICE_NETWORK_UPDATED, |
| 47 | |
| 48 | /** |
| 49 | * Signifies that provider network was added. |
| 50 | */ |
| 51 | SERVICE_NETWORK_PROVIDER_ADDED, |
| 52 | |
| 53 | /** |
| 54 | * Signifies that provider network was removed. |
| 55 | */ |
| 56 | SERVICE_NETWORK_PROVIDER_REMOVED, |
| 57 | |
| 58 | /** |
| 59 | * Signifies that a service network has been removed. |
| 60 | */ |
| 61 | SERVICE_NETWORK_REMOVED, |
| 62 | |
| 63 | /** |
| 64 | * Signifies that a new service port has been created. |
| 65 | */ |
| 66 | SERVICE_PORT_CREATED, |
| 67 | |
| 68 | /** |
| 69 | * Signifies that some service port attributes have changed. |
| 70 | */ |
| 71 | SERVICE_PORT_UPDATED, |
| 72 | |
| 73 | /** |
| 74 | * Signifies that a service port has been removed. |
| 75 | */ |
| 76 | SERVICE_PORT_REMOVED |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Creates an event of a given type and for the specified service network and |
| 81 | * the current time. |
| 82 | * |
| 83 | * @param type service network event type |
| 84 | * @param serviceNetwork service network subject |
| 85 | */ |
| 86 | public ServiceNetworkEvent(Type type, ServiceNetwork serviceNetwork) { |
| 87 | super(type, serviceNetwork); |
| 88 | this.servicePort = null; |
| 89 | this.provider = null; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates an event of a given type and for the specified service network, |
| 94 | * port and the current time. |
| 95 | * |
| 96 | * @param type service network event type |
| 97 | * @param serviceNetwork service network subject |
| 98 | * @param servicePort optional service port subject |
| 99 | */ |
| 100 | public ServiceNetworkEvent(Type type, ServiceNetwork serviceNetwork, ServicePort servicePort) { |
| 101 | super(type, serviceNetwork); |
| 102 | this.servicePort = servicePort; |
| 103 | this.provider = null; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Creates an event of a given type and for the specified service network, |
| 108 | * provider, dependency type with the provider, and the current time. |
| 109 | * |
| 110 | * @param type service network event type |
| 111 | * @param serviceNetwork service network subject |
| 112 | * @param provider optional provider network |
| 113 | */ |
| 114 | public ServiceNetworkEvent(Type type, ServiceNetwork serviceNetwork, Provider provider) { |
| 115 | super(type, serviceNetwork); |
| 116 | this.servicePort = null; |
| 117 | this.provider = provider; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the service port subject. |
| 122 | * It returns valid value only with the service port events. |
| 123 | * |
| 124 | * @return service port; null if the event is not service port specific |
| 125 | */ |
| 126 | public ServicePort servicePort() { |
| 127 | return servicePort; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the provider of the service network. |
| 132 | * |
| 133 | * @return provider network; null if the event is not provider specific |
| 134 | */ |
| 135 | public Provider provider() { |
| 136 | return provider; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public String toString() { |
| 141 | if (servicePort == null) { |
| 142 | return super.toString(); |
| 143 | } |
| 144 | return toStringHelper(this) |
| 145 | .add("time", new LocalDateTime(time())) |
| 146 | .add("type", type()) |
| 147 | .add("serviceNetwork", subject()) |
| 148 | .add("servicePort", servicePort) |
| 149 | .add("provider", provider) |
| 150 | .toString(); |
| 151 | } |
| 152 | } |