blob: f4fd64887cf735281464b3de723562035d83eb60 [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 Parlakisik553db172015-04-08 03:29:04 -070026import org.onlab.util.SharedExecutors;
Simon Hunt9fec43f2015-03-25 11:36:30 -070027import org.onlab.util.Tools;
Murat Parlakisik553db172015-04-08 03:29:04 -070028import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.ApplicationIdStore;
31import org.onosproject.core.CoreService;
32import org.onosproject.core.IdBlockStore;
33import org.onosproject.core.IdGenerator;
Changhoon Yoon541ef712015-05-23 17:18:34 +090034import org.onosproject.core.Permission;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import 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;
Murat Parlakisik553db172015-04-08 03:29:04 -070042import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070043import java.util.List;
44import java.util.Set;
45
46import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070047import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090048import static org.onosproject.security.AppGuard.checkPermission;
49
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070050
51/**
52 * Core service implementation.
53 */
Brian O'Connor520c0522014-11-23 23:50:47 -080054@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070055@Service
56public class CoreManager implements CoreService {
57
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070058 private final Logger log = LoggerFactory.getLogger(getClass());
59
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070060 private static final File VERSION_FILE = new File("../VERSION");
Brian O'Connor33503902015-06-01 17:08:05 -070061 private static Version version = Version.version("1.2.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070062
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ApplicationIdStore applicationIdStore;
65
Brian O'Connor520c0522014-11-23 23:50:47 -080066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected IdBlockStore idBlockStore;
68
Murat Parlakisik553db172015-04-08 03:29:04 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected ComponentConfigService cfgService;
71
Thomas Vachuska36002e62015-05-19 16:12:29 -070072 @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 Parlakisik553db172015-04-08 03:29:04 -070077 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070078 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 Parlakisik553db172015-04-08 03:29:04 -070084
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070085 @Activate
86 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070087 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070088 cfgService.registerProperties(getClass());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070089 List<String> versionLines = Tools.slurp(VERSION_FILE);
90 if (versionLines != null && !versionLines.isEmpty()) {
91 version = Version.version(versionLines.get(0));
92 }
93 }
94
Murat Parlakisik553db172015-04-08 03:29:04 -070095 @Deactivate
96 public void deactivate() {
97 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -070098 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -070099 }
100
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700101 @Override
102 public Version version() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900103 checkPermission(Permission.APP_READ);
104
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700105 return version;
106 }
107
108 @Override
109 public Set<ApplicationId> getAppIds() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900110 checkPermission(Permission.APP_READ);
111
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700112 return applicationIdStore.getAppIds();
113 }
114
115 @Override
116 public ApplicationId getAppId(Short id) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900117 checkPermission(Permission.APP_READ);
118
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700119 return applicationIdStore.getAppId(id);
120 }
121
122 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800123 public ApplicationId getAppId(String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900124 checkPermission(Permission.APP_READ);
125
Ray Milkey02479862015-02-17 17:02:19 -0800126 return applicationIdStore.getAppId(name);
127 }
128
129
130 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700131 public ApplicationId registerApplication(String name) {
132 checkNotNull(name, "Application ID cannot be null");
133 return applicationIdStore.registerApplication(name);
134 }
135
Brian O'Connor520c0522014-11-23 23:50:47 -0800136 @Override
137 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800138 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
139 return new BlockAllocatorBasedIdGenerator(allocator);
140 }
141
Murat Parlakisik553db172015-04-08 03:29:04 -0700142
143 @Modified
144 public void modified(ComponentContext context) {
145 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700146 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700147
Thomas Vachuska36002e62015-05-19 16:12:29 -0700148 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 Parlakisik553db172015-04-08 03:29:04 -0700166
167
168 /**
169 * Get Integer property from the propertyName
170 * Return null if propertyName is not found.
171 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700172 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700173 * @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 Vachuskae0f804a2014-10-27 23:40:48 -0700189}