blob: 311566b328dd10c8c74322313525075719fc768d [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 */
Thomas Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
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.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.HostId;
23import org.onosproject.net.LinkKey;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070024import org.onosproject.net.config.SubjectFactory;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070025
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 }
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070046 @Override
47 public String subjectKey(ApplicationId subject) {
48 return subject.name();
49 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070050 };
51
52 public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
53 new SubjectFactory<DeviceId>(DeviceId.class, "devices") {
54 @Override
55 public DeviceId createSubject(String key) {
56 return DeviceId.deviceId(key);
57 }
58 };
59
Jonathan Hart111b42b2015-07-14 13:28:05 -070060 public static final SubjectFactory<ConnectPoint> CONNECT_POINT_SUBJECT_FACTORY =
61 new SubjectFactory<ConnectPoint>(ConnectPoint.class, "ports") {
62 @Override
63 public ConnectPoint createSubject(String key) {
64 return ConnectPoint.deviceConnectPoint(key);
65 }
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070066 @Override
67 public String subjectKey(ConnectPoint subject) {
68 return key(subject);
69 }
Jonathan Hart111b42b2015-07-14 13:28:05 -070070 };
71
Thomas Vachuska96d55b12015-05-11 08:52:03 -070072 public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
73 new SubjectFactory<HostId>(HostId.class, "hosts") {
74 @Override
75 public HostId createSubject(String key) {
76 return HostId.hostId(key);
77 }
78 };
79
80 public static final SubjectFactory<LinkKey> LINK_SUBJECT_FACTORY =
81 new SubjectFactory<LinkKey>(LinkKey.class, "links") {
82 @Override
83 public LinkKey createSubject(String key) {
84 String[] cps = key.split("-");
85 checkArgument(cps.length == 2, "Incorrect link key format: %s", key);
86 return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
87 ConnectPoint.deviceConnectPoint(cps[1]));
88 }
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070089 @Override
90 public String subjectKey(LinkKey subject) {
91 return key(subject.src()) + "-" + key(subject.dst());
92 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070093 };
94
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070095 /**
96 * Provides reference to the core service, which is required for
97 * application subject factory.
98 *
99 * @param service core service reference
100 */
101 public static void setCoreService(CoreService service) {
102 coreService = service;
103 }
104
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700105 private static String key(ConnectPoint subject) {
106 return subject.deviceId() + "/" + subject.port();
107 }
108
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700109}