blob: da1f8c9dabbeda5f049a8d2d39b3542839fdb798 [file] [log] [blame]
alshabib92c65ad2014-10-08 21:56:05 -07001package org.onlab.onos.impl;
2
alshabib92c65ad2014-10-08 21:56:05 -07003import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Service;
6import org.onlab.onos.ApplicationId;
7import org.onlab.onos.CoreService;
8import org.onlab.onos.Version;
9import org.onlab.util.Tools;
10
toma6897792014-10-08 22:21:05 -070011import java.io.File;
12import java.util.List;
13import java.util.Map;
14import java.util.concurrent.ConcurrentHashMap;
15import java.util.concurrent.atomic.AtomicInteger;
alshabib92c65ad2014-10-08 21:56:05 -070016
17/**
18 * Core service implementation.
19 */
20@Component
21@Service
22public class CoreManager implements CoreService {
23
24 private static final AtomicInteger ID_DISPENSER = new AtomicInteger(1);
toma6897792014-10-08 22:21:05 -070025
alshabib92c65ad2014-10-08 21:56:05 -070026 private static final File VERSION_FILE = new File("../VERSION");
27 private static Version version = Version.version("1.0.0-SNAPSHOT");
28
toma6897792014-10-08 22:21:05 -070029 private final Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070030 private final Map<String, DefaultApplicationId> appIdsByName = new ConcurrentHashMap<>();
alshabib92c65ad2014-10-08 21:56:05 -070031
32 // TODO: work in progress
33
34 @Activate
35 public void activate() {
36 List<String> versionLines = Tools.slurp(VERSION_FILE);
37 if (versionLines != null && !versionLines.isEmpty()) {
38 version = Version.version(versionLines.get(0));
39 }
40 }
41
42 @Override
43 public Version version() {
44 return version;
45 }
46
47 @Override
48 public ApplicationId getAppId(Short id) {
toma6897792014-10-08 22:21:05 -070049 return appIds.get(id);
alshabib92c65ad2014-10-08 21:56:05 -070050 }
51
52 @Override
53 public ApplicationId registerApplication(String name) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070054 DefaultApplicationId appId = appIdsByName.get(name);
55 if (appId == null) {
56 short id = (short) ID_DISPENSER.getAndIncrement();
57 appId = new DefaultApplicationId(id, name);
58 appIds.put(id, appId);
59 appIdsByName.put(name, appId);
60 }
toma6897792014-10-08 22:21:05 -070061 return appId;
alshabib92c65ad2014-10-08 21:56:05 -070062 }
63
64}