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