Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.resource.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.intent.Intent; |
| 26 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 27 | import org.onosproject.net.resource.device.DeviceResourceService; |
| 28 | import org.onosproject.net.resource.device.DeviceResourceStore; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Provides basic implementation of device resources allocation. |
| 38 | */ |
| 39 | @Component(immediate = true) |
| 40 | @Service |
| 41 | public class DeviceResourceManager implements DeviceResourceService { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | private DeviceResourceStore store; |
| 47 | |
| 48 | @Activate |
| 49 | public void activate() { |
| 50 | log.info("Started"); |
| 51 | } |
| 52 | |
| 53 | @Deactivate |
| 54 | public void deactivate() { |
| 55 | log.info("Stopped"); |
| 56 | } |
| 57 | |
| 58 | @Override |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 59 | public boolean requestPorts(Set<Port> ports, Intent intent) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 60 | checkNotNull(intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 61 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 62 | return store.allocatePorts(ports, intent.id()); |
| 63 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 64 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 65 | @Override |
| 66 | public Set<Port> getAllocations(IntentId intentId) { |
| 67 | return store.getAllocations(intentId); |
| 68 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 69 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 70 | @Override |
| 71 | public IntentId getAllocations(Port port) { |
| 72 | return store.getAllocations(port); |
| 73 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 74 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 75 | @Override |
| 76 | public void releaseMapping(IntentId keyIntentId, IntentId valIntentId) { |
| 77 | store.releaseMapping(keyIntentId, valIntentId); |
| 78 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 79 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 80 | @Override |
| 81 | public boolean requestMapping(IntentId keyIntentId, IntentId valIntentId) { |
| 82 | return store.allocateMapping(keyIntentId, valIntentId); |
| 83 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 84 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 85 | @Override |
| 86 | public Set<IntentId> getMapping(IntentId intentId) { |
| 87 | return store.getMapping(intentId); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void releasePorts(IntentId intentId) { |
| 92 | store.releasePorts(intentId); |
| 93 | } |
| 94 | |
| 95 | private Port getTypedPort(Set<Port> ports, Port.Type type) { |
| 96 | for (Port port : ports) { |
| 97 | if (port.type() == type) { |
| 98 | return port; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return null; |
| 103 | } |
| 104 | } |