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; |
| 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 22 | import org.apache.felix.scr.annotations.Service; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Property; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Modified; |
| 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; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.ComponentContext; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 38 | import java.io.File; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 39 | import java.util.Dictionary; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 44 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Core service implementation. |
| 48 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 49 | @Component(immediate = true) |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 50 | @Service |
| 51 | public class CoreManager implements CoreService { |
| 52 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 55 | private static final File VERSION_FILE = new File("../VERSION"); |
Simon Hunt | 9fec43f | 2015-03-25 11:36:30 -0700 | [diff] [blame] | 56 | private static Version version = Version.version("1.2.0-SNAPSHOT"); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected ApplicationIdStore applicationIdStore; |
| 60 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected IdBlockStore idBlockStore; |
| 63 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected ComponentConfigService cfgService; |
| 66 | |
Jon Hall | e3f0fcf | 2015-04-10 09:34:23 -0700 | [diff] [blame] | 67 | @Property(name = "sharedThreadPoolSize", intValue = 30, |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 68 | label = "Configure shared pool maximum size ") |
Jon Hall | e3f0fcf | 2015-04-10 09:34:23 -0700 | [diff] [blame] | 69 | private int sharedThreadPoolSize = 30; |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 70 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 71 | @Activate |
| 72 | public void activate() { |
Thomas Vachuska | 6cba495 | 2015-04-22 12:38:22 -0700 | [diff] [blame^] | 73 | registerApplication(CORE_APP_NAME); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 74 | cfgService.registerProperties(getClass()); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 75 | List<String> versionLines = Tools.slurp(VERSION_FILE); |
| 76 | if (versionLines != null && !versionLines.isEmpty()) { |
| 77 | version = Version.version(versionLines.get(0)); |
| 78 | } |
| 79 | } |
| 80 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 81 | @Deactivate |
| 82 | public void deactivate() { |
| 83 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | b0317c6 | 2015-04-08 23:58:58 -0700 | [diff] [blame] | 84 | SharedExecutors.shutdown(); |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public Version version() { |
| 89 | return version; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Set<ApplicationId> getAppIds() { |
| 94 | return applicationIdStore.getAppIds(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public ApplicationId getAppId(Short id) { |
| 99 | return applicationIdStore.getAppId(id); |
| 100 | } |
| 101 | |
| 102 | @Override |
Ray Milkey | 0247986 | 2015-02-17 17:02:19 -0800 | [diff] [blame] | 103 | public ApplicationId getAppId(String name) { |
| 104 | return applicationIdStore.getAppId(name); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | @Override |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 109 | public ApplicationId registerApplication(String name) { |
| 110 | checkNotNull(name, "Application ID cannot be null"); |
| 111 | return applicationIdStore.registerApplication(name); |
| 112 | } |
| 113 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 114 | @Override |
| 115 | public IdGenerator getIdGenerator(String topic) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 116 | IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore); |
| 117 | return new BlockAllocatorBasedIdGenerator(allocator); |
| 118 | } |
| 119 | |
Murat Parlakisik | 553db17 | 2015-04-08 03:29:04 -0700 | [diff] [blame] | 120 | |
| 121 | @Modified |
| 122 | public void modified(ComponentContext context) { |
| 123 | Dictionary<?, ?> properties = context.getProperties(); |
| 124 | Integer sharedThreadPoolSizeConfig = |
| 125 | getIntegerProperty(properties, "sharedThreadPoolSize"); |
| 126 | if (sharedThreadPoolSizeConfig == null) { |
| 127 | log.info("Shared Pool Size is not configured, default value is {}", |
| 128 | sharedThreadPoolSize); |
| 129 | } else { |
| 130 | if (sharedThreadPoolSizeConfig > 0) { |
| 131 | sharedThreadPoolSize = sharedThreadPoolSizeConfig; |
| 132 | SharedExecutors.setPoolSize(sharedThreadPoolSize); |
| 133 | log.info("Configured. Shared Pool Size is configured to {}", |
| 134 | sharedThreadPoolSize); |
| 135 | } else { |
| 136 | log.warn("Shared Pool Size size must be greater than 0"); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Get Integer property from the propertyName |
| 145 | * Return null if propertyName is not found. |
| 146 | * |
| 147 | * @param properties properties to be looked up |
| 148 | * @param propertyName the name of the property to look up |
| 149 | * @return value when the propertyName is defined or return null |
| 150 | */ |
| 151 | private static Integer getIntegerProperty(Dictionary<?, ?> properties, |
| 152 | String propertyName) { |
| 153 | Integer value = null; |
| 154 | try { |
| 155 | String s = (String) properties.get(propertyName); |
| 156 | value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim()); |
| 157 | } catch (NumberFormatException | ClassCastException e) { |
| 158 | value = null; |
| 159 | } |
| 160 | return value; |
| 161 | } |
| 162 | |
| 163 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 164 | } |