blob: 1eda70392302f25d1945b6a8f1471eadaf286d9c [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska96d55b12015-05-11 08:52:03 -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 */
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;
Simon Huntf59d36b2016-10-04 19:05:53 -070025import org.onosproject.net.region.RegionId;
Simon Huntd7395c82016-10-20 17:54:01 -070026import org.onosproject.ui.model.topo.UiTopoLayoutId;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070027
28import static com.google.common.base.Preconditions.checkArgument;
29
30/**
31 * Set of subject factories for potential configuration subjects.
32 */
33public final class SubjectFactories {
34
35 // Construction forbidden
36 private SubjectFactories() {
37 }
38
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070039 // Required for resolving application identifiers
40 private static CoreService coreService;
41
Simon Huntf59d36b2016-10-04 19:05:53 -070042 /**
43 * Application ID subject factory.
44 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070045 public static final SubjectFactory<ApplicationId> APP_SUBJECT_FACTORY =
46 new SubjectFactory<ApplicationId>(ApplicationId.class, "apps") {
47 @Override
48 public ApplicationId createSubject(String key) {
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070049 return coreService.registerApplication(key);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070050 }
Simon Huntf59d36b2016-10-04 19:05:53 -070051
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070052 @Override
53 public String subjectKey(ApplicationId subject) {
54 return subject.name();
55 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070056 };
57
Simon Huntf59d36b2016-10-04 19:05:53 -070058 /**
59 * Device ID subject factory.
60 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070061 public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
62 new SubjectFactory<DeviceId>(DeviceId.class, "devices") {
63 @Override
64 public DeviceId createSubject(String key) {
65 return DeviceId.deviceId(key);
66 }
67 };
68
Simon Huntf59d36b2016-10-04 19:05:53 -070069 /**
70 * Connect point subject factory.
71 */
Jonathan Hart111b42b2015-07-14 13:28:05 -070072 public static final SubjectFactory<ConnectPoint> CONNECT_POINT_SUBJECT_FACTORY =
73 new SubjectFactory<ConnectPoint>(ConnectPoint.class, "ports") {
74 @Override
75 public ConnectPoint createSubject(String key) {
76 return ConnectPoint.deviceConnectPoint(key);
77 }
Simon Huntf59d36b2016-10-04 19:05:53 -070078
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070079 @Override
80 public String subjectKey(ConnectPoint subject) {
81 return key(subject);
82 }
Jonathan Hart111b42b2015-07-14 13:28:05 -070083 };
84
Simon Huntf59d36b2016-10-04 19:05:53 -070085 /**
86 * Host ID subject factory.
87 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070088 public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
89 new SubjectFactory<HostId>(HostId.class, "hosts") {
90 @Override
91 public HostId createSubject(String key) {
92 return HostId.hostId(key);
93 }
94 };
95
Simon Huntf59d36b2016-10-04 19:05:53 -070096 /**
97 * Link key subject factory.
98 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070099 public static final SubjectFactory<LinkKey> LINK_SUBJECT_FACTORY =
100 new SubjectFactory<LinkKey>(LinkKey.class, "links") {
101 @Override
102 public LinkKey createSubject(String key) {
103 String[] cps = key.split("-");
104 checkArgument(cps.length == 2, "Incorrect link key format: %s", key);
105 return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
Simon Huntf59d36b2016-10-04 19:05:53 -0700106 ConnectPoint.deviceConnectPoint(cps[1]));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700107 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700108
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700109 @Override
110 public String subjectKey(LinkKey subject) {
111 return key(subject.src()) + "-" + key(subject.dst());
112 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700113 };
114
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -0700115 /**
Simon Huntf59d36b2016-10-04 19:05:53 -0700116 * Region ID subject factory.
117 */
118 public static final SubjectFactory<RegionId> REGION_SUBJECT_FACTORY =
119 new SubjectFactory<RegionId>(RegionId.class, "regions") {
120 @Override
121 public RegionId createSubject(String key) {
122 return RegionId.regionId(key);
123 }
124 };
125
126 /**
Simon Huntd7395c82016-10-20 17:54:01 -0700127 * UI Topology layout ID subject factory.
128 */
129 public static final SubjectFactory<UiTopoLayoutId> LAYOUT_SUBJECT_FACTORY =
130 new SubjectFactory<UiTopoLayoutId>(UiTopoLayoutId.class, "layouts") {
131 @Override
132 public UiTopoLayoutId createSubject(String key) {
133 return UiTopoLayoutId.layoutId(key);
134 }
135 };
136
137 /**
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -0700138 * Provides reference to the core service, which is required for
139 * application subject factory.
140 *
141 * @param service core service reference
142 */
143 public static void setCoreService(CoreService service) {
144 coreService = service;
145 }
146
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700147 private static String key(ConnectPoint subject) {
148 return subject.deviceId() + "/" + subject.port();
149 }
150
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700151}