blob: a91a3526fb480e6198f0da0cd72e2896cf567ad4 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 */
16package org.onosproject.incubator.net.config.basics;
17
18import org.onosproject.core.ApplicationId;
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070019import org.onosproject.core.CoreService;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070020import org.onosproject.incubator.net.config.SubjectFactory;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.HostId;
24import org.onosproject.net.LinkKey;
25
26import static com.google.common.base.Preconditions.checkArgument;
27
28/**
29 * Set of subject factories for potential configuration subjects.
30 */
31public final class SubjectFactories {
32
33 // Construction forbidden
34 private SubjectFactories() {
35 }
36
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070037 // Required for resolving application identifiers
38 private static CoreService coreService;
39
Thomas Vachuska96d55b12015-05-11 08:52:03 -070040 public static final SubjectFactory<ApplicationId> APP_SUBJECT_FACTORY =
41 new SubjectFactory<ApplicationId>(ApplicationId.class, "apps") {
42 @Override
43 public ApplicationId createSubject(String key) {
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070044 return coreService.registerApplication(key);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070045 }
46 };
47
48 public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
49 new SubjectFactory<DeviceId>(DeviceId.class, "devices") {
50 @Override
51 public DeviceId createSubject(String key) {
52 return DeviceId.deviceId(key);
53 }
54 };
55
Jonathan Hart111b42b2015-07-14 13:28:05 -070056 public static final SubjectFactory<ConnectPoint> CONNECT_POINT_SUBJECT_FACTORY =
57 new SubjectFactory<ConnectPoint>(ConnectPoint.class, "ports") {
58 @Override
59 public ConnectPoint createSubject(String key) {
60 return ConnectPoint.deviceConnectPoint(key);
61 }
62 };
63
Thomas Vachuska96d55b12015-05-11 08:52:03 -070064 public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
65 new SubjectFactory<HostId>(HostId.class, "hosts") {
66 @Override
67 public HostId createSubject(String key) {
68 return HostId.hostId(key);
69 }
70 };
71
72 public static final SubjectFactory<LinkKey> LINK_SUBJECT_FACTORY =
73 new SubjectFactory<LinkKey>(LinkKey.class, "links") {
74 @Override
75 public LinkKey createSubject(String key) {
76 String[] cps = key.split("-");
77 checkArgument(cps.length == 2, "Incorrect link key format: %s", key);
78 return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
79 ConnectPoint.deviceConnectPoint(cps[1]));
80 }
81 };
82
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070083 /**
84 * Provides reference to the core service, which is required for
85 * application subject factory.
86 *
87 * @param service core service reference
88 */
89 public static void setCoreService(CoreService service) {
90 coreService = service;
91 }
92
Thomas Vachuska96d55b12015-05-11 08:52:03 -070093}