alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 1 | package org.onlab.onos.impl; |
| 2 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Service; |
| 6 | import org.onlab.onos.ApplicationId; |
| 7 | import org.onlab.onos.CoreService; |
| 8 | import org.onlab.onos.Version; |
| 9 | import org.onlab.util.Tools; |
| 10 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 11 | import java.io.File; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
| 14 | import java.util.concurrent.ConcurrentHashMap; |
| 15 | import java.util.concurrent.atomic.AtomicInteger; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Core service implementation. |
| 19 | */ |
| 20 | @Component |
| 21 | @Service |
| 22 | public class CoreManager implements CoreService { |
| 23 | |
| 24 | private static final AtomicInteger ID_DISPENSER = new AtomicInteger(1); |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 25 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 26 | private static final File VERSION_FILE = new File("../VERSION"); |
| 27 | private static Version version = Version.version("1.0.0-SNAPSHOT"); |
| 28 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 29 | private final Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 30 | private final Map<String, DefaultApplicationId> appIdsByName = new ConcurrentHashMap<>(); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 31 | |
| 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) { |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 49 | return appIds.get(id); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public ApplicationId registerApplication(String name) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 54 | 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 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 61 | return appId; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } |