Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 19 | import org.onlab.util.KryoNamespace; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
| 22 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 23 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 24 | import org.onosproject.openstacknetworking.api.InstancePortStore; |
| 25 | import org.onosproject.openstacknetworking.api.InstancePortStoreDelegate; |
| 26 | import org.onosproject.store.AbstractStore; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.ConsistentMap; |
| 29 | import org.onosproject.store.service.MapEvent; |
| 30 | import org.onosproject.store.service.MapEventListener; |
| 31 | import org.onosproject.store.service.Serializer; |
| 32 | import org.onosproject.store.service.StorageService; |
| 33 | import org.onosproject.store.service.Versioned; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 34 | import org.osgi.service.component.annotations.Activate; |
| 35 | import org.osgi.service.component.annotations.Component; |
| 36 | import org.osgi.service.component.annotations.Deactivate; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.util.Set; |
| 42 | import java.util.concurrent.ExecutorService; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkArgument; |
| 45 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 46 | import static org.onlab.util.Tools.groupedThreads; |
| 47 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
| 48 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
| 49 | import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 50 | import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATED; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 51 | import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATING; |
Jian Li | 0488c73 | 2018-09-14 20:53:07 +0900 | [diff] [blame] | 52 | import static org.onosproject.openstacknetworking.api.InstancePort.State.REMOVE_PENDING; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 53 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_MIGRATION_ENDED; |
| 54 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_MIGRATION_STARTED; |
| 55 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_DETECTED; |
| 56 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_UPDATED; |
| 57 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_VANISHED; |
| 58 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_RESTARTED; |
| 59 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_TERMINATED; |
| 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
| 62 | /** |
| 63 | * Manages the inventory of openstack instance port using a {@code ConsistentMap}. |
| 64 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | @Component(immediate = true, service = InstancePortStore.class) |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 66 | public class DistributedInstancePortStore |
| 67 | extends AbstractStore<InstancePortEvent, InstancePortStoreDelegate> |
| 68 | implements InstancePortStore { |
| 69 | |
| 70 | protected final Logger log = getLogger(getClass()); |
| 71 | |
| 72 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 73 | private static final String ERR_DUPLICATE = " already exists"; |
| 74 | |
| 75 | private static final KryoNamespace SERIALIZER_INSTANCE_PORT = KryoNamespace.newBuilder() |
| 76 | .register(KryoNamespaces.API) |
| 77 | .register(InstancePort.class) |
| 78 | .register(DefaultInstancePort.class) |
| 79 | .register(InstancePort.State.class) |
| 80 | .build(); |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 83 | protected CoreService coreService; |
| 84 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 86 | protected StorageService storageService; |
| 87 | |
| 88 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 89 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 90 | |
| 91 | private final MapEventListener<String, InstancePort> |
| 92 | instancePortMapListener = new InstancePortMapListener(); |
| 93 | |
| 94 | private ConsistentMap<String, InstancePort> instancePortStore; |
| 95 | |
| 96 | @Activate |
| 97 | protected void activate() { |
| 98 | ApplicationId appId = coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 99 | |
| 100 | instancePortStore = storageService.<String, InstancePort>consistentMapBuilder() |
| 101 | .withSerializer(Serializer.using(SERIALIZER_INSTANCE_PORT)) |
| 102 | .withName("openstack-instanceport-store") |
| 103 | .withApplicationId(appId) |
| 104 | .build(); |
| 105 | instancePortStore.addListener(instancePortMapListener); |
| 106 | |
| 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | protected void deactivate() { |
| 112 | instancePortStore.removeListener(instancePortMapListener); |
| 113 | eventExecutor.shutdown(); |
| 114 | |
| 115 | log.info("Stopped"); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void createInstancePort(InstancePort port) { |
| 120 | instancePortStore.compute(port.portId(), (id, existing) -> { |
| 121 | final String error = port.portId() + ERR_DUPLICATE; |
| 122 | checkArgument(existing == null, error); |
| 123 | return port; |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void updateInstancePort(InstancePort port) { |
| 129 | instancePortStore.compute(port.portId(), (id, existing) -> { |
| 130 | final String error = port.portId() + ERR_NOT_FOUND; |
| 131 | checkArgument(existing != null, error); |
| 132 | return port; |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public InstancePort removeInstancePort(String portId) { |
| 138 | Versioned<InstancePort> port = instancePortStore.remove(portId); |
| 139 | return port == null ? null : port.value(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public InstancePort instancePort(String portId) { |
| 144 | return instancePortStore.asJavaMap().get(portId); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Set<InstancePort> instancePorts() { |
| 149 | return ImmutableSet.copyOf(instancePortStore.asJavaMap().values()); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void clear() { |
| 154 | instancePortStore.clear(); |
| 155 | } |
| 156 | |
| 157 | private class InstancePortMapListener implements MapEventListener<String, InstancePort> { |
| 158 | |
| 159 | @Override |
| 160 | public void event(MapEvent<String, InstancePort> event) { |
| 161 | switch (event.type()) { |
| 162 | case INSERT: |
| 163 | log.debug("Instance port created"); |
| 164 | eventExecutor.execute(() -> |
| 165 | notifyDelegate(new InstancePortEvent( |
| 166 | OPENSTACK_INSTANCE_PORT_DETECTED, |
| 167 | event.newValue().value())) |
| 168 | ); |
| 169 | break; |
| 170 | case UPDATE: |
| 171 | log.debug("Instance port updated"); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 172 | eventExecutor.execute(() -> processInstancePortUpdate(event)); |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 173 | break; |
| 174 | case REMOVE: |
| 175 | log.debug("Instance port removed"); |
| 176 | eventExecutor.execute(() -> |
| 177 | notifyDelegate(new InstancePortEvent( |
| 178 | OPENSTACK_INSTANCE_PORT_VANISHED, |
| 179 | event.oldValue().value())) |
| 180 | ); |
| 181 | break; |
| 182 | default: |
| 183 | log.error("Unsupported instance port event type"); |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private void processInstancePortUpdate(MapEvent<String, InstancePort> event) { |
| 189 | InstancePort.State oldState = event.oldValue().value().state(); |
| 190 | InstancePort.State newState = event.newValue().value().state(); |
| 191 | |
Jian Li | ee8214a | 2018-07-21 20:07:28 +0900 | [diff] [blame] | 192 | if ((oldState == ACTIVE || oldState == INACTIVE) && newState == MIGRATING) { |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 193 | notifyDelegate(new InstancePortEvent( |
| 194 | OPENSTACK_INSTANCE_MIGRATION_STARTED, |
| 195 | event.newValue().value())); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 196 | return; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 197 | } |
| 198 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 199 | if (oldState == MIGRATING && newState == MIGRATED) { |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 200 | notifyDelegate(new InstancePortEvent( |
| 201 | OPENSTACK_INSTANCE_MIGRATION_ENDED, |
| 202 | event.newValue().value())); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 203 | updateInstancePort(event.newValue().value().updateState(ACTIVE)); |
| 204 | return; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (oldState == ACTIVE && newState == INACTIVE) { |
| 208 | notifyDelegate(new InstancePortEvent( |
| 209 | OPENSTACK_INSTANCE_TERMINATED, |
| 210 | event.newValue().value())); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 211 | return; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (oldState == INACTIVE && newState == ACTIVE) { |
| 215 | notifyDelegate(new InstancePortEvent( |
| 216 | OPENSTACK_INSTANCE_RESTARTED, |
| 217 | event.newValue().value())); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 218 | return; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 219 | } |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 220 | |
| 221 | // this should be auto-transition |
| 222 | if (oldState == MIGRATED && newState == ACTIVE) { |
| 223 | return; |
| 224 | } |
| 225 | |
Jian Li | 0488c73 | 2018-09-14 20:53:07 +0900 | [diff] [blame] | 226 | // we do not trigger instance port update for pending state transition |
| 227 | if (newState == REMOVE_PENDING) { |
| 228 | return; |
| 229 | } |
| 230 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 231 | notifyDelegate(new InstancePortEvent( |
| 232 | OPENSTACK_INSTANCE_PORT_UPDATED, |
| 233 | event.newValue().value())); |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |