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 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.core.CoreService; |
| 20 | import org.onosproject.event.AbstractListenerManager; |
| 21 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 22 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdEvent; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdListener; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdStore; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MdStoreDelegate; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 32 | import org.osgi.service.component.annotations.Activate; |
| 33 | import org.osgi.service.component.annotations.Component; |
| 34 | import org.osgi.service.component.annotations.Deactivate; |
| 35 | import org.osgi.service.component.annotations.Reference; |
| 36 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 40 | import java.util.Collection; |
| 41 | import java.util.Optional; |
| 42 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Manager of Cfm Md Service - persists Maintenance Domain in distributed store. |
| 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 46 | @Component(immediate = true, service = CfmMdService.class) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 47 | public class CfmMdManager extends AbstractListenerManager<MdEvent, MdListener> |
| 48 | implements CfmMdService { |
| 49 | |
| 50 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 51 | private static final String APP_ID = "org.onosproject.app.cfm"; |
| 52 | |
| 53 | protected ApplicationId appId; |
| 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 56 | protected CoreService coreService; |
| 57 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 59 | protected MdStore store; |
| 60 | |
| 61 | protected final MdStoreDelegate delegate = new InternalStoreDelegate(); |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | appId = coreService.registerApplication(APP_ID); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 66 | eventDispatcher.addSink(MdEvent.class, listenerRegistry); |
| 67 | store.setDelegate(delegate); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 68 | log.info("CFM Service Started"); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | eventDispatcher.removeSink(MdEvent.class); |
| 74 | store.unsetDelegate(delegate); |
| 75 | log.info("CFM Service Stopped"); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Collection<MaintenanceDomain> getAllMaintenanceDomain() { |
| 80 | log.debug("Retrieving all MDs from distributed store"); |
| 81 | return store.getAllMaintenanceDomain(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Optional<MaintenanceDomain> getMaintenanceDomain(MdId mdName) { |
| 86 | log.debug("Retrieving MD {} from distributed store", mdName); |
| 87 | return store.getMaintenanceDomain(mdName); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean deleteMaintenanceDomain(MdId mdName) throws CfmConfigException { |
| 92 | log.info("Deleting MD {} from distributed store", mdName); |
| 93 | return store.deleteMaintenanceDomain(mdName); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Collection<MaintenanceAssociation> getAllMaintenanceAssociation(MdId mdName) { |
| 98 | log.debug("Retrieving all MA of MD {} from distributed store", mdName); |
| 99 | return store.getMaintenanceDomain(mdName) |
| 100 | .orElseThrow(() -> new IllegalArgumentException("Unknown MD " + mdName)) |
| 101 | .maintenanceAssociationList(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Optional<MaintenanceAssociation> getMaintenanceAssociation( |
| 106 | MdId mdName, MaIdShort maName) { |
| 107 | log.debug("Retrieving MA {} of MD {} from distributed store", maName, mdName); |
| 108 | return store.getMaintenanceDomain(mdName) |
| 109 | .orElseThrow(() -> new IllegalArgumentException("Unknown MD " + mdName)) |
| 110 | .maintenanceAssociationList() |
| 111 | .stream().filter(ma -> ma.maId().equals(maName)) |
| 112 | .findFirst(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean deleteMaintenanceAssociation(MdId mdName, MaIdShort maName) throws CfmConfigException { |
| 117 | log.info("Deleting MA {} of MD {} from distributed store", maName, mdName); |
| 118 | MaintenanceDomain.MdBuilder builder = DefaultMaintenanceDomain |
| 119 | .builder(store.getMaintenanceDomain(mdName) |
| 120 | .orElseThrow(() -> new IllegalArgumentException("Unknown MD: " + mdName))); |
| 121 | |
| 122 | //Check the MA is present |
| 123 | if (!builder.checkMaExists(maName)) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | builder = builder.deleteFromMaList(maName); |
| 128 | |
| 129 | store.createUpdateMaintenanceDomain(builder.build()); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean createMaintenanceDomain(MaintenanceDomain newMd) throws CfmConfigException { |
| 135 | log.info("Creating/Updating MD {} in distributed store", newMd.mdId()); |
| 136 | return store.createUpdateMaintenanceDomain(newMd); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public boolean createMaintenanceAssociation(MdId mdName, MaintenanceAssociation newMa) |
| 141 | throws CfmConfigException { |
| 142 | log.info("Updating MD {} in distributed store by adding new MA {}", mdName, newMa.maId()); |
| 143 | MaintenanceDomain.MdBuilder builder = DefaultMaintenanceDomain |
| 144 | .builder(store.getMaintenanceDomain(mdName) |
| 145 | .orElseThrow(() -> new IllegalArgumentException("Unknown MD: " + mdName))); |
| 146 | |
| 147 | boolean replaced = false; |
| 148 | //Check the MA is present |
| 149 | if (builder.checkMaExists(newMa.maId())) { |
| 150 | builder = builder.deleteFromMaList(newMa.maId()); |
| 151 | replaced = true; |
| 152 | } |
| 153 | |
| 154 | builder.addToMaList(newMa); |
| 155 | store.createUpdateMaintenanceDomain(builder.build()); |
| 156 | return replaced; |
| 157 | } |
| 158 | |
| 159 | private class InternalStoreDelegate implements MdStoreDelegate { |
| 160 | @Override |
| 161 | public void notify(MdEvent event) { |
Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [diff] [blame] | 162 | log.debug("New MD event: {}", event); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 163 | eventDispatcher.post(event); |
| 164 | } |
| 165 | } |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 166 | } |