blob: f4d560a4a670bacc755ef0c2ef521336887554f2 [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;
Murat Parlakisik553db172015-04-08 03:29:04 -070027import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.ApplicationIdStore;
30import org.onosproject.core.CoreService;
31import org.onosproject.core.IdBlockStore;
32import org.onosproject.core.IdGenerator;
33import org.onosproject.core.Version;
Thomas Vachuska36002e62015-05-19 16:12:29 -070034import org.onosproject.event.EventDeliveryService;
Murat Parlakisik553db172015-04-08 03:29:04 -070035import org.osgi.service.component.ComponentContext;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
Thomas Vachuska36002e62015-05-19 16:12:29 -070038
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070039import java.io.File;
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -070040import java.io.IOException;
41import java.nio.file.Files;
42import java.nio.file.Path;
43import java.nio.file.Paths;
Murat Parlakisik553db172015-04-08 03:29:04 -070044import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070045import java.util.List;
46import java.util.Set;
47
48import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070049import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090050import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090051import static org.onosproject.security.AppPermission.Type.*;
52
Changhoon Yoon541ef712015-05-23 17:18:34 +090053
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070054
55/**
56 * Core service implementation.
57 */
Brian O'Connor520c0522014-11-23 23:50:47 -080058@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070059@Service
60public class CoreManager implements CoreService {
61
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070062 private final Logger log = LoggerFactory.getLogger(getClass());
63
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070064 private static final File VERSION_FILE = new File("../VERSION");
Brian O'Connorf967ad62015-09-18 15:19:54 -070065 private static Version version = Version.version("1.4.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070066
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected ApplicationIdStore applicationIdStore;
69
Brian O'Connor520c0522014-11-23 23:50:47 -080070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected IdBlockStore idBlockStore;
72
Murat Parlakisik553db172015-04-08 03:29:04 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected ComponentConfigService cfgService;
75
Thomas Vachuska36002e62015-05-19 16:12:29 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected EventDeliveryService eventDeliveryService;
78
79 private static final int DEFAULT_POOL_SIZE = 30;
80 @Property(name = "sharedThreadPoolSize", intValue = DEFAULT_POOL_SIZE,
Murat Parlakisik553db172015-04-08 03:29:04 -070081 label = "Configure shared pool maximum size ")
Thomas Vachuska36002e62015-05-19 16:12:29 -070082 private int sharedThreadPoolSize = DEFAULT_POOL_SIZE;
83
84 private static final int DEFAULT_EVENT_TIME = 2000;
85 @Property(name = "maxEventTimeLimit", intValue = DEFAULT_EVENT_TIME,
86 label = "Maximum number of millis an event sink has to process an event")
87 private int maxEventTimeLimit = DEFAULT_EVENT_TIME;
Murat Parlakisik553db172015-04-08 03:29:04 -070088
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070089 @Activate
90 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070091 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070092 cfgService.registerProperties(getClass());
HIGUCHI Yuta3b3bd1e2015-09-22 16:39:33 -070093 try {
94 Path path = Paths.get(VERSION_FILE.getPath());
95 List<String> versionLines = Files.readAllLines(path);
96 if (versionLines != null && !versionLines.isEmpty()) {
97 version = Version.version(versionLines.get(0));
98 }
99 } catch (IOException e) {
100 // version file not found, using default
101 log.trace("Version file not found", e);
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700102 }
103 }
104
Murat Parlakisik553db172015-04-08 03:29:04 -0700105 @Deactivate
106 public void deactivate() {
107 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700108 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -0700109 }
110
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700111 @Override
112 public Version version() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900113 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900114
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700115 return version;
116 }
117
118 @Override
119 public Set<ApplicationId> getAppIds() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900120 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900121
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700122 return applicationIdStore.getAppIds();
123 }
124
125 @Override
126 public ApplicationId getAppId(Short id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900127 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900128
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700129 return applicationIdStore.getAppId(id);
130 }
131
132 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800133 public ApplicationId getAppId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900134 checkPermission(APP_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900135
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
147 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800148 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
149 return new BlockAllocatorBasedIdGenerator(allocator);
150 }
151
Murat Parlakisik553db172015-04-08 03:29:04 -0700152
153 @Modified
154 public void modified(ComponentContext context) {
155 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska36002e62015-05-19 16:12:29 -0700156 Integer poolSize = getIntegerProperty(properties, "sharedThreadPoolSize");
Murat Parlakisik553db172015-04-08 03:29:04 -0700157
Thomas Vachuska36002e62015-05-19 16:12:29 -0700158 if (poolSize != null && poolSize > 1) {
159 sharedThreadPoolSize = poolSize;
160 SharedExecutors.setPoolSize(sharedThreadPoolSize);
161 } else if (poolSize != null) {
162 log.warn("sharedThreadPoolSize must be greater than 1");
163 }
164
165 Integer timeLimit = getIntegerProperty(properties, "maxEventTimeLimit");
166 if (timeLimit != null && timeLimit > 1) {
167 maxEventTimeLimit = timeLimit;
168 eventDeliveryService.setDispatchTimeLimit(maxEventTimeLimit);
169 } else if (timeLimit != null) {
170 log.warn("maxEventTimeLimit must be greater than 1");
171 }
172
173 log.info("Settings: sharedThreadPoolSize={}, maxEventTimeLimit={}",
174 sharedThreadPoolSize, maxEventTimeLimit);
175 }
Murat Parlakisik553db172015-04-08 03:29:04 -0700176
177
178 /**
179 * Get Integer property from the propertyName
180 * Return null if propertyName is not found.
181 *
Thomas Vachuska36002e62015-05-19 16:12:29 -0700182 * @param properties properties to be looked up
Murat Parlakisik553db172015-04-08 03:29:04 -0700183 * @param propertyName the name of the property to look up
184 * @return value when the propertyName is defined or return null
185 */
186 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
187 String propertyName) {
188 Integer value = null;
189 try {
190 String s = (String) properties.get(propertyName);
191 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
192 } catch (NumberFormatException | ClassCastException e) {
193 value = null;
194 }
195 return value;
196 }
197
198
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700199}