blob: 0761229279b1d1a18411bd87e8b7112c08af11dc [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;
34import org.onosproject.core.Version;
Thomas Vachuska36002e62015-05-19 16:12:29 -070035import org.onosproject.event.EventDeliveryService;
Murat Parlakisik553db172015-04-08 03:29:04 -070036import org.osgi.service.component.ComponentContext;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
Thomas Vachuska36002e62015-05-19 16:12:29 -070039
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070040import java.io.File;
Murat Parlakisik553db172015-04-08 03:29:04 -070041import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070042import java.util.List;
43import java.util.Set;
44
45import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070046import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090047import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090048import static org.onosproject.security.AppPermission.Type.*;
49
Changhoon Yoon541ef712015-05-23 17:18:34 +090050
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070051
52/**
53 * Core service implementation.
54 */
Brian O'Connor520c0522014-11-23 23:50:47 -080055@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070056@Service
57public class CoreManager implements CoreService {
58
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070059 private final Logger log = LoggerFactory.getLogger(getClass());
60
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070061 private static final File VERSION_FILE = new File("../VERSION");
Brian O'Connore8bcb702015-09-04 23:55:47 -070062 private static Version version = Version.version("1.3.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070063
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ApplicationIdStore applicationIdStore;
66
Brian O'Connor520c0522014-11-23 23:50:47 -080067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected IdBlockStore idBlockStore;
69
Murat Parlakisik553db172015-04-08 03:29:04 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected ComponentConfigService cfgService;
72
Thomas Vachuska36002e62015-05-19 16:12:29 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected EventDeliveryService eventDeliveryService;
75
76 private static final int DEFAULT_POOL_SIZE = 30;
77 @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070078 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070079 private int sharedThreadPoolSize = DEFAULT_POOL_SIZE;
80
81 private static final int DEFAULT_EVENT_TIME = 2000;
82 @Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME,
83 label = "Maximum number of millis an event sink has to process an event")
84 private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
Murat Parlakisik553db172015-04-08 03:29:04 -070085
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070086 @Activate
87 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070088 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070089 cfgService.registerProperties(getClass());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070090 List<String> versionLines = Tools.slurp(VERSION_FILE);
91 if (versionLines != null && !versionLines.isEmpty()) {
92 version = Version.version(versionLines.get(0));
93 }
94 }
95
Murat Parlakisik553db172015-04-08 03:29:04 -070096 @Deactivate
97 public void deactivate() {
98 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -070099 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700100 }
101
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700102 @Override
103 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900104 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900105
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700106 return version;
107 }
108
109 @Override
110 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900111 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900112
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700113 return applicationIdStore.getAppIds();
114 }
115
116 @Override
117 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900118 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900119
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700120 return applicationIdStore.getAppId(id);
121 }
122
123 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800124 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900125 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900126
Ray Milkey02479862015-02-17 17:02:19 -0800127 return applicationIdStore.getAppId(name);
128 }
129
130
131 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700132 public ApplicationId registerApplication(String name) {
133 checkNotNull(name, "Application ID cannot be null");
134 return applicationIdStore.registerApplication(name);
135 }
136
Brian O'Connor520c0522014-11-23 23:50:47 -0800137 @Override
138 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800139 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
140 return new BlockAllocatorBasedIdGenerator(allocator);
141 }
142
Murat Parlakisik553db172015-04-08 03:29:04 -0700143
144 @Modified
145 public void modified(ComponentContext context) {
146 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700147 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700148
Thomas Vachuska36002e62015-05-19 16:12:29 -0700149 if (poolSize != null && poolSize > 1) {
150 sharedThreadPoolSize = poolSize;
151 SharedExecutors.setPoolSize(sharedThreadPoolSize);
152 } else if (poolSize != null) {
153 log.warn("sharedThreadPoolSize must be greater than 1");
154 }
155
156 Integer timeLimit = getIntegerProperty(properties, "maxEventTimeLimit");
157 if (timeLimit != null && timeLimit > 1) {
158 maxEventTimeLimit = timeLimit;
159 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
160 } else if (timeLimit != null) {
161 log.warn("maxEventTimeLimit must be greater than 1");
162 }
163
164 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}",
165 sharedThreadPoolSize, maxEventTimeLimit);
166 }
Murat Parlakisik553db172015-04-08 03:29:04 -0700167
168
169 /**
170 * Get Integer property from the propertyName
171 * Return null if propertyName is not found.
172 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700173 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700174 * @param propertyName the name of the property to look up
175 * @return value when the propertyName is defined or return null
176 */
177 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
178 String propertyName) {
179 Integer value = null;
180 try {
181 String s = (String) properties.get(propertyName);
182 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
183 } catch (NumberFormatException | ClassCastException e) {
184 value = null;
185 }
186 return value;
187 }
188
189
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700190}