62b4112b6c16ceea474e78e786a92277ec9a8f9a
[onosfw.git] /
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.resource.device.DeviceResourceService;
28 import org.onosproject.net.resource.device.DeviceResourceStore;
29 import org.slf4j.Logger;
30
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
59     public boolean requestPorts(Set<Port> ports, Intent intent) {
60         checkNotNull(intent);
61
62         return store.allocatePorts(ports, intent.id());
63     }
64
65     @Override
66     public Set<Port> getAllocations(IntentId intentId) {
67         return store.getAllocations(intentId);
68     }
69
70     @Override
71     public IntentId getAllocations(Port port) {
72         return store.getAllocations(port);
73     }
74
75     @Override
76     public void releaseMapping(IntentId intentId) {
77         store.releaseMapping(intentId);
78     }
79
80     @Override
81     public boolean requestMapping(IntentId keyIntentId, IntentId valIntentId) {
82         return store.allocateMapping(keyIntentId, valIntentId);
83     }
84
85     @Override
86     public Set<IntentId> getMapping(IntentId intentId) {
87         return store.getMapping(intentId);
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 }