blob: 8e0de97f4bfe8f394ec2ebfefd4c3d45ad2e98eb [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;
20import org.apache.felix.scr.annotations.Reference;
21import org.apache.felix.scr.annotations.ReferenceCardinality;
22import org.apache.felix.scr.annotations.Service;
Murat Parlakisik553db172015-04-08 03:29:04 -070023import org.apache.felix.scr.annotations.Property;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Modified;
26import 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;
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 Vachuskae0f804a2014-10-27 23:40:48 -070038import java.io.File;
Murat Parlakisik553db172015-04-08 03:29:04 -070039import java.util.Dictionary;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070040import java.util.List;
41import java.util.Set;
42
43import static com.google.common.base.Preconditions.checkNotNull;
Murat Parlakisik553db172015-04-08 03:29:04 -070044import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070045
46/**
47 * Core service implementation.
48 */
Brian O'Connor520c0522014-11-23 23:50:47 -080049@Component(immediate = true)
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070050@Service
51public class CoreManager implements CoreService {
52
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070053 private final Logger log = LoggerFactory.getLogger(getClass());
54
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070055 private static final File VERSION_FILE = new File("../VERSION");
Simon Hunt9fec43f2015-03-25 11:36:30 -070056 private static Version version = Version.version("1.2.0-SNAPSHOT");
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070057
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ApplicationIdStore applicationIdStore;
60
Brian O'Connor520c0522014-11-23 23:50:47 -080061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected IdBlockStore idBlockStore;
63
Murat Parlakisik553db172015-04-08 03:29:04 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ComponentConfigService cfgService;
66
Jon Halle3f0fcf2015-04-10 09:34:23 -070067 @Property(name = "sharedThreadPoolSize", intValue = 30,
Murat Parlakisik553db172015-04-08 03:29:04 -070068 label = "Configure shared pool maximum size ")
Jon Halle3f0fcf2015-04-10 09:34:23 -070069 private int sharedThreadPoolSize = 30;
Murat Parlakisik553db172015-04-08 03:29:04 -070070
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070071 @Activate
72 public void activate() {
Thomas Vachuska6cba4952015-04-22 12:38:22 -070073 registerApplication(CORE_APP_NAME);
Murat Parlakisik553db172015-04-08 03:29:04 -070074 cfgService.registerProperties(getClass());
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070075 List<String> versionLines = Tools.slurp(VERSION_FILE);
76 if (versionLines != null && !versionLines.isEmpty()) {
77 version = Version.version(versionLines.get(0));
78 }
79 }
80
Murat Parlakisik553db172015-04-08 03:29:04 -070081 @Deactivate
82 public void deactivate() {
83 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuskab0317c62015-04-08 23:58:58 -070084 SharedExecutors.shutdown();
Murat Parlakisik553db172015-04-08 03:29:04 -070085 }
86
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070087 @Override
88 public Version version() {
89 return version;
90 }
91
92 @Override
93 public Set<ApplicationId> getAppIds() {
94 return applicationIdStore.getAppIds();
95 }
96
97 @Override
98 public ApplicationId getAppId(Short id) {
99 return applicationIdStore.getAppId(id);
100 }
101
102 @Override
Ray Milkey02479862015-02-17 17:02:19 -0800103 public ApplicationId getAppId(String name) {
104 return applicationIdStore.getAppId(name);
105 }
106
107
108 @Override
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700109 public ApplicationId registerApplication(String name) {
110 checkNotNull(name, "Application ID cannot be null");
111 return applicationIdStore.registerApplication(name);
112 }
113
Brian O'Connor520c0522014-11-23 23:50:47 -0800114 @Override
115 public IdGenerator getIdGenerator(String topic) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800116 IdBlockAllocator allocator = new StoreBasedIdBlockAllocator(topic, idBlockStore);
117 return new BlockAllocatorBasedIdGenerator(allocator);
118 }
119
Murat Parlakisik553db172015-04-08 03:29:04 -0700120
121 @Modified
122 public void modified(ComponentContext context) {
123 Dictionary<?, ?> properties = context.getProperties();
124 Integer sharedThreadPoolSizeConfig =
125 getIntegerProperty(properties, "sharedThreadPoolSize");
126 if (sharedThreadPoolSizeConfig == null) {
127 log.info("Shared Pool Size is not configured, default value is {}",
128 sharedThreadPoolSize);
129 } else {
130 if (sharedThreadPoolSizeConfig > 0) {
131 sharedThreadPoolSize = sharedThreadPoolSizeConfig;
132 SharedExecutors.setPoolSize(sharedThreadPoolSize);
133 log.info("Configured. Shared Pool Size is configured to {}",
134 sharedThreadPoolSize);
135 } else {
136 log.warn("Shared Pool Size size must be greater than 0");
137 }
138 }
139 }
140
141
142
143 /**
144 * Get Integer property from the propertyName
145 * Return null if propertyName is not found.
146 *
147 * @param properties properties to be looked up
148 * @param propertyName the name of the property to look up
149 * @return value when the propertyName is defined or return null
150 */
151 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
152 String propertyName) {
153 Integer value = null;
154 try {
155 String s = (String) properties.get(propertyName);
156 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
157 } catch (NumberFormatException | ClassCastException e) {
158 value = null;
159 }
160 return value;
161 }
162
163
Thomas Vachuskae0f804a2014-10-27 23:40:48 -0700164}