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