Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [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.vpls.store; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onlab.util.KryoNamespace; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.intf.Interface; |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 28 | import org.onosproject.net.config.NetworkConfigService; |
| 29 | import org.onosproject.store.AbstractStore; |
| 30 | import org.onosproject.store.StoreDelegate; |
| 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 33 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 34 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 35 | import org.onosproject.store.service.StorageService; |
| 36 | import org.onosproject.store.service.WallClockTimestamp; |
| 37 | import org.onosproject.vpls.VplsManager; |
| 38 | import org.onosproject.vpls.api.VplsData; |
| 39 | import org.onosproject.vpls.api.VplsOperation; |
| 40 | import org.onosproject.vpls.config.VplsAppConfig; |
| 41 | import org.onosproject.vpls.api.VplsStore; |
| 42 | import org.onosproject.vpls.config.VplsConfig; |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | import java.util.Collection; |
| 47 | import java.util.Set; |
| 48 | import java.util.concurrent.CompletableFuture; |
| 49 | import java.util.stream.Collectors; |
| 50 | |
| 51 | import static java.util.Objects.*; |
| 52 | |
| 53 | /** |
| 54 | * Implementation of VPLSConfigurationService which reads VPLS configuration |
| 55 | * from the network configuration service. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | @Service |
| 59 | public class DistributedVplsStore |
| 60 | extends AbstractStore<VplsStoreEvent, StoreDelegate<VplsStoreEvent>> |
| 61 | implements VplsStore { |
| 62 | |
| 63 | private static final KryoNamespace APP_KRYO = KryoNamespace.newBuilder() |
| 64 | .register(KryoNamespaces.API) |
Yi Tseng | f31d2d9 | 2017-05-17 18:03:33 -0700 | [diff] [blame] | 65 | .register(Interface.class) |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 66 | .register(VplsData.class) |
Yi Tseng | f31d2d9 | 2017-05-17 18:03:33 -0700 | [diff] [blame] | 67 | .register(VplsData.VplsState.class) |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 68 | .register(VplsOperation.class) |
| 69 | .build(); |
| 70 | |
| 71 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected CoreService coreService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected StorageService storageService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected NetworkConfigService networkConfigService; |
| 81 | |
| 82 | private EventuallyConsistentMap<String, VplsData> vplsDataStore; |
| 83 | private EventuallyConsistentMapListener<String, VplsData> vplsDataListener; |
| 84 | private ApplicationId appId; |
| 85 | |
| 86 | @Activate |
| 87 | protected void active() { |
| 88 | appId = coreService.registerApplication(VplsManager.VPLS_APP); |
| 89 | |
| 90 | vplsDataStore = storageService.<String, VplsData>eventuallyConsistentMapBuilder() |
| 91 | .withName("VPLS-Data") |
| 92 | .withTimestampProvider((name, vpls) -> new WallClockTimestamp()) |
| 93 | .withSerializer(APP_KRYO) |
| 94 | .build(); |
| 95 | vplsDataListener = new InternalVplsDataListener(); |
| 96 | vplsDataStore.addListener(vplsDataListener); |
| 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | protected void deactive() { |
| 102 | vplsDataStore.removeListener(vplsDataListener); |
Yi Tseng | ada511f | 2017-05-18 14:46:04 -0700 | [diff] [blame] | 103 | networkConfigService.removeConfig(appId); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 104 | log.info("Stopped"); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void addVpls(VplsData vplsData) { |
| 109 | requireNonNull(vplsData); |
| 110 | if (vplsData.name().isEmpty()) { |
| 111 | throw new IllegalArgumentException("VPLS name is empty."); |
| 112 | } |
| 113 | vplsData.state(VplsData.VplsState.ADDING); |
| 114 | this.vplsDataStore.put(vplsData.name(), vplsData); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void removeVpls(VplsData vplsData) { |
| 119 | requireNonNull(vplsData); |
| 120 | if (vplsData.name().isEmpty()) { |
| 121 | throw new IllegalArgumentException("VPLS name is empty."); |
| 122 | } |
| 123 | vplsData.state(VplsData.VplsState.REMOVING); |
| 124 | if (!this.vplsDataStore.containsKey(vplsData.name())) { |
| 125 | // notify the delegate asynchronously if VPLS does not exists |
| 126 | CompletableFuture.runAsync(() -> { |
| 127 | VplsStoreEvent event = new VplsStoreEvent(VplsStoreEvent.Type.REMOVE, |
| 128 | vplsData); |
| 129 | notifyDelegate(event); |
| 130 | }); |
| 131 | return; |
| 132 | } |
| 133 | this.vplsDataStore.remove(vplsData.name()); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void updateVpls(VplsData vplsData) { |
| 138 | switch (vplsData.state()) { |
| 139 | case ADDED: |
| 140 | case REMOVED: |
| 141 | case FAILED: |
| 142 | // state update only |
| 143 | this.vplsDataStore.put(vplsData.name(), vplsData); |
| 144 | break; |
| 145 | default: |
| 146 | vplsData.state(VplsData.VplsState.UPDATING); |
| 147 | this.vplsDataStore.put(vplsData.name(), vplsData); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public VplsData getVpls(String vplsName) { |
| 154 | return vplsDataStore.get(vplsName); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Collection<VplsData> getAllVpls() { |
| 159 | return vplsDataStore.values(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Writes all VPLS data to the network configuration store. |
| 164 | * |
| 165 | * @param vplsDataCollection the VPLSs data |
| 166 | */ |
| 167 | public void writeVplsToNetConfig(Collection<VplsData> vplsDataCollection) { |
| 168 | VplsAppConfig config = networkConfigService.addConfig(appId, VplsAppConfig.class); |
Yi Tseng | f31d2d9 | 2017-05-17 18:03:33 -0700 | [diff] [blame] | 169 | if (config == null) { |
| 170 | log.debug("VPLS config is not available now"); |
| 171 | return; |
| 172 | } |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 173 | config.clearVplsConfig(); |
| 174 | |
| 175 | // Setup update time for this VPLS application configuration |
| 176 | WallClockTimestamp ts = new WallClockTimestamp(); |
| 177 | config.updateTime(ts.unixTimestamp()); |
| 178 | |
| 179 | vplsDataCollection.forEach(vplsData -> { |
| 180 | Set<String> interfaceNames = vplsData.interfaces() |
| 181 | .stream() |
| 182 | .map(Interface::name) |
| 183 | .collect(Collectors.toSet()); |
| 184 | VplsConfig vplsConfig = new VplsConfig(vplsData.name(), interfaceNames, |
| 185 | vplsData.encapsulationType()); |
| 186 | config.addVpls(vplsConfig); |
| 187 | }); |
| 188 | |
| 189 | networkConfigService.applyConfig(appId, VplsAppConfig.class, config.node()); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Listener for VPLS data store. |
| 194 | */ |
| 195 | private class InternalVplsDataListener implements EventuallyConsistentMapListener<String, VplsData> { |
| 196 | private static final String STATE_UPDATE = "VPLS state updated, new VPLS: {}"; |
| 197 | |
| 198 | @Override |
| 199 | public void event(EventuallyConsistentMapEvent<String, VplsData> event) { |
| 200 | VplsData vplsData = event.value(); |
| 201 | // Update network config |
| 202 | writeVplsToNetConfig(getAllVpls()); |
| 203 | switch (event.type()) { |
| 204 | case PUT: |
| 205 | // Add or Update |
| 206 | if (vplsData.state() == VplsData.VplsState.ADDING) { |
| 207 | VplsStoreEvent vplsStoreEvent = |
| 208 | new VplsStoreEvent(VplsStoreEvent.Type.ADD, vplsData); |
| 209 | notifyDelegate(vplsStoreEvent); |
| 210 | } else if (vplsData.state() == VplsData.VplsState.UPDATING) { |
| 211 | VplsStoreEvent vplsStoreEvent = |
| 212 | new VplsStoreEvent(VplsStoreEvent.Type.UPDATE, vplsData); |
| 213 | notifyDelegate(vplsStoreEvent); |
| 214 | } else { |
| 215 | // Do nothing here, just update state from operation service |
| 216 | log.debug(STATE_UPDATE, vplsData); |
| 217 | } |
| 218 | break; |
| 219 | case REMOVE: |
Yi Tseng | 3069c61 | 2017-05-26 17:09:43 -0700 | [diff] [blame] | 220 | if (vplsData == null) { |
| 221 | vplsData = VplsData.of(event.key()); |
| 222 | } |
| 223 | vplsData.state(VplsData.VplsState.REMOVING); |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 224 | VplsStoreEvent vplsStoreEvent = |
| 225 | new VplsStoreEvent(VplsStoreEvent.Type.REMOVE, vplsData); |
| 226 | notifyDelegate(vplsStoreEvent); |
| 227 | break; |
| 228 | default: |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | } |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame^] | 234 | } |