blob: 733da9caedb789ff8183f90e1de9862b56e9f625 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080018import org.onlab.metrics.MetricsService;
Murat Parlakisik553db172015-04-08 03:29:04 -070019import org.onlab.util.SharedExecutors;
Yuta HIGUCHI45ff2d92017-06-08 16:32:00 -070020import org.onlab.util.SharedScheduledExecutors;
Jian Lid9b5f552016-03-11 18:15:31 -080021import org.onlab.util.Tools;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.onosproject.app.ApplicationIdStore;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080023import org.onosproject.app.ApplicationService;
Murat Parlakisik553db172015-04-08 03:29:04 -070024import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.CoreService;
27import org.onosproject.core.IdBlockStore;
28import org.onosproject.core.IdGenerator;
29import org.onosproject.core.Version;
Jordan Haltermanf70bf462017-07-29 13:12:00 -070030import org.onosproject.core.VersionService;
Thomas Vachuska36002e62015-05-19 16:12:29 -070031import org.onosproject.event.EventDeliveryService;
Murat Parlakisik553db172015-04-08 03:29:04 -070032import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import org.osgi.service.component.annotations.Activate;
34import org.osgi.service.component.annotations.Component;
35import org.osgi.service.component.annotations.Deactivate;
36import org.osgi.service.component.annotations.Modified;
37import org.osgi.service.component.annotations.Reference;
38import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
Thomas Vachuska36002e62015-05-19 16:12:29 -070041
Murat Parlakisik553db172015-04-08 03:29:04 -070042import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070043import java.util.Set;
44
45import static com.google.common.base.Preconditions.checkNotNull;
Changhoon Yoon541ef712015-05-23 17:18:34 +090046import static org.onosproject.security.AppGuard.checkPermission;
Jian Lid9b5f552016-03-11 18:15:31 -080047import static org.onosproject.security.AppPermission.Type.APP_READ;
48import static org.onosproject.security.AppPermission.Type.APP_WRITE;
Changhoon Yoon541ef712015-05-23 17:18:34 +090049
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070050/**
51 * Core service implementation.
52 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053@Component(immediate = true, service = CoreService.class)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070054public class CoreManager implements CoreService {
55
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070056 private final Logger log = LoggerFactory.getLogger(getClass());
57
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Haltermanf70bf462017-07-29 13:12:00 -070059 protected VersionService versionService;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070060
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070062 protected ApplicationIdStore applicationIdStore;
63
Ray Milkeyd84f89b2018-08-17 14:54:17 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor520c0522014-11-23 23:50:47 -080065 protected IdBlockStore idBlockStore;
66
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskac65dd712015-11-04 17:19:10 -080068 protected ApplicationService appService;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Murat Parlakisik553db172015-04-08 03:29:04 -070071 protected ComponentConfigService cfgService;
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska36002e62015-05-19 16:12:29 -070074 protected EventDeliveryService eventDeliveryService;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080077 protected MetricsService metricsService;
78
Thomas Vachuska36002e62015-05-19 16:12:29 -070079 private static final int DEFAULT_POOL_SIZE = 30;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 //@Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
81 // label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070082 private int sharedThreadPoolSize = DEFAULT_POOL_SIZE;
83
84 private static final int DEFAULT_EVENT_TIME = 2000;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070085 //@Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME,
86 // label = "Maximum number of millis an event sink has to process an event")
Thomas Vachuska36002e62015-05-19 16:12:29 -070087 private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
Murat Parlakisik553db172015-04-08 03:29:04 -070088
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080089 private static final boolean DEFAULT_PERFORMANCE_CHECK = false;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 //@Property(name = "sharedThreadPerformanceCheck", boolValue = DEFAULT_PERFORMANCE_CHECK,
91 // label = "Enable queue performance check on shared pool")
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080092 private boolean calculatePoolPerformance = DEFAULT_PERFORMANCE_CHECK;
93
94
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070095 @Activate
Thomas Vachuska0666f152016-08-05 12:03:54 -070096 protected void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070097 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070098 cfgService.registerProperties(getClass());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070099 }
100
Murat Parlakisik553db172015-04-08 03:29:04 -0700101 @Deactivate
Thomas Vachuska0666f152016-08-05 12:03:54 -0700102 protected void deactivate() {
Murat Parlakisik553db172015-04-08 03:29:04 -0700103 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700104 SharedExecutors.shutdown();
Yuta HIGUCHI45ff2d92017-06-08 16:32:00 -0700105 SharedScheduledExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700106 }
107
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700108 @Override
109 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900110 checkPermission(APP_READ);
Jordan Haltermanf70bf462017-07-29 13:12:00 -0700111 return versionService.version();
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700112 }
113
114 @Override
115 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900116 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700117 return applicationIdStore.getAppIds();
118 }
119
120 @Override
121 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900122 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700123 return applicationIdStore.getAppId(id);
124 }
125
126 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800127 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900128 checkPermission(APP_READ);
Ray Milkey02479862015-02-17 17:02:19 -0800129 return applicationIdStore.getAppId(name);
130 }
131
Ray Milkey02479862015-02-17 17:02:19 -0800132 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700133 public ApplicationId registerApplication(String name) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900134 checkPermission(APP_WRITE);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700135 checkNotNull(name, "Application ID cannot be null");
136 return applicationIdStore.registerApplication(name);
137 }
138
Brian O'Connor520c0522014-11-23 23:50:47 -0800139 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800140 public ApplicationId registerApplication(String name, Runnable preDeactivate) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900141 checkPermission(APP_WRITE);
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800142 ApplicationId id = registerApplication(name);
143 appService.registerDeactivateHook(id, preDeactivate);
144 return id;
145 }
146
147 @Override
Brian O'Connor520c0522014-11-23 23:50:47 -0800148 public IdGenerator getIdGenerator(String topic) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900149 checkPermission(APP_READ);
Brian O'Connor520c0522014-11-23 23:50:47 -0800150 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
151 return new BlockAllocatorBasedIdGenerator(allocator);
152 }
153
Murat Parlakisik553db172015-04-08 03:29:04 -0700154 @Modified
Thomas Vachuska0666f152016-08-05 12:03:54 -0700155 protected void modified(ComponentContext context) {
Murat Parlakisik553db172015-04-08 03:29:04 -0700156 Dictionary<?, ?> properties = context.getProperties();
Jian Lid9b5f552016-03-11 18:15:31 -0800157 Integer poolSize = Tools.getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700158
Thomas Vachuska36002e62015-05-19 16:12:29 -0700159 if (poolSize != null && poolSize > 1) {
160 sharedThreadPoolSize = poolSize;
161 SharedExecutors.setPoolSize(sharedThreadPoolSize);
162 } else if (poolSize != null) {
163 log.warn("sharedThreadPoolSize must be greater than 1");
164 }
165
Jian Lid9b5f552016-03-11 18:15:31 -0800166 Integer timeLimit = Tools.getIntegerProperty(properties, "maxEventTimeLimit");
Jonathan Hart943893f2016-04-08 13:38:54 -0700167 if (timeLimit != null && timeLimit >= 0) {
Thomas Vachuska36002e62015-05-19 16:12:29 -0700168 maxEventTimeLimit = timeLimit;
169 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
170 } else if (timeLimit != null) {
Jonathan Hart943893f2016-04-08 13:38:54 -0700171 log.warn("maxEventTimeLimit must be greater than or equal to 0");
Thomas Vachuska36002e62015-05-19 16:12:29 -0700172 }
173
Jian Lid9b5f552016-03-11 18:15:31 -0800174 Boolean performanceCheck = Tools.isPropertyEnabled(properties, "sharedThreadPerformanceCheck");
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800175 if (performanceCheck != null) {
176 calculatePoolPerformance = performanceCheck;
Thomas Vachuska0666f152016-08-05 12:03:54 -0700177 SharedExecutors.setMetricsService(calculatePoolPerformance ? metricsService : null);
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800178 }
179
180 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}, calculatePoolPerformance={}",
181 sharedThreadPoolSize, maxEventTimeLimit, calculatePoolPerformance);
Thomas Vachuska36002e62015-05-19 16:12:29 -0700182 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700183}