blob: eae8cf6af4c2a3158e7de7e48c742b936b033451 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.core.impl;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070017
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
Thomas Vachuska36002e62015-05-19 16:12:29 -070020import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080026import org.onlab.metrics.MetricsService;
Murat Parlakisik553db172015-04-08 03:29:04 -070027import org.onlab.util.SharedExecutors;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080028import org.onosproject.app.ApplicationService;
Murat Parlakisik553db172015-04-08 03:29:04 -070029import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.ApplicationIdStore;
32import org.onosproject.core.CoreService;
33import org.onosproject.core.IdBlockStore;
34import org.onosproject.core.IdGenerator;
35import org.onosproject.core.Version;
Thomas Vachuska36002e62015-05-19 16:12:29 -070036import org.onosproject.event.EventDeliveryService;
Murat Parlakisik553db172015-04-08 03:29:04 -070037import org.osgi.service.component.ComponentContext;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
Thomas Vachuska36002e62015-05-19 16:12:29 -070040
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070041import java.io.File;
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -070042import java.io.IOException;
43import java.nio.file.Files;
44import java.nio.file.Path;
45import java.nio.file.Paths;
Murat Parlakisik553db172015-04-08 03:29:04 -070046import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070047import java.util.List;
48import java.util.Set;
49
50import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070051import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090052import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080053import static org.onosproject.security.AppPermission.Type.APP_READ;
Changhoon Yoonb856b812015-08-10 03:47:19 +090054
Changhoon Yoon541ef712015-05-23 17:18:34 +090055
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070056
57/**
58 * Core service implementation.
59 */
Brian O'Connor520c0522014-11-23 23:50:47 -080060@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070061@Service
62public class CoreManager implements CoreService {
63
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070064 private final Logger log = LoggerFactory.getLogger(getClass());
65
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070066 private static final File VERSION_FILE = new File("../VERSION");
Thomas Vachuska5d8994f2016-03-03 17:13:26 -080067 private static Version version = Version.version("1.5.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070068
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected ApplicationIdStore applicationIdStore;
71
Brian O'Connor520c0522014-11-23 23:50:47 -080072 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected IdBlockStore idBlockStore;
74
Murat Parlakisik553db172015-04-08 03:29:04 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskac65dd712015-11-04 17:19:10 -080076 protected ApplicationService appService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Murat Parlakisik553db172015-04-08 03:29:04 -070079 protected ComponentConfigService cfgService;
80
Thomas Vachuska36002e62015-05-19 16:12:29 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected EventDeliveryService eventDeliveryService;
83
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected MetricsService metricsService;
86
Thomas Vachuska36002e62015-05-19 16:12:29 -070087 private static final int DEFAULT_POOL_SIZE = 30;
88 @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070089 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070090 private int sharedThreadPoolSize = DEFAULT_POOL_SIZE;
91
92 private static final int DEFAULT_EVENT_TIME = 2000;
93 @Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME,
94 label = "Maximum number of millis an event sink has to process an event")
95 private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
Murat Parlakisik553db172015-04-08 03:29:04 -070096
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080097 private static final boolean DEFAULT_PERFORMANCE_CHECK = false;
98 @Property(name = "sharedThreadPerformanceCheck", boolValue = DEFAULT_PERFORMANCE_CHECK,
99 label = "Enable queue performance check on shared pool")
100 private boolean calculatePoolPerformance = DEFAULT_PERFORMANCE_CHECK;
101
102
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700103 @Activate
104 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -0700105 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -0700106 cfgService.registerProperties(getClass());
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -0700107 try {
108 Path path = Paths.get(VERSION_FILE.getPath());
109 List<String> versionLines = Files.readAllLines(path);
110 if (versionLines != null && !versionLines.isEmpty()) {
111 version = Version.version(versionLines.get(0));
112 }
113 } catch (IOException e) {
114 // version file not found, using default
115 log.trace("Version file not found", e);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700116 }
117 }
118
Murat Parlakisik553db172015-04-08 03:29:04 -0700119 @Deactivate
120 public void deactivate() {
121 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700122 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700123 }
124
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700125 @Override
126 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900127 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700128 return version;
129 }
130
131 @Override
132 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900133 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700134 return applicationIdStore.getAppIds();
135 }
136
137 @Override
138 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900139 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700140 return applicationIdStore.getAppId(id);
141 }
142
143 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800144 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900145 checkPermission(APP_READ);
Ray Milkey02479862015-02-17 17:02:19 -0800146 return applicationIdStore.getAppId(name);
147 }
148
149
150 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700151 public ApplicationId registerApplication(String name) {
152 checkNotNull(name, "Application ID cannot be null");
153 return applicationIdStore.registerApplication(name);
154 }
155
Brian O'Connor520c0522014-11-23 23:50:47 -0800156 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800157 public ApplicationId registerApplication(String name, Runnable preDeactivate) {
158 ApplicationId id = registerApplication(name);
159 appService.registerDeactivateHook(id, preDeactivate);
160 return id;
161 }
162
163 @Override
Brian O'Connor520c0522014-11-23 23:50:47 -0800164 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800165 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
166 return new BlockAllocatorBasedIdGenerator(allocator);
167 }
168
Murat Parlakisik553db172015-04-08 03:29:04 -0700169
170 @Modified
171 public void modified(ComponentContext context) {
172 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700173 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700174
Thomas Vachuska36002e62015-05-19 16:12:29 -0700175 if (poolSize != null && poolSize > 1) {
176 sharedThreadPoolSize = poolSize;
177 SharedExecutors.setPoolSize(sharedThreadPoolSize);
178 } else if (poolSize != null) {
179 log.warn("sharedThreadPoolSize must be greater than 1");
180 }
181
182 Integer timeLimit = getIntegerProperty(properties, "maxEventTimeLimit");
183 if (timeLimit != null && timeLimit > 1) {
184 maxEventTimeLimit = timeLimit;
185 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
186 } else if (timeLimit != null) {
187 log.warn("maxEventTimeLimit must be greater than 1");
188 }
189
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800190 Boolean performanceCheck = isPropertyEnabled(properties, "sharedThreadPerformanceCheck");
191 if (performanceCheck != null) {
192 calculatePoolPerformance = performanceCheck;
193 SharedExecutors.setCalculatePoolPerformance(calculatePoolPerformance, metricsService);
194 }
195
196 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}, calculatePoolPerformance={}",
197 sharedThreadPoolSize, maxEventTimeLimit, calculatePoolPerformance);
Thomas Vachuska36002e62015-05-19 16:12:29 -0700198 }
Murat Parlakisik553db172015-04-08 03:29:04 -0700199
200
201 /**
202 * Get Integer property from the propertyName
203 * Return null if propertyName is not found.
204 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700205 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700206 * @param propertyName the name of the property to look up
207 * @return value when the propertyName is defined or return null
208 */
209 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
210 String propertyName) {
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800211 Integer value;
Murat Parlakisik553db172015-04-08 03:29:04 -0700212 try {
213 String s = (String) properties.get(propertyName);
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800214 value = isNullOrEmpty(s) ? null : Integer.parseInt(s.trim());
Murat Parlakisik553db172015-04-08 03:29:04 -0700215 } catch (NumberFormatException | ClassCastException e) {
216 value = null;
217 }
218 return value;
219 }
220
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800221 /**
222 * Check property name is defined and set to true.
223 *
224 * @param properties properties to be looked up
225 * @param propertyName the name of the property to look up
226 * @return value when the propertyName is defined or return null
227 */
228 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
229 String propertyName) {
230 Boolean value = null;
231 try {
232 String s = (String) properties.get(propertyName);
233 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
234 } catch (ClassCastException e) {
235 // No propertyName defined.
236 value = null;
237 }
238 return value;
239 }
240
241
Murat Parlakisik553db172015-04-08 03:29:04 -0700242
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700243}