Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | |
| 17 | package org.opencord.olt.driver; |
| 18 | |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.cfg.ConfigProperty; |
| 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.LeadershipService; |
| 23 | import org.onosproject.cluster.NodeId; |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 24 | import org.onosproject.net.driver.AbstractDriverLoader; |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 25 | import org.osgi.service.component.ComponentContext; |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 26 | import org.osgi.service.component.annotations.Component; |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 27 | import org.osgi.service.component.annotations.Modified; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 32 | import java.util.Dictionary; |
| 33 | import java.util.Properties; |
| 34 | import java.util.concurrent.TimeUnit; |
| 35 | |
| 36 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 37 | import static org.onlab.util.Tools.get; |
| 38 | import static org.opencord.olt.impl.OsgiPropertyConstants.*; |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Loader for olt device drivers. |
| 43 | */ |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 44 | @Component(immediate = true, property = { |
| 45 | REQUIRED_DRIVERS_PROPERTY_DELAY + ":Integer=" + REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT |
| 46 | }) |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 47 | public class OltDriversLoader extends AbstractDriverLoader { |
| 48 | |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 49 | public static final String DRIVER_REGISTRY_MANAGER = |
| 50 | "org.onosproject.net.driver.impl.DriverRegistryManager"; |
| 51 | public static final String REQUIRED_DRIVERS = "requiredDrivers"; |
| 52 | public static final String VOLTHA_DRIVER_NAME = "voltha"; |
| 53 | public static final String DRIVER_UPDATE_LEADSHIP_TOPIC = "driver-update"; |
| 54 | public static final String OLT_DRIVERS_LOADER = "org.opencord.olt.driver.OltDriversLoader"; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 57 | private ComponentConfigService compCfgService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 60 | protected LeadershipService leadershipService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 63 | protected ClusterService clusterService; |
| 64 | |
| 65 | /** |
| 66 | * Default amounts of eapol retry. |
| 67 | **/ |
| 68 | protected int requiredDriversPropertyDelay = REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT; |
| 69 | |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 70 | private final Logger log = getLogger(getClass()); |
| 71 | |
| 72 | public OltDriversLoader() { |
| 73 | super("/olt-drivers.xml"); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void activate() { |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 78 | log.info("Activating OLT Driver loader"); |
| 79 | compCfgService.registerProperties(getClass()); |
| 80 | //The AbstractDriversLoader does not pass the context, to avoid changes to all |
| 81 | // inheriting classes getting the property differently |
| 82 | ConfigProperty requiredDriversPropertyDelayNew = |
| 83 | compCfgService.getProperty(OLT_DRIVERS_LOADER, REQUIRED_DRIVERS_PROPERTY_DELAY); |
| 84 | requiredDriversPropertyDelay = requiredDriversPropertyDelayNew == null ? |
| 85 | REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT : requiredDriversPropertyDelayNew.asInteger(); |
| 86 | log.info("OLT Driver loader requiredDriversPropertyDelay: {}", requiredDriversPropertyDelay); |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 87 | super.activate(); |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 88 | // Verify if this node is the leader |
| 89 | NodeId leader = leadershipService.runForLeadership(DRIVER_UPDATE_LEADSHIP_TOPIC).leaderNodeId(); |
| 90 | if (clusterService.getLocalNode().id().equals(leader)) { |
| 91 | Thread thread = new Thread(() -> { |
| 92 | // Sleep is needed to allow driver correct initialization and |
| 93 | // flow objective cache invalidation. |
| 94 | try { |
| 95 | TimeUnit.SECONDS.sleep(requiredDriversPropertyDelay); |
| 96 | } catch (InterruptedException e) { |
| 97 | log.error("Interrupted thread while activating", e); |
| 98 | } |
| 99 | String currentRequiredDrivers = |
| 100 | compCfgService.getProperty(DRIVER_REGISTRY_MANAGER, REQUIRED_DRIVERS) |
| 101 | .asString(); |
| 102 | //insertion of voltha in the required drivers |
| 103 | if (!currentRequiredDrivers.contains(VOLTHA_DRIVER_NAME)) { |
| 104 | String updatedRequiredDrivers = currentRequiredDrivers; |
| 105 | if (!updatedRequiredDrivers.endsWith(",")) { |
| 106 | updatedRequiredDrivers = updatedRequiredDrivers + ","; |
| 107 | } |
| 108 | updatedRequiredDrivers = updatedRequiredDrivers + VOLTHA_DRIVER_NAME; |
| 109 | compCfgService.setProperty(DRIVER_REGISTRY_MANAGER, |
| 110 | REQUIRED_DRIVERS, updatedRequiredDrivers); |
| 111 | log.debug("Added voltha driver to required drivers {}", |
| 112 | updatedRequiredDrivers); |
| 113 | } |
| 114 | }); |
| 115 | thread.start(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Modified |
| 120 | public void modified(ComponentContext context) { |
| 121 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 122 | try { |
| 123 | String requiredDriversPropertyDelayNew = get(properties, REQUIRED_DRIVERS_PROPERTY_DELAY); |
| 124 | requiredDriversPropertyDelay = isNullOrEmpty(requiredDriversPropertyDelayNew) ? |
| 125 | REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT : |
| 126 | Integer.parseInt(requiredDriversPropertyDelayNew.trim()); |
| 127 | |
| 128 | log.info("OLT Driver loader requiredDriversPropertyDelay: {}", requiredDriversPropertyDelay); |
| 129 | |
| 130 | } catch (Exception e) { |
| 131 | log.error("Error while modifying the properties", e); |
| 132 | requiredDriversPropertyDelay = REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT; |
| 133 | } |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void deactivate() { |
Andrea Campanella | 438e1ad | 2021-03-26 11:41:16 +0100 | [diff] [blame^] | 138 | log.info("Deactivating OLT Driver loader"); |
| 139 | //The AbstractDriversLoader does not pass the context, to avoid changes to all |
| 140 | // inheriting classes getting the property differently |
| 141 | ConfigProperty requiredDriversPropertyDelayNew = |
| 142 | compCfgService.getProperty(OLT_DRIVERS_LOADER, REQUIRED_DRIVERS_PROPERTY_DELAY); |
| 143 | requiredDriversPropertyDelay = requiredDriversPropertyDelayNew == null ? |
| 144 | REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT : requiredDriversPropertyDelayNew.asInteger(); |
| 145 | log.info("OLT Driver loader requiredDriversPropertyDelay: {}", requiredDriversPropertyDelay); |
| 146 | NodeId leader = leadershipService.runForLeadership(DRIVER_UPDATE_LEADSHIP_TOPIC).leaderNodeId(); |
| 147 | if (clusterService.getLocalNode().id().equals(leader)) { |
| 148 | String currentRequiredDrivers = |
| 149 | compCfgService.getProperty(DRIVER_REGISTRY_MANAGER, REQUIRED_DRIVERS) |
| 150 | .asString(); |
| 151 | //removal of voltha from the required driver |
| 152 | if (currentRequiredDrivers.contains(VOLTHA_DRIVER_NAME)) { |
| 153 | String updatedRequiredDrivers = currentRequiredDrivers.replace(VOLTHA_DRIVER_NAME, ""); |
| 154 | //handling the case where `voltha` was not the last required driver in the list |
| 155 | if (updatedRequiredDrivers.contains(",,")) { |
| 156 | updatedRequiredDrivers = updatedRequiredDrivers.replace(",,", ","); |
| 157 | } |
| 158 | if (updatedRequiredDrivers.endsWith(",")) { |
| 159 | updatedRequiredDrivers = updatedRequiredDrivers.substring(0, updatedRequiredDrivers.length() - 1); |
| 160 | } |
| 161 | compCfgService.setProperty(DRIVER_REGISTRY_MANAGER, |
| 162 | REQUIRED_DRIVERS, updatedRequiredDrivers); |
| 163 | log.debug("Removed voltha from required drivers {}", updatedRequiredDrivers); |
| 164 | } |
| 165 | } |
| 166 | // Sleep is needed to allow proper property sharing across the instances through accumulator |
| 167 | // of component config manager |
| 168 | try { |
| 169 | TimeUnit.SECONDS.sleep(requiredDriversPropertyDelay); |
| 170 | } catch (InterruptedException e) { |
| 171 | log.error("Interrupted while de-activating", e); |
| 172 | } |
| 173 | compCfgService.unregisterProperties(getClass(), false); |
Andrea Campanella | 37f07e4 | 2021-02-16 11:24:39 +0100 | [diff] [blame] | 174 | super.deactivate(); |
| 175 | } |
| 176 | } |