blob: f6714d45e553468180498839248079d126b28f63 [file] [log] [blame]
Andrea Campanella37f07e42021-02-16 11:24:39 +01001/*
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
17package org.opencord.olt.driver;
18
Andrea Campanella438e1ad2021-03-26 11:41:16 +010019import org.onosproject.cfg.ComponentConfigService;
20import org.onosproject.cfg.ConfigProperty;
21import org.onosproject.cluster.ClusterService;
22import org.onosproject.cluster.LeadershipService;
23import org.onosproject.cluster.NodeId;
Andrea Campanella37f07e42021-02-16 11:24:39 +010024import org.onosproject.net.driver.AbstractDriverLoader;
Andrea Campanella438e1ad2021-03-26 11:41:16 +010025import org.osgi.service.component.ComponentContext;
Andrea Campanella37f07e42021-02-16 11:24:39 +010026import org.osgi.service.component.annotations.Component;
Andrea Campanella438e1ad2021-03-26 11:41:16 +010027import org.osgi.service.component.annotations.Modified;
28import org.osgi.service.component.annotations.Reference;
29import org.osgi.service.component.annotations.ReferenceCardinality;
Andrea Campanella37f07e42021-02-16 11:24:39 +010030import org.slf4j.Logger;
31
Andrea Campanella438e1ad2021-03-26 11:41:16 +010032import java.util.Dictionary;
33import java.util.Properties;
34import java.util.concurrent.TimeUnit;
35
36import static com.google.common.base.Strings.isNullOrEmpty;
37import static org.onlab.util.Tools.get;
38import static org.opencord.olt.impl.OsgiPropertyConstants.*;
Andrea Campanella37f07e42021-02-16 11:24:39 +010039import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Loader for olt device drivers.
43 */
Andrea Campanella438e1ad2021-03-26 11:41:16 +010044@Component(immediate = true, property = {
45 REQUIRED_DRIVERS_PROPERTY_DELAY + ":Integer=" + REQUIRED_DRIVERS_PROPERTY_DELAY_DEFAULT
46})
Andrea Campanella37f07e42021-02-16 11:24:39 +010047public class OltDriversLoader extends AbstractDriverLoader {
48
Andrea Campanella438e1ad2021-03-26 11:41:16 +010049 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 Campanella37f07e42021-02-16 11:24:39 +010070 private final Logger log = getLogger(getClass());
71
72 public OltDriversLoader() {
73 super("/olt-drivers.xml");
74 }
75
76 @Override
77 public void activate() {
Andrea Campanella438e1ad2021-03-26 11:41:16 +010078 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 Campanella37f07e42021-02-16 11:24:39 +010087 super.activate();
Andrea Campanella438e1ad2021-03-26 11:41:16 +010088 // 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 Campanella37f07e42021-02-16 11:24:39 +0100134 }
135
136 @Override
137 public void deactivate() {
Andrea Campanella438e1ad2021-03-26 11:41:16 +0100138 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 Campanella37f07e42021-02-16 11:24:39 +0100174 super.deactivate();
175 }
176}