Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +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 | f4ba44f | 2017-03-14 03:25:52 +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.ofagent.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +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.incubator.net.virtual.NetworkId; |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 23 | import org.onosproject.incubator.net.virtual.TenantId; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 24 | import org.onosproject.ofagent.api.OFAgent; |
| 25 | import org.onosproject.ofagent.api.OFAgentEvent; |
| 26 | import org.onosproject.ofagent.api.OFAgentEvent.Type; |
| 27 | import org.onosproject.ofagent.api.OFAgentStore; |
| 28 | import org.onosproject.ofagent.api.OFAgentStoreDelegate; |
| 29 | import org.onosproject.ofagent.api.OFController; |
| 30 | import org.onosproject.store.AbstractStore; |
| 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.MapEvent; |
| 34 | import org.onosproject.store.service.MapEventListener; |
| 35 | import org.onosproject.store.service.Serializer; |
| 36 | import org.onosproject.store.service.StorageService; |
| 37 | import org.onosproject.store.service.Versioned; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 38 | import org.osgi.service.component.annotations.Activate; |
| 39 | import org.osgi.service.component.annotations.Component; |
| 40 | import org.osgi.service.component.annotations.Deactivate; |
| 41 | import org.osgi.service.component.annotations.Reference; |
| 42 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.ExecutorService; |
| 47 | import java.util.stream.Collectors; |
| 48 | |
| 49 | import static com.google.common.base.Preconditions.checkArgument; |
| 50 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 51 | import static org.onlab.util.Tools.groupedThreads; |
| 52 | import static org.onosproject.ofagent.api.OFAgent.State.STARTED; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | import static org.onosproject.ofagent.api.OFAgentEvent.Type.OFAGENT_CONTROLLER_ADDED; |
| 54 | import static org.onosproject.ofagent.api.OFAgentEvent.Type.OFAGENT_CONTROLLER_REMOVED; |
| 55 | import static org.onosproject.ofagent.api.OFAgentEvent.Type.OFAGENT_STARTED; |
| 56 | import static org.onosproject.ofagent.api.OFAgentEvent.Type.OFAGENT_STOPPED; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 57 | import static org.onosproject.ofagent.api.OFAgentService.APPLICATION_NAME; |
| 58 | import static org.slf4j.LoggerFactory.getLogger; |
| 59 | |
| 60 | /** |
| 61 | * Implementation of the {@link OFAgentStore} with consistent map. |
| 62 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Component(immediate = true, service = OFAgentStore.class) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 64 | public class DistributedOFAgentStore extends AbstractStore<OFAgentEvent, OFAgentStoreDelegate> |
| 65 | implements OFAgentStore { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
| 69 | private static final String ERR_NOT_FOUND = " does not exist"; |
| 70 | private static final String ERR_DUPLICATE = " already exists"; |
| 71 | |
| 72 | private static final KryoNamespace SERIALIZER_OFAGENT = KryoNamespace.newBuilder() |
| 73 | .register(KryoNamespaces.API) |
| 74 | .register(OFAgent.class) |
| 75 | .register(OFAgent.State.class) |
| 76 | .register(NetworkId.class) |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 77 | .register(TenantId.class) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 78 | .register(DefaultOFAgent.class) |
| 79 | .register(OFController.class) |
| 80 | .register(DefaultOFController.class) |
| 81 | .build(); |
| 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 84 | protected CoreService coreService; |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 87 | protected StorageService storageService; |
| 88 | |
| 89 | private final ExecutorService eventExecutor = newSingleThreadExecutor( |
| 90 | groupedThreads(this.getClass().getSimpleName(), "event-handler", log)); |
| 91 | private final MapEventListener<NetworkId, OFAgent> ofAgentMapListener = new OFAgentMapListener(); |
| 92 | |
| 93 | private ConsistentMap<NetworkId, OFAgent> ofAgentStore; |
| 94 | |
| 95 | @Activate |
| 96 | protected void activate() { |
| 97 | ApplicationId appId = coreService.registerApplication(APPLICATION_NAME); |
| 98 | ofAgentStore = storageService.<NetworkId, OFAgent>consistentMapBuilder() |
| 99 | .withSerializer(Serializer.using(SERIALIZER_OFAGENT)) |
| 100 | .withName("ofagentstore") |
| 101 | .withApplicationId(appId) |
| 102 | .build(); |
| 103 | ofAgentStore.addListener(ofAgentMapListener); |
| 104 | |
| 105 | log.info("Started"); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | protected void deactivate() { |
| 110 | ofAgentStore.removeListener(ofAgentMapListener); |
| 111 | eventExecutor.shutdown(); |
| 112 | |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void createOfAgent(OFAgent ofAgent) { |
| 118 | ofAgentStore.compute(ofAgent.networkId(), (id, existing) -> { |
| 119 | final String error = ofAgent.networkId() + ERR_DUPLICATE; |
| 120 | checkArgument(existing == null, error); |
| 121 | return ofAgent; |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void updateOfAgent(OFAgent ofAgent) { |
| 127 | ofAgentStore.compute(ofAgent.networkId(), (id, existing) -> { |
| 128 | final String error = ofAgent.networkId() + ERR_NOT_FOUND; |
| 129 | checkArgument(existing != null, error); |
| 130 | return ofAgent; |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public OFAgent removeOfAgent(NetworkId networkId) { |
| 136 | Versioned<OFAgent> ofAgent = ofAgentStore.remove(networkId); |
| 137 | return ofAgent == null ? null : ofAgent.value(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public OFAgent ofAgent(NetworkId networkId) { |
| 142 | Versioned<OFAgent> ofAgent = ofAgentStore.get(networkId); |
| 143 | return ofAgent == null ? null : ofAgent.value(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Set<OFAgent> ofAgents() { |
| 148 | Set<OFAgent> ofAgents = ofAgentStore.values().stream() |
| 149 | .map(Versioned::value) |
| 150 | .collect(Collectors.toSet()); |
| 151 | return ImmutableSet.copyOf(ofAgents); |
| 152 | } |
| 153 | |
| 154 | private class OFAgentMapListener implements MapEventListener<NetworkId, OFAgent> { |
| 155 | |
| 156 | @Override |
| 157 | public void event(MapEvent<NetworkId, OFAgent> event) { |
| 158 | switch (event.type()) { |
| 159 | case INSERT: |
| 160 | eventExecutor.execute(() -> { |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 161 | log.debug(OFAgent.TRACER_LOG_TENANT_ID_PREFIX + event.newValue().value().tenantId() |
| 162 | + " OFAgent for network {} created", event.key()); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 163 | notifyDelegate(new OFAgentEvent( |
| 164 | Type.OFAGENT_CREATED, |
| 165 | event.newValue().value())); |
| 166 | }); |
| 167 | break; |
| 168 | case UPDATE: |
| 169 | eventExecutor.execute(() -> { |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 170 | log.debug(OFAgent.TRACER_LOG_TENANT_ID_PREFIX + event.newValue().value().tenantId() |
| 171 | + " OFAgent for network {} updated", event.key()); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 172 | processUpdated(event.oldValue().value(), event.newValue().value()); |
| 173 | }); |
| 174 | break; |
| 175 | case REMOVE: |
| 176 | eventExecutor.execute(() -> { |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 177 | log.debug(OFAgent.TRACER_LOG_TENANT_ID_PREFIX + event.oldValue().value().tenantId() |
| 178 | + " OFAgent for network {} removed", event.key()); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 179 | notifyDelegate(new OFAgentEvent( |
| 180 | Type.OFAGENT_REMOVED, |
| 181 | event.oldValue().value())); |
| 182 | }); |
| 183 | break; |
| 184 | default: |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private void processUpdated(OFAgent oldValue, OFAgent newValue) { |
| 190 | if (!oldValue.controllers().equals(newValue.controllers())) { |
| 191 | oldValue.controllers().stream() |
| 192 | .filter(controller -> !newValue.controllers().contains(controller)) |
| 193 | .forEach(controller -> notifyDelegate(new OFAgentEvent( |
| 194 | OFAGENT_CONTROLLER_REMOVED, |
| 195 | newValue, |
| 196 | controller) |
| 197 | )); |
| 198 | |
| 199 | newValue.controllers().stream() |
| 200 | .filter(controller -> !oldValue.controllers().contains(controller)) |
| 201 | .forEach(controller -> notifyDelegate(new OFAgentEvent( |
| 202 | OFAGENT_CONTROLLER_ADDED, |
| 203 | newValue, |
| 204 | controller |
| 205 | ))); |
| 206 | } |
| 207 | |
| 208 | if (oldValue.state() != newValue.state()) { |
| 209 | Type eventType = newValue.state() == STARTED ? OFAGENT_STARTED : OFAGENT_STOPPED; |
| 210 | notifyDelegate(new OFAgentEvent(eventType, newValue)); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |