Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +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.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.store.AbstractStore; |
| 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.ConsistentMap; |
| 31 | import org.onosproject.store.service.MapEvent; |
| 32 | import org.onosproject.store.service.MapEventListener; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.Versioned; |
| 36 | import org.opencord.cordvtn.api.net.CidrAddr; |
| 37 | import org.opencord.cordvtn.api.node.CordVtnNode; |
| 38 | import org.opencord.cordvtn.api.node.CordVtnNodeEvent; |
| 39 | import org.opencord.cordvtn.api.node.CordVtnNodeHandler; |
| 40 | import org.opencord.cordvtn.api.node.CordVtnNodeState; |
| 41 | import org.opencord.cordvtn.api.node.CordVtnNodeStore; |
| 42 | import org.opencord.cordvtn.api.node.CordVtnNodeStoreDelegate; |
| 43 | import org.opencord.cordvtn.api.node.SshAccessInfo; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import java.util.Set; |
| 47 | import java.util.concurrent.ExecutorService; |
| 48 | import java.util.stream.Collectors; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkArgument; |
| 51 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 52 | import static org.onlab.util.Tools.groupedThreads; |
| 53 | import static org.opencord.cordvtn.api.Constants.CORDVTN_APP_ID; |
| 54 | import static org.opencord.cordvtn.api.node.CordVtnNodeState.COMPLETE; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | /** |
| 58 | * Manages the inventory of cordvtn nodes using a {@link ConsistentMap}. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
| 62 | public class DistributedCordVtnNodeStore extends AbstractStore<CordVtnNodeEvent, CordVtnNodeStoreDelegate> |
| 63 | implements CordVtnNodeStore { |
| 64 | |
| 65 | protected final Logger log = getLogger(getClass()); |
| 66 | |
| 67 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 68 | private static final String ERR_DUPLICATE = " already exists"; |
| 69 | |
| 70 | private static final KryoNamespace SERIALIZER_CORDVTN_NODE = KryoNamespace.newBuilder() |
| 71 | .register(KryoNamespaces.API) |
| 72 | .register(CordVtnNode.class) |
| 73 | .register(DefaultCordVtnNode.class) |
| 74 | .register(CidrAddr.class) |
| 75 | .register(SshAccessInfo.class) |
| 76 | .register(CordVtnNodeState.class) |
| 77 | .register(CordVtnNodeHandler.class) |
| 78 | .register(DefaultCordVtnNodeHandler.class) |
| 79 | .build(); |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 82 | protected CoreService coreService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected StorageService storageService; |
| 86 | |
| 87 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 88 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 89 | private final MapEventListener<String, CordVtnNode> nodeStoreListener = new InternalMapListener(); |
| 90 | private ConsistentMap<String, CordVtnNode> nodeStore; |
| 91 | |
| 92 | @Activate |
| 93 | protected void activate() { |
| 94 | ApplicationId appId = coreService.registerApplication(CORDVTN_APP_ID); |
| 95 | nodeStore = storageService.<String, CordVtnNode>consistentMapBuilder() |
| 96 | .withSerializer(Serializer.using(SERIALIZER_CORDVTN_NODE)) |
| 97 | .withName("cordvtn-nodestore") |
| 98 | .withApplicationId(appId) |
| 99 | .build(); |
| 100 | nodeStore.addListener(nodeStoreListener); |
| 101 | log.info("Started"); |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | protected void deactivate() { |
| 106 | nodeStore.removeListener(nodeStoreListener); |
| 107 | log.info("Stopped"); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Set<CordVtnNode> nodes() { |
| 112 | Set<CordVtnNode> nodes = nodeStore.values().stream() |
| 113 | .map(Versioned::value) |
| 114 | .collect(Collectors.toSet()); |
| 115 | return ImmutableSet.copyOf(nodes); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public CordVtnNode node(String hostname) { |
| 120 | Versioned<CordVtnNode> versioned = nodeStore.get(hostname); |
| 121 | return versioned == null ? null : versioned.value(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void createNode(CordVtnNode node) { |
| 126 | nodeStore.compute(node.hostname(), (hostname, existing) -> { |
| 127 | final String error = node.hostname() + ERR_DUPLICATE; |
| 128 | checkArgument(existing == null, error); |
| 129 | return node; |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void updateNode(CordVtnNode node) { |
| 135 | nodeStore.compute(node.hostname(), (hostname, existing) -> { |
| 136 | final String error = node.hostname() + ERR_NOT_FOUND; |
| 137 | checkArgument(existing != null, error); |
| 138 | return node; |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public CordVtnNode removeNode(String hostname) { |
| 144 | Versioned<CordVtnNode> removed = nodeStore.remove(hostname); |
| 145 | return removed == null ? null : removed.value(); |
| 146 | } |
| 147 | |
| 148 | private class InternalMapListener implements MapEventListener<String, CordVtnNode> { |
| 149 | |
| 150 | @Override |
| 151 | public void event(MapEvent<String, CordVtnNode> event) { |
| 152 | switch (event.type()) { |
| 153 | case INSERT: |
| 154 | log.debug("CordVtn node is created {}", event.newValue().value()); |
| 155 | eventExecutor.execute(() -> { |
| 156 | notifyDelegate(new CordVtnNodeEvent( |
| 157 | CordVtnNodeEvent.Type.NODE_CREATED, |
| 158 | event.newValue().value() |
| 159 | )); |
| 160 | if (event.newValue().value().state() == COMPLETE) { |
| 161 | notifyDelegate(new CordVtnNodeEvent( |
| 162 | CordVtnNodeEvent.Type.NODE_COMPLETE, |
| 163 | event.newValue().value() |
| 164 | )); |
| 165 | } |
| 166 | }); |
| 167 | break; |
| 168 | case UPDATE: |
| 169 | log.debug("CordVtn node is updated {}", event.newValue().value()); |
| 170 | eventExecutor.execute(() -> { |
| 171 | notifyDelegate(new CordVtnNodeEvent( |
| 172 | CordVtnNodeEvent.Type.NODE_UPDATED, |
| 173 | event.newValue().value() |
| 174 | )); |
| 175 | processUpdated(event.oldValue().value(), event.newValue().value()); |
| 176 | }); |
| 177 | break; |
| 178 | case REMOVE: |
| 179 | log.debug("CordVtn node is removed {}", event.oldValue().value()); |
| 180 | eventExecutor.execute(() -> { |
| 181 | notifyDelegate(new CordVtnNodeEvent( |
| 182 | CordVtnNodeEvent.Type.NODE_REMOVED, |
| 183 | event.oldValue().value() |
| 184 | )); |
| 185 | }); |
| 186 | break; |
| 187 | default: |
| 188 | // do nothing |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private void processUpdated(CordVtnNode oldNode, CordVtnNode newNode) { |
| 194 | if (oldNode.state() != COMPLETE && newNode.state() == COMPLETE) { |
| 195 | notifyDelegate(new CordVtnNodeEvent( |
| 196 | CordVtnNodeEvent.Type.NODE_COMPLETE, |
| 197 | newNode |
| 198 | )); |
| 199 | } else if (oldNode.state() == COMPLETE && newNode.state() != COMPLETE) { |
| 200 | notifyDelegate(new CordVtnNodeEvent( |
| 201 | CordVtnNodeEvent.Type.NODE_INCOMPLETE, |
| 202 | newNode |
| 203 | )); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |