blob: e82efd798367ed5cc6e7f60276c0934c5193b41e [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;
Ray Milkeyd04e2272018-10-16 18:20:18 -070046import static org.onosproject.net.OsgiPropertyConstants.CALCULATE_PERFORMANCE_CHECK;
47import static org.onosproject.net.OsgiPropertyConstants.CALCULATE_PERFORMANCE_CHECK_DEFAULT;
48import static org.onosproject.net.OsgiPropertyConstants.MAX_EVENT_TIME_LIMIT;
49import static org.onosproject.net.OsgiPropertyConstants.MAX_EVENT_TIME_LIMIT_DEFAULT;
50import static org.onosproject.net.OsgiPropertyConstants.SHARED_THREAD_POOL_SIZE;
51import static org.onosproject.net.OsgiPropertyConstants.SHARED_THREAD_POOL_SIZE_DEFAULT;
Changhoon Yoon541ef712015-05-23 17:18:34 +090052import static org.onosproject.security.AppGuard.checkPermission;
Jian Lid9b5f552016-03-11 18:15:31 -080053import static org.onosproject.security.AppPermission.Type.APP_READ;
54import static org.onosproject.security.AppPermission.Type.APP_WRITE;
Changhoon Yoon541ef712015-05-23 17:18:34 +090055
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070056/**
57 * Core service implementation.
58 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070059@Component(
60 immediate = true,
61 service = CoreService.class,
62 property = {
Ray Milkey2d7bca12018-10-17 14:51:52 -070063 SHARED_THREAD_POOL_SIZE + ":Integer=" + SHARED_THREAD_POOL_SIZE_DEFAULT,
64 MAX_EVENT_TIME_LIMIT + ":Integer=" + MAX_EVENT_TIME_LIMIT_DEFAULT,
65 CALCULATE_PERFORMANCE_CHECK + ":Boolean=" + CALCULATE_PERFORMANCE_CHECK_DEFAULT
Ray Milkeyd04e2272018-10-16 18:20:18 -070066 }
67)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070068public class CoreManager implements CoreService {
69
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070070 private final Logger log = LoggerFactory.getLogger(getClass());
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Haltermanf70bf462017-07-29 13:12:00 -070073 protected VersionService versionService;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070074
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070076 protected ApplicationIdStore applicationIdStore;
77
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor520c0522014-11-23 23:50:47 -080079 protected IdBlockStore idBlockStore;
80
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskac65dd712015-11-04 17:19:10 -080082 protected ApplicationService appService;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Murat Parlakisik553db172015-04-08 03:29:04 -070085 protected ComponentConfigService cfgService;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska36002e62015-05-19 16:12:29 -070088 protected EventDeliveryService eventDeliveryService;
89
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080091 protected MetricsService metricsService;
92
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070093 /** Configure shared pool maximum size. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070094 private int sharedThreadPoolSize = SHARED_THREAD_POOL_SIZE_DEFAULT;
Thomas Vachuska36002e62015-05-19 16:12:29 -070095
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070096 /** Maximum number of millis an event sink has to process an event. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070097 private int maxEventTimeLimit = MAX_EVENT_TIME_LIMIT_DEFAULT;
Murat Parlakisik553db172015-04-08 03:29:04 -070098
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070099 /** Enable queue performance check on shared pool. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700100 private boolean calculatePoolPerformance = CALCULATE_PERFORMANCE_CHECK_DEFAULT;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800101
102
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700103 @Activate
Thomas Vachuska0666f152016-08-05 12:03:54 -0700104 protected 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());
Ray Milkey2cd37362018-12-06 13:20:02 -0800107 log.info("ONOS starting up on Java version {}, JVM version {}",
108 System.getProperty("java.version"),
109 System.getProperty("java.vm.version"));
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700110 }
111
Murat Parlakisik553db172015-04-08 03:29:04 -0700112 @Deactivate
Thomas Vachuska0666f152016-08-05 12:03:54 -0700113 protected void deactivate() {
Murat Parlakisik553db172015-04-08 03:29:04 -0700114 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700115 SharedExecutors.shutdown();
Yuta HIGUCHI45ff2d92017-06-08 16:32:00 -0700116 SharedScheduledExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700117 }
118
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700119 @Override
120 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900121 checkPermission(APP_READ);
Jordan Haltermanf70bf462017-07-29 13:12:00 -0700122 return versionService.version();
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700123 }
124
125 @Override
126 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900127 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700128 return applicationIdStore.getAppIds();
129 }
130
131 @Override
132 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900133 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700134 return applicationIdStore.getAppId(id);
135 }
136
137 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800138 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900139 checkPermission(APP_READ);
Ray Milkey02479862015-02-17 17:02:19 -0800140 return applicationIdStore.getAppId(name);
141 }
142
Ray Milkey02479862015-02-17 17:02:19 -0800143 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700144 public ApplicationId registerApplication(String name) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900145 checkPermission(APP_WRITE);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700146 checkNotNull(name, "Application ID cannot be null");
147 return applicationIdStore.registerApplication(name);
148 }
149
Brian O'Connor520c0522014-11-23 23:50:47 -0800150 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800151 public ApplicationId registerApplication(String name, Runnable preDeactivate) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900152 checkPermission(APP_WRITE);
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800153 ApplicationId id = registerApplication(name);
154 appService.registerDeactivateHook(id, preDeactivate);
155 return id;
156 }
157
158 @Override
Brian O'Connor520c0522014-11-23 23:50:47 -0800159 public IdGenerator getIdGenerator(String topic) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900160 checkPermission(APP_READ);
Brian O'Connor520c0522014-11-23 23:50:47 -0800161 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
162 return new BlockAllocatorBasedIdGenerator(allocator);
163 }
164
Murat Parlakisik553db172015-04-08 03:29:04 -0700165 @Modified
Thomas Vachuska0666f152016-08-05 12:03:54 -0700166 protected void modified(ComponentContext context) {
Murat Parlakisik553db172015-04-08 03:29:04 -0700167 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700168 Integer poolSize = Tools.getIntegerProperty(properties, SHARED_THREAD_POOL_SIZE);
Murat Parlakisik553db172015-04-08 03:29:04 -0700169
Thomas Vachuska36002e62015-05-19 16:12:29 -0700170 if (poolSize != null && poolSize > 1) {
171 sharedThreadPoolSize = poolSize;
172 SharedExecutors.setPoolSize(sharedThreadPoolSize);
173 } else if (poolSize != null) {
174 log.warn("sharedThreadPoolSize must be greater than 1");
175 }
176
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700177 Integer timeLimit = Tools.getIntegerProperty(properties, MAX_EVENT_TIME_LIMIT);
Jonathan Hart943893f2016-04-08 13:38:54 -0700178 if (timeLimit != null && timeLimit >= 0) {
Thomas Vachuska36002e62015-05-19 16:12:29 -0700179 maxEventTimeLimit = timeLimit;
180 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
181 } else if (timeLimit != null) {
Jonathan Hart943893f2016-04-08 13:38:54 -0700182 log.warn("maxEventTimeLimit must be greater than or equal to 0");
Thomas Vachuska36002e62015-05-19 16:12:29 -0700183 }
184
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700185 Boolean performanceCheck = Tools.isPropertyEnabled(properties, CALCULATE_PERFORMANCE_CHECK);
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800186 if (performanceCheck != null) {
187 calculatePoolPerformance = performanceCheck;
Thomas Vachuska0666f152016-08-05 12:03:54 -0700188 SharedExecutors.setMetricsService(calculatePoolPerformance ? metricsService : null);
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800189 }
190
191 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}, calculatePoolPerformance={}",
192 sharedThreadPoolSize, maxEventTimeLimit, calculatePoolPerformance);
Thomas Vachuska36002e62015-05-19 16:12:29 -0700193 }
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700194}