Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 26 | import org.onlab.util.SharedExecutors; |
Simon Hunt | 9fec43f | 2015-03-25 11:36:30 -0700 | [diff] [blame] | 27 | import org.onlab.util.Tools; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 28 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.ApplicationIdStore; |
| 31 | import org.onosproject.core.CoreService; |
| 32 | import org.onosproject.core.IdBlockStore; |
| 33 | import org.onosproject.core.IdGenerator; |
| 34 | import org.onosproject.core.Version; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 35 | import org.onosproject.event.EventDeliveryService; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 36 | import org.osgi.service.component.ComponentContext; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 39 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 40 | import java.io.File; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 41 | import java.util.Dictionary; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 46 | import static com.google.common.base.Strings.isNullOrEmpty; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 47 | import static org.onosproject.security.AppGuard.checkPermission; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 48 | import static org.onosproject.security.AppPermission.Type.*; |
| 49 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 50 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Core service implementation. |
| 54 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 55 | @Component(immediate = true) |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 56 | @Service |
| 57 | public class CoreManager implements CoreService { |
| 58 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 61 | private static final File VERSION_FILE = new File("../VERSION"); |
Brian O'Connor | 6ee05b6 | 2015-09-18 13:42:12 -0700 | [diff] [blame^] | 62 | private static Version version = Version.version("1.3.0"); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected ApplicationIdStore applicationIdStore; |
| 66 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected IdBlockStore idBlockStore; |
| 69 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected ComponentConfigService cfgService; |
| 72 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected EventDeliveryService eventDeliveryService; |
| 75 | |
| 76 | private static final int DEFAULT_POOL_SIZE = 30; |
| 77 | @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE, |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 78 | label = "Configure shared pool maximum size ") |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 79 | private int sharedThreadPoolSize = DEFAULT_POOL_SIZE; |
| 80 | |
| 81 | private static final int DEFAULT_EVENT_TIME = 2000; |
| 82 | @Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME, |
| 83 | label = "Maximum number of millis an event sink has to process an event") |
| 84 | private int maxEventTimeLimit = DEFAULT_EVENT_TIME; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 85 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 86 | @Activate |
| 87 | public void activate() { |
Thomas Vachuska | 6cba495 | 2015-04-22 12:38:22 -0700 | [diff] [blame] | 88 | registerApplication(CORE_APP_NAME); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 89 | cfgService.registerProperties(getClass()); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 90 | List<String> versionLines = Tools.slurp(VERSION_FILE); |
| 91 | if (versionLines != null && !versionLines.isEmpty()) { |
| 92 | version = Version.version(versionLines.get(0)); |
| 93 | } |
| 94 | } |
| 95 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 96 | @Deactivate |
| 97 | public void deactivate() { |
| 98 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 99 | SharedExecutors.shutdown(); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public Version version() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 104 | checkPermission(APP_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 105 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 106 | return version; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Set<ApplicationId> getAppIds() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 111 | checkPermission(APP_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 112 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 113 | return applicationIdStore.getAppIds(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public ApplicationId getAppId(Short id) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 118 | checkPermission(APP_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 119 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 120 | return applicationIdStore.getAppId(id); |
| 121 | } |
| 122 | |
| 123 | @Override |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 124 | public ApplicationId getAppId(String name) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 125 | checkPermission(APP_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 126 | |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 127 | return applicationIdStore.getAppId(name); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | @Override |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 132 | public ApplicationId registerApplication(String name) { |
| 133 | checkNotNull(name, "Application ID cannot be null"); |
| 134 | return applicationIdStore.registerApplication(name); |
| 135 | } |
| 136 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 137 | @Override |
| 138 | public IdGenerator getIdGenerator(String topic) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 139 | IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore); |
| 140 | return new BlockAllocatorBasedIdGenerator(allocator); |
| 141 | } |
| 142 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 143 | |
| 144 | @Modified |
| 145 | public void modified(ComponentContext context) { |
| 146 | Dictionary<?, ?> properties = context.getProperties(); |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 147 | Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize"); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 148 | |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 149 | if (poolSize != null && poolSize > 1) { |
| 150 | sharedThreadPoolSize = poolSize; |
| 151 | SharedExecutors.setPoolSize(sharedThreadPoolSize); |
| 152 | } else if (poolSize != null) { |
| 153 | log.warn("sharedThreadPoolSize must be greater than 1"); |
| 154 | } |
| 155 | |
| 156 | Integer timeLimit = getIntegerProperty(properties, "maxEventTimeLimit"); |
| 157 | if (timeLimit != null && timeLimit > 1) { |
| 158 | maxEventTimeLimit = timeLimit; |
| 159 | eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit); |
| 160 | } else if (timeLimit != null) { |
| 161 | log.warn("maxEventTimeLimit must be greater than 1"); |
| 162 | } |
| 163 | |
| 164 | log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}", |
| 165 | sharedThreadPoolSize, maxEventTimeLimit); |
| 166 | } |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | /** |
| 170 | * Get Integer property from the propertyName |
| 171 | * Return null if propertyName is not found. |
| 172 | * |
Thomas Vachuska | 36002e6 | 2015-05-19 16:12:29 -0700 | [diff] [blame] | 173 | * @param properties properties to be looked up |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 174 | * @param propertyName the name of the property to look up |
| 175 | * @return value when the propertyName is defined or return null |
| 176 | */ |
| 177 | private static Integer getIntegerProperty(Dictionary<?, ?> properties, |
| 178 | String propertyName) { |
| 179 | Integer value = null; |
| 180 | try { |
| 181 | String s = (String) properties.get(propertyName); |
| 182 | value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim()); |
| 183 | } catch (NumberFormatException | ClassCastException e) { |
| 184 | value = null; |
| 185 | } |
| 186 | return value; |
| 187 | } |
| 188 | |
| 189 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 190 | } |