blob: 104dfe58efd3dd60c627a1203ada224dc4746c6d [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070026
27import static com.google.common.base.Preconditions.checkArgument;
28
29/**
30 * Set of subject factories for potential configuration subjects.
31 */
32public final class SubjectFactories {
33
34 // Construction forbidden
35 private SubjectFactories() {
36 }
37
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070038 // Required for resolving application identifiers
39 private static CoreService coreService;
40
Simon Huntf59d36b2016-10-04 19:05:53 -070041 /**
42 * Application ID subject factory.
43 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070044 public static final SubjectFactory<ApplicationId> APP_SUBJECT_FACTORY =
45 new SubjectFactory<ApplicationId>(ApplicationId.class, "apps") {
46 @Override
47 public ApplicationId createSubject(String key) {
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -070048 return coreService.registerApplication(key);
Thomas Vachuska96d55b12015-05-11 08:52:03 -070049 }
Simon Huntf59d36b2016-10-04 19:05:53 -070050
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070051 @Override
52 public String subjectKey(ApplicationId subject) {
53 return subject.name();
54 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -070055 };
56
Simon Huntf59d36b2016-10-04 19:05:53 -070057 /**
58 * Device ID subject factory.
59 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070060 public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
61 new SubjectFactory<DeviceId>(DeviceId.class, "devices") {
62 @Override
63 public DeviceId createSubject(String key) {
64 return DeviceId.deviceId(key);
65 }
66 };
67
Simon Huntf59d36b2016-10-04 19:05:53 -070068 /**
69 * Connect point subject factory.
70 */
Jonathan Hart111b42b2015-07-14 13:28:05 -070071 public static final SubjectFactory<ConnectPoint> CONNECT_POINT_SUBJECT_FACTORY =
72 new SubjectFactory<ConnectPoint>(ConnectPoint.class, "ports") {
73 @Override
74 public ConnectPoint createSubject(String key) {
75 return ConnectPoint.deviceConnectPoint(key);
76 }
Simon Huntf59d36b2016-10-04 19:05:53 -070077
Thomas Vachuskaea5adc62015-10-07 11:52:30 -070078 @Override
79 public String subjectKey(ConnectPoint subject) {
80 return key(subject);
81 }
Jonathan Hart111b42b2015-07-14 13:28:05 -070082 };
83
Simon Huntf59d36b2016-10-04 19:05:53 -070084 /**
85 * Host ID subject factory.
86 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070087 public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
88 new SubjectFactory<HostId>(HostId.class, "hosts") {
89 @Override
90 public HostId createSubject(String key) {
91 return HostId.hostId(key);
92 }
93 };
94
Simon Huntf59d36b2016-10-04 19:05:53 -070095 /**
96 * Link key subject factory.
97 */
Thomas Vachuska96d55b12015-05-11 08:52:03 -070098 public static final SubjectFactory<LinkKey> LINK_SUBJECT_FACTORY =
99 new SubjectFactory<LinkKey>(LinkKey.class, "links") {
100 @Override
101 public LinkKey createSubject(String key) {
102 String[] cps = key.split("-");
103 checkArgument(cps.length == 2, "Incorrect link key format: %s", key);
104 return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
Simon Huntf59d36b2016-10-04 19:05:53 -0700105 ConnectPoint.deviceConnectPoint(cps[1]));
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700106 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700107
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700108 @Override
109 public String subjectKey(LinkKey subject) {
110 return key(subject.src()) + "-" + key(subject.dst());
111 }
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700112 };
113
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -0700114 /**
Simon Huntf59d36b2016-10-04 19:05:53 -0700115 * Region ID subject factory.
116 */
117 public static final SubjectFactory<RegionId> REGION_SUBJECT_FACTORY =
118 new SubjectFactory<RegionId>(RegionId.class, "regions") {
119 @Override
120 public RegionId createSubject(String key) {
121 return RegionId.regionId(key);
122 }
123 };
124
125 /**
Thomas Vachuska9fadbfc2015-07-23 10:56:02 -0700126 * Provides reference to the core service, which is required for
127 * application subject factory.
128 *
129 * @param service core service reference
130 */
131 public static void setCoreService(CoreService service) {
132 coreService = service;
133 }
134
Thomas Vachuskaea5adc62015-10-07 11:52:30 -0700135 private static String key(ConnectPoint subject) {
136 return subject.deviceId() + "/" + subject.port();
137 }
138
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700139}