blob: 419694bf77ce1ae376193375b4a7779e18454f42 [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;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080027import org.onosproject.app.ApplicationService;
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;
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -070041import java.io.IOException;
42import java.nio.file.Files;
43import java.nio.file.Path;
44import java.nio.file.Paths;
Murat Parlakisik553db172015-04-08 03:29:04 -070045import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070046import java.util.List;
47import java.util.Set;
48
49import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070050import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090051import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080052import static org.onosproject.security.AppPermission.Type.APP_READ;
Changhoon Yoonb856b812015-08-10 03:47:19 +090053
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'Connor4aded4c2015-12-11 16:55:13 -080066 private static Version version = Version.version("1.4.0-rc2");
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
83 private static final int DEFAULT_POOL_SIZE = 30;
84 @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070085 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070086 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 Parlakisik553db172015-04-08 03:29:04 -070092
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070093 @Activate
94 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070095 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070096 cfgService.registerProperties(getClass());
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -070097 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 Vachuskae0f804a2014-10-27 23:40:48 -0700106 }
107 }
108
Murat Parlakisik553db172015-04-08 03:29:04 -0700109 @Deactivate
110 public void deactivate() {
111 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700112 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700113 }
114
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700115 @Override
116 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900117 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700118 return version;
119 }
120
121 @Override
122 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900123 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700124 return applicationIdStore.getAppIds();
125 }
126
127 @Override
128 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900129 checkPermission(APP_READ);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700130 return applicationIdStore.getAppId(id);
131 }
132
133 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800134 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900135 checkPermission(APP_READ);
Ray Milkey02479862015-02-17 17:02:19 -0800136 return applicationIdStore.getAppId(name);
137 }
138
139
140 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700141 public ApplicationId registerApplication(String name) {
142 checkNotNull(name, "Application ID cannot be null");
143 return applicationIdStore.registerApplication(name);
144 }
145
Brian O'Connor520c0522014-11-23 23:50:47 -0800146 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800147 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'Connor520c0522014-11-23 23:50:47 -0800154 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800155 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
156 return new BlockAllocatorBasedIdGenerator(allocator);
157 }
158
Murat Parlakisik553db172015-04-08 03:29:04 -0700159
160 @Modified
161 public void modified(ComponentContext context) {
162 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700163 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700164
Thomas Vachuska36002e62015-05-19 16:12:29 -0700165 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 Parlakisik553db172015-04-08 03:29:04 -0700183
184
185 /**
186 * Get Integer property from the propertyName
187 * Return null if propertyName is not found.
188 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700189 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700190 * @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 Vachuskac65dd712015-11-04 17:19:10 -0800195 Integer value;
Murat Parlakisik553db172015-04-08 03:29:04 -0700196 try {
197 String s = (String) properties.get(propertyName);
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800198 value = isNullOrEmpty(s) ? null : Integer.parseInt(s.trim());
Murat Parlakisik553db172015-04-08 03:29:04 -0700199 } catch (NumberFormatException | ClassCastException e) {
200 value = null;
201 }
202 return value;
203 }
204
205
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700206}