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