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