Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | 9fadbfc | 2015-07-23 10:56:02 -0700 | [diff] [blame] | 19 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.HostId; |
| 23 | import org.onosproject.net.LinkKey; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.SubjectFactory; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 25 | import org.onosproject.net.region.RegionId; |
Simon Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | |
| 30 | /** |
| 31 | * Set of subject factories for potential configuration subjects. |
| 32 | */ |
| 33 | public final class SubjectFactories { |
| 34 | |
| 35 | // Construction forbidden |
| 36 | private SubjectFactories() { |
| 37 | } |
| 38 | |
Thomas Vachuska | 9fadbfc | 2015-07-23 10:56:02 -0700 | [diff] [blame] | 39 | // Required for resolving application identifiers |
| 40 | private static CoreService coreService; |
| 41 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Application ID subject factory. |
| 44 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 45 | public static final SubjectFactory<ApplicationId> APP_SUBJECT_FACTORY = |
| 46 | new SubjectFactory<ApplicationId>(ApplicationId.class, "apps") { |
| 47 | @Override |
| 48 | public ApplicationId createSubject(String key) { |
Thomas Vachuska | 9fadbfc | 2015-07-23 10:56:02 -0700 | [diff] [blame] | 49 | return coreService.registerApplication(key); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 50 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 51 | |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 52 | @Override |
| 53 | public String subjectKey(ApplicationId subject) { |
| 54 | return subject.name(); |
| 55 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Device ID subject factory. |
| 60 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 61 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Connect point subject factory. |
| 71 | */ |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 72 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 78 | |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public String subjectKey(ConnectPoint subject) { |
| 81 | return key(subject); |
| 82 | } |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Host ID subject factory. |
| 87 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 88 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Link key subject factory. |
| 98 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 99 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 106 | ConnectPoint.deviceConnectPoint(cps[1])); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 107 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 108 | |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public String subjectKey(LinkKey subject) { |
| 111 | return key(subject.src()) + "-" + key(subject.dst()); |
| 112 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Thomas Vachuska | 9fadbfc | 2015-07-23 10:56:02 -0700 | [diff] [blame] | 115 | /** |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 116 | * 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 Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 127 | * 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 Vachuska | 9fadbfc | 2015-07-23 10:56:02 -0700 | [diff] [blame] | 138 | * Provides reference to the core service, which is required for |
| 139 | * application subject factory. |
| 140 | * |
| 141 | * @param service core service reference |
| 142 | */ |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 143 | public static synchronized void setCoreService(CoreService service) { |
Thomas Vachuska | 9fadbfc | 2015-07-23 10:56:02 -0700 | [diff] [blame] | 144 | coreService = service; |
| 145 | } |
| 146 | |
Thomas Vachuska | ea5adc6 | 2015-10-07 11:52:30 -0700 | [diff] [blame] | 147 | private static String key(ConnectPoint subject) { |
| 148 | return subject.deviceId() + "/" + subject.port(); |
| 149 | } |
| 150 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 151 | } |