Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.core.impl; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 17 | |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 18 | import org.onlab.metrics.MetricsService; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 19 | import org.onlab.util.SharedExecutors; |
Yuta HIGUCHI | 45ff2d9 | 2017-06-08 16:32:00 -0700 | [diff] [blame] | 20 | import org.onlab.util.SharedScheduledExecutors; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 21 | import org.onlab.util.Tools; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.onosproject.app.ApplicationIdStore; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 23 | import org.onosproject.app.ApplicationService; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 24 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.IdBlockStore; |
| 28 | import org.onosproject.core.IdGenerator; |
| 29 | import org.onosproject.core.Version; |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 30 | import org.onosproject.core.VersionService; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 31 | import org.onosproject.event.EventDeliveryService; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | import org.osgi.service.component.annotations.Activate; |
| 34 | import org.osgi.service.component.annotations.Component; |
| 35 | import org.osgi.service.component.annotations.Deactivate; |
| 36 | import org.osgi.service.component.annotations.Modified; |
| 37 | import org.osgi.service.component.annotations.Reference; |
| 38 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 41 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 42 | import java.util.Dictionary; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 43 | import java.util.Set; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 46 | import static org.onosproject.net.OsgiPropertyConstants.CALCULATE_PERFORMANCE_CHECK; |
| 47 | import static org.onosproject.net.OsgiPropertyConstants.CALCULATE_PERFORMANCE_CHECK_DEFAULT; |
| 48 | import static org.onosproject.net.OsgiPropertyConstants.MAX_EVENT_TIME_LIMIT; |
| 49 | import static org.onosproject.net.OsgiPropertyConstants.MAX_EVENT_TIME_LIMIT_DEFAULT; |
| 50 | import static org.onosproject.net.OsgiPropertyConstants.SHARED_THREAD_POOL_SIZE; |
| 51 | import static org.onosproject.net.OsgiPropertyConstants.SHARED_THREAD_POOL_SIZE_DEFAULT; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 52 | import static org.onosproject.security.AppGuard.checkPermission; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 53 | import static org.onosproject.security.AppPermission.Type.APP_READ; |
| 54 | import static org.onosproject.security.AppPermission.Type.APP_WRITE; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 55 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Core service implementation. |
| 58 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 59 | @Component( |
| 60 | immediate = true, |
| 61 | service = CoreService.class, |
| 62 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 63 | 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 Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 66 | } |
| 67 | ) |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 68 | public class CoreManager implements CoreService { |
| 69 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 70 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 73 | protected VersionService versionService; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 76 | protected ApplicationIdStore applicationIdStore; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 79 | protected IdBlockStore idBlockStore; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 82 | protected ApplicationService appService; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 85 | protected ComponentConfigService cfgService; |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 88 | protected EventDeliveryService eventDeliveryService; |
| 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 91 | protected MetricsService metricsService; |
| 92 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 93 | /** Configure shared pool maximum size. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 94 | private int sharedThreadPoolSize = SHARED_THREAD_POOL_SIZE_DEFAULT; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 95 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 96 | /** Maximum number of millis an event sink has to process an event. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 97 | private int maxEventTimeLimit = MAX_EVENT_TIME_LIMIT_DEFAULT; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 98 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 99 | /** Enable queue performance check on shared pool. */ |
Ray Milkey | bd508ed | 2019-03-19 14:22:02 -0700 | [diff] [blame] | 100 | private boolean sharedThreadPerformanceCheck = CALCULATE_PERFORMANCE_CHECK_DEFAULT; |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 101 | |
| 102 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 103 | @Activate |
Thomas Vachuska | 0666f15 | 2016-08-05 12:03:54 -0700 | [diff] [blame] | 104 | protected void activate() { |
Thomas Vachuska | 6cba495 | 2015-04-22 12:38:22 -0700 | [diff] [blame] | 105 | registerApplication(CORE_APP_NAME); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 106 | cfgService.registerProperties(getClass()); |
Ray Milkey | 2cd3736 | 2018-12-06 13:20:02 -0800 | [diff] [blame] | 107 | log.info("ONOS starting up on Java version {}, JVM version {}", |
| 108 | System.getProperty("java.version"), |
| 109 | System.getProperty("java.vm.version")); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 112 | @Deactivate |
Thomas Vachuska | 0666f15 | 2016-08-05 12:03:54 -0700 | [diff] [blame] | 113 | protected void deactivate() { |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 114 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 115 | SharedExecutors.shutdown(); |
Yuta HIGUCHI | 45ff2d9 | 2017-06-08 16:32:00 -0700 | [diff] [blame] | 116 | SharedScheduledExecutors.shutdown(); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public Version version() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 121 | checkPermission(APP_READ); |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 122 | return versionService.version(); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Set<ApplicationId> getAppIds() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 127 | checkPermission(APP_READ); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 128 | return applicationIdStore.getAppIds(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public ApplicationId getAppId(Short id) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 133 | checkPermission(APP_READ); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 134 | return applicationIdStore.getAppId(id); |
| 135 | } |
| 136 | |
| 137 | @Override |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 138 | public ApplicationId getAppId(String name) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 139 | checkPermission(APP_READ); |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 140 | return applicationIdStore.getAppId(name); |
| 141 | } |
| 142 | |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 143 | @Override |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 144 | public ApplicationId registerApplication(String name) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 145 | checkPermission(APP_WRITE); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 146 | checkNotNull(name, "Application ID cannot be null"); |
| 147 | return applicationIdStore.registerApplication(name); |
| 148 | } |
| 149 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 150 | @Override |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 151 | public ApplicationId registerApplication(String name, Runnable preDeactivate) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 152 | checkPermission(APP_WRITE); |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 153 | ApplicationId id = registerApplication(name); |
| 154 | appService.registerDeactivateHook(id, preDeactivate); |
| 155 | return id; |
| 156 | } |
| 157 | |
| 158 | @Override |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 159 | public IdGenerator getIdGenerator(String topic) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 160 | checkPermission(APP_READ); |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 161 | IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore); |
| 162 | return new BlockAllocatorBasedIdGenerator(allocator); |
| 163 | } |
| 164 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 165 | @Modified |
Thomas Vachuska | 0666f15 | 2016-08-05 12:03:54 -0700 | [diff] [blame] | 166 | protected void modified(ComponentContext context) { |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 167 | Dictionary<?, ?> properties = context.getProperties(); |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 168 | Integer poolSize = Tools.getIntegerProperty(properties, SHARED_THREAD_POOL_SIZE); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 169 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 170 | 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 Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 177 | Integer timeLimit = Tools.getIntegerProperty(properties, MAX_EVENT_TIME_LIMIT); |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 178 | if (timeLimit != null && timeLimit >= 0) { |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 179 | maxEventTimeLimit = timeLimit; |
| 180 | eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit); |
| 181 | } else if (timeLimit != null) { |
Jonathan Hart | 943893f | 2016-04-08 13:38:54 -0700 | [diff] [blame] | 182 | log.warn("maxEventTimeLimit must be greater than or equal to 0"); |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 185 | Boolean performanceCheck = Tools.isPropertyEnabled(properties, CALCULATE_PERFORMANCE_CHECK); |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 186 | if (performanceCheck != null) { |
Ray Milkey | bd508ed | 2019-03-19 14:22:02 -0700 | [diff] [blame] | 187 | sharedThreadPerformanceCheck = performanceCheck; |
| 188 | SharedExecutors.setMetricsService(sharedThreadPerformanceCheck ? metricsService : null); |
Murat Parlakisik | dc17f7b | 2016-01-26 12:08:35 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Ray Milkey | bd508ed | 2019-03-19 14:22:02 -0700 | [diff] [blame] | 191 | log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}, sharedThreadPerformanceCheck={}", |
| 192 | sharedThreadPoolSize, maxEventTimeLimit, sharedThreadPerformanceCheck); |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 193 | } |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 194 | } |