Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 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.openstacknode.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.openstacknode.api.NodeState; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 31 | import org.onosproject.openstacknode.api.OpenstackNodeStore; |
| 32 | import org.onosproject.openstacknode.api.OpenstackNodeStoreDelegate; |
| 33 | import org.onosproject.store.AbstractStore; |
| 34 | import org.onosproject.store.serializers.KryoNamespaces; |
| 35 | import org.onosproject.store.service.ConsistentMap; |
| 36 | import org.onosproject.store.service.MapEvent; |
| 37 | import org.onosproject.store.service.MapEventListener; |
| 38 | import org.onosproject.store.service.Serializer; |
| 39 | import org.onosproject.store.service.StorageService; |
| 40 | import org.onosproject.store.service.Versioned; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Set; |
| 44 | import java.util.concurrent.ExecutorService; |
| 45 | import java.util.stream.Collectors; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkArgument; |
| 48 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 49 | import static org.onlab.util.Tools.groupedThreads; |
| 50 | import static org.onosproject.openstacknode.api.NodeState.COMPLETE; |
| 51 | import static org.onosproject.openstacknode.api.NodeState.INCOMPLETE; |
| 52 | import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.*; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * Implementation of openstack node store using consistent map. |
| 57 | */ |
| 58 | @Service |
| 59 | @Component(immediate = true) |
| 60 | public class DistributedOpenstackNodeStore |
| 61 | extends AbstractStore<OpenstackNodeEvent, OpenstackNodeStoreDelegate> |
| 62 | implements OpenstackNodeStore { |
| 63 | |
| 64 | protected final Logger log = getLogger(getClass()); |
| 65 | |
| 66 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 67 | private static final String ERR_DUPLICATE = " already exists"; |
| 68 | |
| 69 | private static final KryoNamespace SERIALIZER_OPENSTACK_NODE = KryoNamespace.newBuilder() |
| 70 | .register(KryoNamespaces.API) |
| 71 | .register(OpenstackNode.class) |
| 72 | .register(DefaultOpenstackNode.class) |
| 73 | .register(OpenstackNode.NodeType.class) |
| 74 | .register(NodeState.class) |
| 75 | .build(); |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected CoreService coreService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected StorageService storageService; |
| 82 | |
| 83 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 84 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 85 | |
| 86 | private final MapEventListener<String, OpenstackNode> osNodeMapListener = |
| 87 | new OpenstackNodeMapListener(); |
| 88 | private ConsistentMap<String, OpenstackNode> osNodeStore; |
| 89 | |
| 90 | @Activate |
| 91 | protected void activate() { |
| 92 | ApplicationId appId = coreService.registerApplication("org.onosproject.openstacknode"); |
| 93 | osNodeStore = storageService.<String, OpenstackNode>consistentMapBuilder() |
| 94 | .withSerializer(Serializer.using(SERIALIZER_OPENSTACK_NODE)) |
| 95 | .withName("openstack-nodestore") |
| 96 | .withApplicationId(appId) |
| 97 | .build(); |
| 98 | osNodeStore.addListener(osNodeMapListener); |
| 99 | log.info("Started"); |
| 100 | } |
| 101 | |
| 102 | @Deactivate |
| 103 | protected void deactivate() { |
| 104 | osNodeStore.removeListener(osNodeMapListener); |
| 105 | eventExecutor.shutdown(); |
| 106 | log.info("Stopped"); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void createNode(OpenstackNode osNode) { |
| 111 | osNodeStore.compute(osNode.hostname(), (hostname, existing) -> { |
| 112 | final String error = osNode.hostname() + ERR_DUPLICATE; |
| 113 | checkArgument(existing == null, error); |
| 114 | return osNode; |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void updateNode(OpenstackNode osNode) { |
| 120 | osNodeStore.compute(osNode.hostname(), (hostname, existing) -> { |
| 121 | final String error = osNode.hostname() + ERR_NOT_FOUND; |
| 122 | checkArgument(existing != null, error); |
| 123 | return osNode; |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public OpenstackNode removeNode(String hostname) { |
| 129 | Versioned<OpenstackNode> osNode = osNodeStore.remove(hostname); |
| 130 | if (osNode == null) { |
| 131 | final String error = hostname + ERR_NOT_FOUND; |
| 132 | throw new IllegalArgumentException(error); |
| 133 | } |
| 134 | return osNode.value(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Set<OpenstackNode> nodes() { |
| 139 | Set<OpenstackNode> osNodes = osNodeStore.values().stream() |
| 140 | .map(Versioned::value) |
| 141 | .collect(Collectors.toSet()); |
| 142 | return ImmutableSet.copyOf(osNodes); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public OpenstackNode node(String hostname) { |
| 147 | Versioned<OpenstackNode> osNode = osNodeStore.get(hostname); |
| 148 | return osNode == null ? null : osNode.value(); |
| 149 | } |
| 150 | |
Jian Li | 340165f | 2018-02-27 10:38:17 +0900 | [diff] [blame^] | 151 | /** |
| 152 | * An internal openstack node map listener. |
| 153 | */ |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 154 | private class OpenstackNodeMapListener implements MapEventListener<String, OpenstackNode> { |
| 155 | |
| 156 | @Override |
| 157 | public void event(MapEvent<String, OpenstackNode> event) { |
| 158 | switch (event.type()) { |
| 159 | case INSERT: |
| 160 | log.debug("OpenStack node created {}", event.newValue()); |
| 161 | eventExecutor.execute(() -> { |
| 162 | notifyDelegate(new OpenstackNodeEvent( |
| 163 | OPENSTACK_NODE_CREATED, |
| 164 | event.newValue().value() |
| 165 | )); |
| 166 | }); |
| 167 | break; |
| 168 | case UPDATE: |
| 169 | log.debug("OpenStack node updated {}", event.newValue()); |
| 170 | eventExecutor.execute(() -> { |
| 171 | notifyDelegate(new OpenstackNodeEvent( |
| 172 | OPENSTACK_NODE_UPDATED, |
| 173 | event.newValue().value() |
| 174 | )); |
| 175 | if (event.newValue().value().state() == COMPLETE) { |
| 176 | notifyDelegate(new OpenstackNodeEvent( |
| 177 | OPENSTACK_NODE_COMPLETE, |
| 178 | event.newValue().value() |
| 179 | )); |
| 180 | } else if (event.newValue().value().state() == INCOMPLETE) { |
| 181 | notifyDelegate(new OpenstackNodeEvent( |
| 182 | OPENSTACK_NODE_INCOMPLETE, |
| 183 | event.newValue().value() |
| 184 | )); |
| 185 | } |
| 186 | }); |
| 187 | break; |
| 188 | case REMOVE: |
| 189 | log.debug("OpenStack node removed {}", event.oldValue()); |
| 190 | eventExecutor.execute(() -> { |
| 191 | notifyDelegate(new OpenstackNodeEvent( |
| 192 | OPENSTACK_NODE_REMOVED, |
| 193 | event.oldValue().value() |
| 194 | )); |
| 195 | }); |
| 196 | break; |
| 197 | default: |
| 198 | // do nothing |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |