blob: 5c78bb4a2f0989d6ff93040652241b0fff76144d [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;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070047
48/**
49 * Core service implementation.
50 */
Brian O'Connor520c0522014-11-23 23:50:47 -080051@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070052@Service
53public class CoreManager implements CoreService {
54
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070055 private final Logger log = LoggerFactory.getLogger(getClass());
56
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070057 private static final File VERSION_FILE = new File("../VERSION");
Simon Hunt9fec43f2015-03-25 11:36:30 -070058 private static Version version = Version.version("1.2.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070059
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected ApplicationIdStore applicationIdStore;
62
Brian O'Connor520c0522014-11-23 23:50:47 -080063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected IdBlockStore idBlockStore;
65
Murat Parlakisik553db172015-04-08 03:29:04 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ComponentConfigService cfgService;
68
Thomas Vachuska36002e62015-05-19 16:12:29 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected EventDeliveryService eventDeliveryService;
71
72 private static final int DEFAULT_POOL_SIZE = 30;
73 @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070074 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070075 private int sharedThreadPoolSize = DEFAULT_POOL_SIZE;
76
77 private static final int DEFAULT_EVENT_TIME = 2000;
78 @Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME,
79 label = "Maximum number of millis an event sink has to process an event")
80 private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
Murat Parlakisik553db172015-04-08 03:29:04 -070081
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070082 @Activate
83 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070084 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070085 cfgService.registerProperties(getClass());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070086 List<String> versionLines = Tools.slurp(VERSION_FILE);
87 if (versionLines != null && !versionLines.isEmpty()) {
88 version = Version.version(versionLines.get(0));
89 }
90 }
91
Murat Parlakisik553db172015-04-08 03:29:04 -070092 @Deactivate
93 public void deactivate() {
94 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -070095 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -070096 }
97
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070098 @Override
99 public Version version() {
100 return version;
101 }
102
103 @Override
104 public Set<ApplicationId> getAppIds() {
105 return applicationIdStore.getAppIds();
106 }
107
108 @Override
109 public ApplicationId getAppId(Short id) {
110 return applicationIdStore.getAppId(id);
111 }
112
113 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800114 public ApplicationId getAppId(String name) {
115 return applicationIdStore.getAppId(name);
116 }
117
118
119 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700120 public ApplicationId registerApplication(String name) {
121 checkNotNull(name, "Application ID cannot be null");
122 return applicationIdStore.registerApplication(name);
123 }
124
Brian O'Connor520c0522014-11-23 23:50:47 -0800125 @Override
126 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800127 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
128 return new BlockAllocatorBasedIdGenerator(allocator);
129 }
130
Murat Parlakisik553db172015-04-08 03:29:04 -0700131
132 @Modified
133 public void modified(ComponentContext context) {
134 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700135 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700136
Thomas Vachuska36002e62015-05-19 16:12:29 -0700137 if (poolSize != null && poolSize > 1) {
138 sharedThreadPoolSize = poolSize;
139 SharedExecutors.setPoolSize(sharedThreadPoolSize);
140 } else if (poolSize != null) {
141 log.warn("sharedThreadPoolSize must be greater than 1");
142 }
143
144 Integer timeLimit = getIntegerProperty(properties, "maxEventTimeLimit");
145 if (timeLimit != null && timeLimit > 1) {
146 maxEventTimeLimit = timeLimit;
147 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
148 } else if (timeLimit != null) {
149 log.warn("maxEventTimeLimit must be greater than 1");
150 }
151
152 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}",
153 sharedThreadPoolSize, maxEventTimeLimit);
154 }
Murat Parlakisik553db172015-04-08 03:29:04 -0700155
156
157 /**
158 * Get Integer property from the propertyName
159 * Return null if propertyName is not found.
160 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700161 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700162 * @param propertyName the name of the property to look up
163 * @return value when the propertyName is defined or return null
164 */
165 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
166 String propertyName) {
167 Integer value = null;
168 try {
169 String s = (String) properties.get(propertyName);
170 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
171 } catch (NumberFormatException | ClassCastException e) {
172 value = null;
173 }
174 return value;
175 }
176
177
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700178}