blob: 71fa9d5e3d09f5935995020a3ce8af6cc3eb6646 [file] [log] [blame]
Marc De Leenheer1afa2a02015-05-13 09:18:07 -07001/*
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 */
16package org.onosproject.net.resource.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
24import org.onosproject.net.Port;
25import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentId;
27import org.onosproject.net.intent.OpticalConnectivityIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070028import org.onosproject.net.resource.device.DeviceResourceService;
29import org.onosproject.net.resource.device.DeviceResourceStore;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070030import org.slf4j.Logger;
31
32import java.util.Arrays;
33import java.util.HashSet;
34import java.util.Set;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Provides basic implementation of device resources allocation.
41 */
42@Component(immediate = true)
43@Service
44public 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}