blob: b1e781ab68c6ffb74ef8f7c5bd1e01fd82be3cab [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.core.impl;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070017
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
Thomas Vachuska36002e62015-05-19 16:12:29 -070020import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080026import org.onlab.metrics.MetricsService;
Murat Parlakisik553db172015-04-08 03:29:04 -070027import org.onlab.util.SharedExecutors;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080028import org.onosproject.app.ApplicationService;
Murat Parlakisik553db172015-04-08 03:29:04 -070029import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.ApplicationIdStore;
32import org.onosproject.core.CoreService;
33import org.onosproject.core.IdBlockStore;
34import org.onosproject.core.IdGenerator;
35import org.onosproject.core.Version;
Thomas Vachuska36002e62015-05-19 16:12:29 -070036import org.onosproject.event.EventDeliveryService;
Murat Parlakisik553db172015-04-08 03:29:04 -070037import org.osgi.service.component.ComponentContext;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
Thomas Vachuska36002e62015-05-19 16:12:29 -070040
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070041import java.io.File;
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -070042import java.io.IOException;
43import java.nio.file.Files;
44import java.nio.file.Path;
45import java.nio.file.Paths;
Murat Parlakisik553db172015-04-08 03:29:04 -070046import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070047import java.util.List;
48import java.util.Set;
49
50import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070051import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090052import static org.onosproject.security.AppGuard.checkPermission;
Heedo Kang4a47a302016-02-29 17:40:23 +090053import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090054
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070055
56/**
57 * Core service implementation.
58 */
Brian O'Connor520c0522014-11-23 23:50:47 -080059@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070060@Service
61public class CoreManager implements CoreService {
62
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070063 private final Logger log = LoggerFactory.getLogger(getClass());
64
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070065 private static final File VERSION_FILE = new File("../VERSION");
Brian O'Connor955c3162016-03-10 15:27:19 -080066 private static Version version = Version.version("1.6.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070067
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected ApplicationIdStore applicationIdStore;
70
Brian O'Connor520c0522014-11-23 23:50:47 -080071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected IdBlockStore idBlockStore;
73
Murat Parlakisik553db172015-04-08 03:29:04 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskac65dd712015-11-04 17:19:10 -080075 protected ApplicationService appService;
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Murat Parlakisik553db172015-04-08 03:29:04 -070078 protected ComponentConfigService cfgService;
79
Thomas Vachuska36002e62015-05-19 16:12:29 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected EventDeliveryService eventDeliveryService;
82
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected MetricsService metricsService;
85
Thomas Vachuska36002e62015-05-19 16:12:29 -070086 private static final int DEFAULT_POOL_SIZE = 30;
87 @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070088 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070089 private int sharedThreadPoolSize = DEFAULT_POOL_SIZE;
90
91 private static final int DEFAULT_EVENT_TIME = 2000;
92 @Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME,
93 label = "Maximum number of millis an event sink has to process an event")
94 private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
Murat Parlakisik553db172015-04-08 03:29:04 -070095
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080096 private static final boolean DEFAULT_PERFORMANCE_CHECK = false;
97 @Property(name = "sharedThreadPerformanceCheck", boolValue = DEFAULT_PERFORMANCE_CHECK,
98 label = "Enable queue performance check on shared pool")
99 private boolean calculatePoolPerformance = DEFAULT_PERFORMANCE_CHECK;
100
101
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700102 @Activate
103 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -0700104 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -0700105 cfgService.registerProperties(getClass());
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -0700106 try {
107 Path path = Paths.get(VERSION_FILE.getPath());
108 List<String> versionLines = Files.readAllLines(path);
109 if (versionLines != null && !versionLines.isEmpty()) {
110 version = Version.version(versionLines.get(0));
111 }
112 } catch (IOException e) {
113 // version file not found, using default
114 log.trace("Version file not found", e);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700115 }
116 }
117
Murat Parlakisik553db172015-04-08 03:29:04 -0700118 @Deactivate
119 public void deactivate() {
120 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700121 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700122 }
123
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700124 @Override
125 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900126 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700127 return version;
128 }
129
130 @Override
131 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900132 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700133 return applicationIdStore.getAppIds();
134 }
135
136 @Override
137 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900138 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700139 return applicationIdStore.getAppId(id);
140 }
141
142 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800143 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900144 checkPermission(APP_READ);
Ray Milkey02479862015-02-17 17:02:19 -0800145 return applicationIdStore.getAppId(name);
146 }
147
148
149 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700150 public ApplicationId registerApplication(String name) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900151 checkPermission(APP_WRITE);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700152 checkNotNull(name, "Application ID cannot be null");
153 return applicationIdStore.registerApplication(name);
154 }
155
Brian O'Connor520c0522014-11-23 23:50:47 -0800156 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800157 public ApplicationId registerApplication(String name, Runnable preDeactivate) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900158 checkPermission(APP_WRITE);
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800159 ApplicationId id = registerApplication(name);
160 appService.registerDeactivateHook(id, preDeactivate);
161 return id;
162 }
163
164 @Override
Brian O'Connor520c0522014-11-23 23:50:47 -0800165 public IdGenerator getIdGenerator(String topic) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900166 checkPermission(APP_READ);
Brian O'Connor520c0522014-11-23 23:50:47 -0800167 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
168 return new BlockAllocatorBasedIdGenerator(allocator);
169 }
170
Murat Parlakisik553db172015-04-08 03:29:04 -0700171
172 @Modified
173 public void modified(ComponentContext context) {
174 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700175 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700176
Thomas Vachuska36002e62015-05-19 16:12:29 -0700177 if (poolSize != null && poolSize > 1) {
178 sharedThreadPoolSize = poolSize;
179 SharedExecutors.setPoolSize(sharedThreadPoolSize);
180 } else if (poolSize != null) {
181 log.warn("sharedThreadPoolSize must be greater than 1");
182 }
183
184 Integer timeLimit = getIntegerProperty(properties, "maxEventTimeLimit");
185 if (timeLimit != null && timeLimit > 1) {
186 maxEventTimeLimit = timeLimit;
187 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
188 } else if (timeLimit != null) {
189 log.warn("maxEventTimeLimit must be greater than 1");
190 }
191
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800192 Boolean performanceCheck = isPropertyEnabled(properties, "sharedThreadPerformanceCheck");
193 if (performanceCheck != null) {
194 calculatePoolPerformance = performanceCheck;
195 SharedExecutors.setCalculatePoolPerformance(calculatePoolPerformance, metricsService);
196 }
197
198 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}, calculatePoolPerformance={}",
199 sharedThreadPoolSize, maxEventTimeLimit, calculatePoolPerformance);
Thomas Vachuska36002e62015-05-19 16:12:29 -0700200 }
Murat Parlakisik553db172015-04-08 03:29:04 -0700201
202
203 /**
204 * Get Integer property from the propertyName
205 * Return null if propertyName is not found.
206 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700207 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700208 * @param propertyName the name of the property to look up
209 * @return value when the propertyName is defined or return null
210 */
211 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
212 String propertyName) {
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800213 Integer value;
Murat Parlakisik553db172015-04-08 03:29:04 -0700214 try {
215 String s = (String) properties.get(propertyName);
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800216 value = isNullOrEmpty(s) ? null : Integer.parseInt(s.trim());
Murat Parlakisik553db172015-04-08 03:29:04 -0700217 } catch (NumberFormatException | ClassCastException e) {
218 value = null;
219 }
220 return value;
221 }
222
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800223 /**
224 * Check property name is defined and set to true.
225 *
226 * @param properties properties to be looked up
227 * @param propertyName the name of the property to look up
228 * @return value when the propertyName is defined or return null
229 */
230 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
231 String propertyName) {
232 Boolean value = null;
233 try {
234 String s = (String) properties.get(propertyName);
235 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
236 } catch (ClassCastException e) {
237 // No propertyName defined.
238 value = null;
239 }
240 return value;
241 }
242
243
Murat Parlakisik553db172015-04-08 03:29:04 -0700244
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700245}