Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.incubator.net.l2monitoring.cfm.impl; |
| 17 | |
| 18 | import com.google.common.net.InternetDomainName; |
| 19 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Deactivate; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.util.Identifier; |
| 28 | import org.onlab.util.KryoNamespace; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaId2Octet; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdIccY1731; |
| 37 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdPrimaryVid; |
| 38 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdRfc2685VpnId; |
| 39 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 40 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 41 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 42 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdDomainName; |
| 43 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdMacUint; |
| 44 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdNone; |
| 45 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 46 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 47 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdEvent; |
| 48 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdStore; |
| 49 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdStoreDelegate; |
| 50 | import org.onosproject.store.serializers.KryoNamespaces; |
| 51 | import org.onosproject.store.AbstractStore; |
| 52 | import org.onosproject.store.service.ConsistentMap; |
| 53 | import org.onosproject.store.service.MapEvent; |
| 54 | import org.onosproject.store.service.MapEventListener; |
| 55 | import org.onosproject.store.service.Serializer; |
| 56 | import org.onosproject.store.service.StorageService; |
| 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
| 60 | import java.util.Collection; |
| 61 | import java.util.Map; |
| 62 | import java.util.Optional; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 63 | import java.util.Set; |
| 64 | import java.util.stream.Collectors; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Maintenance Domain Store implementation backed by consistent map. |
| 68 | */ |
| 69 | @Component(immediate = true) |
| 70 | @Service |
| 71 | public class DistributedMdStore extends AbstractStore<MdEvent, MdStoreDelegate> |
| 72 | implements MdStore { |
| 73 | |
| 74 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected StorageService storageService; |
| 78 | |
| 79 | private ConsistentMap<MdId, MaintenanceDomain> maintenanceDomainConsistentMap; |
| 80 | private Map<MdId, MaintenanceDomain> maintenanceDomainMap; |
| 81 | |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 82 | private MapEventListener<MdId, MaintenanceDomain> mapListener = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 83 | |
| 84 | @Activate |
| 85 | public void activate() { |
| 86 | maintenanceDomainConsistentMap = storageService |
| 87 | .<MdId, MaintenanceDomain>consistentMapBuilder() |
| 88 | .withName("onos-cfm-ma-map") |
| 89 | .withSerializer(Serializer.using(new KryoNamespace.Builder() |
| 90 | .register(KryoNamespaces.API) |
| 91 | .register(DefaultMaintenanceDomain.class) |
| 92 | .register(MdIdCharStr.class) |
| 93 | .register(MdIdDomainName.class) |
| 94 | .register(MdIdMacUint.class) |
| 95 | .register(MdIdNone.class) |
| 96 | .register(MaintenanceDomain.MdLevel.class) |
| 97 | .register(DefaultMaintenanceAssociation.class) |
| 98 | .register(MaIdCharStr.class) |
| 99 | .register(MaIdShort.class) |
| 100 | .register(MaId2Octet.class) |
| 101 | .register(MaIdIccY1731.class) |
| 102 | .register(MaIdPrimaryVid.class) |
| 103 | .register(MaIdRfc2685VpnId.class) |
| 104 | .register(MaintenanceAssociation.CcmInterval.class) |
| 105 | .register(DefaultComponent.class) |
| 106 | .register(MepId.class) |
| 107 | .register(Identifier.class) |
| 108 | .register(InternetDomainName.class) |
| 109 | .register(MacAddress.class) |
| 110 | .register(ImmutablePair.class) |
Sean Condon | 3a1efef | 2018-02-24 13:16:03 +0000 | [diff] [blame] | 111 | .register(org.onosproject.incubator.net.l2monitoring.cfm.Component.MhfCreationType.class) |
| 112 | .register(org.onosproject.incubator.net.l2monitoring.cfm.Component.IdPermissionType.class) |
| 113 | .register(org.onosproject.incubator.net.l2monitoring.cfm.Component.TagType.class) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 114 | .build("md"))) |
| 115 | .build(); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 116 | mapListener = new InternalMdListener(); |
| 117 | maintenanceDomainConsistentMap.addListener(mapListener); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 118 | |
| 119 | maintenanceDomainMap = maintenanceDomainConsistentMap.asJavaMap(); |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 120 | log.info("MDStore started"); |
| 121 | } |
| 122 | |
| 123 | @Deactivate |
| 124 | public void deactivate() { |
| 125 | maintenanceDomainConsistentMap.removeListener(mapListener); |
| 126 | log.info("Stopped"); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Collection<MaintenanceDomain> getAllMaintenanceDomain() { |
| 131 | return maintenanceDomainMap.values(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Optional<MaintenanceDomain> getMaintenanceDomain(MdId mdName) { |
| 136 | return Optional.ofNullable( |
| 137 | maintenanceDomainMap.get(mdName)); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean deleteMaintenanceDomain(MdId mdName) { |
| 142 | return maintenanceDomainMap.remove(mdName) == null ? false : true; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public boolean createUpdateMaintenanceDomain(MaintenanceDomain md) { |
| 147 | return maintenanceDomainMap.put(md.mdId(), md) == null ? false : true; |
| 148 | } |
| 149 | |
| 150 | private class InternalMdListener implements MapEventListener<MdId, MaintenanceDomain> { |
| 151 | @Override |
| 152 | public void event(MapEvent<MdId, MaintenanceDomain> mapEvent) { |
| 153 | final MdEvent.Type type; |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 154 | MaIdShort maId = null; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 155 | switch (mapEvent.type()) { |
| 156 | case INSERT: |
| 157 | type = MdEvent.Type.MD_ADDED; |
| 158 | break; |
| 159 | case UPDATE: |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 160 | // Examine the diff to see if it was a removal or addition of an MA caused it |
| 161 | if (mapEvent.oldValue().value().maintenanceAssociationList().size() > |
| 162 | mapEvent.newValue().value().maintenanceAssociationList().size()) { |
| 163 | Set<MaIdShort> newMaIds = mapEvent.newValue().value().maintenanceAssociationList() |
| 164 | .stream() |
| 165 | .map(MaintenanceAssociation::maId) |
| 166 | .collect(Collectors.toSet()); |
| 167 | Optional<MaintenanceAssociation> removedMa = |
| 168 | mapEvent.oldValue().value().maintenanceAssociationList() |
| 169 | .stream() |
| 170 | .filter(maOld -> !newMaIds.contains(maOld.maId())).findFirst(); |
| 171 | if (removedMa.isPresent()) { |
| 172 | maId = removedMa.get().maId(); |
| 173 | } |
| 174 | type = MdEvent.Type.MA_REMOVED; |
| 175 | } else if (mapEvent.oldValue().value().maintenanceAssociationList().size() < |
| 176 | mapEvent.newValue().value().maintenanceAssociationList().size()) { |
| 177 | Set<MaIdShort> oldMaIds = mapEvent.oldValue().value().maintenanceAssociationList() |
| 178 | .stream() |
| 179 | .map(MaintenanceAssociation::maId) |
| 180 | .collect(Collectors.toSet()); |
| 181 | Optional<MaintenanceAssociation> addedMa = |
| 182 | mapEvent.newValue().value().maintenanceAssociationList() |
| 183 | .stream() |
| 184 | .filter(maNew -> !oldMaIds.contains(maNew.maId())).findFirst(); |
| 185 | if (addedMa.isPresent()) { |
| 186 | maId = addedMa.get().maId(); |
| 187 | } |
| 188 | type = MdEvent.Type.MA_ADDED; |
| 189 | } else { |
| 190 | type = MdEvent.Type.MD_UPDATED; |
| 191 | } |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 192 | break; |
| 193 | case REMOVE: |
| 194 | default: |
| 195 | type = MdEvent.Type.MD_REMOVED; |
| 196 | break; |
| 197 | } |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 198 | if (mapEvent.oldValue() != null && mapEvent.oldValue().value() != null) { |
| 199 | MaintenanceDomain oldMd = mapEvent.oldValue().value(); |
| 200 | try { |
| 201 | if (maId != null) { |
| 202 | notifyDelegate(new MdEvent(type, mapEvent.key(), oldMd, maId)); |
| 203 | } else { |
| 204 | notifyDelegate(new MdEvent(type, mapEvent.key(), oldMd)); |
| 205 | } |
| 206 | } catch (CfmConfigException e) { |
| 207 | log.warn("Unable to copy MD {}", oldMd); |
| 208 | notifyDelegate(new MdEvent(type, mapEvent.key())); |
| 209 | } |
| 210 | } else { |
| 211 | notifyDelegate(new MdEvent(type, mapEvent.key())); |
| 212 | } |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |