xuzhang
Committed by Gerrit Code Review

[ONOS-2248]The implementation of port resource service.

Change-Id: Ifaba79cad89a038bea018d886419398fcc3e7fb3
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.app.vtnrsc.virtualport.impl;
17 +
18 +import java.util.Collection;
19 +import java.util.Collections;
20 +
21 +import org.apache.felix.scr.annotations.Activate;
22 +import org.apache.felix.scr.annotations.Component;
23 +import org.apache.felix.scr.annotations.Deactivate;
24 +import org.apache.felix.scr.annotations.Reference;
25 +import org.apache.felix.scr.annotations.ReferenceCardinality;
26 +import org.apache.felix.scr.annotations.Service;
27 +import org.onlab.util.KryoNamespace;
28 +import org.onosproject.app.vtnrsc.TenantId;
29 +import org.onosproject.app.vtnrsc.TenantNetworkId;
30 +import org.onosproject.app.vtnrsc.VirtualPort;
31 +import org.onosproject.app.vtnrsc.VirtualPortId;
32 +import org.onosproject.app.vtnrsc.tenantnetwork.TenantNetworkService;
33 +import org.onosproject.app.vtnrsc.virtualport.VirtualPortService;
34 +import org.onosproject.net.DeviceId;
35 +import org.onosproject.store.service.EventuallyConsistentMap;
36 +import org.onosproject.store.service.MultiValuedTimestamp;
37 +import org.onosproject.store.service.StorageService;
38 +import org.onosproject.store.service.WallClockTimestamp;
39 +import org.slf4j.Logger;
40 +import org.slf4j.LoggerFactory;
41 +
42 +/**
43 + * Provides implementation of the VirtualPort APIs.
44 + */
45 +@Component(immediate = true)
46 +@Service
47 +public class VirtualPortManager implements VirtualPortService {
48 + private final Logger log = LoggerFactory.getLogger(getClass());
49 +
50 + private EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore;
51 +
52 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 + protected StorageService storageService;
54 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 + protected TenantNetworkService networkService;
56 +
57 + @Activate
58 + public void activate() {
59 + KryoNamespace.Builder seriallizer = KryoNamespace.newBuilder()
60 + .register(MultiValuedTimestamp.class);
61 + vPortStore = storageService
62 + .<VirtualPortId, VirtualPort>eventuallyConsistentMapBuilder()
63 + .withName("vPortId_vPort").withSerializer(seriallizer)
64 + .withTimestampProvider((k, v) -> new WallClockTimestamp())
65 + .build();
66 + log.info("Started");
67 + }
68 +
69 + @Deactivate
70 + public void deactivate() {
71 + vPortStore.destroy();
72 + log.info("Stoppped");
73 + }
74 +
75 + @Override
76 + public boolean exists(VirtualPortId vPortId) {
77 + return vPortStore.containsKey(vPortId);
78 + }
79 +
80 + @Override
81 + public VirtualPort getPort(VirtualPortId vPortId) {
82 + if (!exists(vPortId)) {
83 + return null;
84 + }
85 + return vPortStore.get(vPortId);
86 + }
87 +
88 + @Override
89 + public Collection<VirtualPort> getPorts() {
90 + return Collections.unmodifiableCollection(vPortStore.values());
91 + }
92 +
93 + @Override
94 + public Collection<VirtualPort> getPorts(TenantNetworkId networkId) {
95 + Collection<VirtualPort> vPortWithNetworkId =
96 + Collections.unmodifiableCollection(vPortStore.values());
97 + if (networkId == null || !networkService.exists(networkId)) {
98 + return null;
99 + }
100 + for (VirtualPort vPort : vPortWithNetworkId) {
101 + if (!vPort.networkId().equals(networkId)) {
102 + vPortWithNetworkId.remove(vPort);
103 + }
104 + }
105 + return vPortWithNetworkId;
106 + }
107 +
108 + @Override
109 + public Collection<VirtualPort> getPorts(TenantId tenantId) {
110 + Collection<VirtualPort> vPortWithTenantId =
111 + Collections.unmodifiableCollection(vPortStore.values());
112 + if (tenantId == null) {
113 + return null;
114 + }
115 + for (VirtualPort vPort : vPortWithTenantId) {
116 + if (!vPort.tenantId().equals(tenantId)) {
117 + vPortWithTenantId.remove(vPort);
118 + }
119 + }
120 + return vPortWithTenantId;
121 + }
122 +
123 + @Override
124 + public Collection<VirtualPort> getPorts(DeviceId deviceId) {
125 + Collection<VirtualPort> vPortWithDeviceId =
126 + Collections.unmodifiableCollection(vPortStore.values());
127 + if (deviceId == null) {
128 + return null;
129 + }
130 + for (VirtualPort vPort : vPortWithDeviceId) {
131 + if (!vPort.deviceId().equals(deviceId)) {
132 + vPortWithDeviceId.remove(vPort);
133 + }
134 + }
135 + return vPortWithDeviceId;
136 + }
137 +
138 + @Override
139 + public boolean createPorts(Iterable<VirtualPort> vPorts) {
140 + for (VirtualPort vPort:vPorts) {
141 + log.info("vPortId is {} ", vPort.portId().toString());
142 + vPortStore.put(vPort.portId(), vPort);
143 + }
144 + return true;
145 + }
146 +
147 + @Override
148 + public boolean updatePorts(Iterable<VirtualPort> vPorts) {
149 + Boolean flag = false;
150 + if (vPorts != null) {
151 + for (VirtualPort vPort:vPorts) {
152 + vPortStore.put(vPort.portId(), vPort);
153 + flag = true;
154 + }
155 + }
156 + return flag;
157 + }
158 +
159 + @Override
160 + public boolean removePorts(Iterable<VirtualPortId> vPortIds) {
161 + Boolean flag = false;
162 + if (vPortIds != null) {
163 + for (VirtualPortId vPortId:vPortIds) {
164 + vPortStore.remove(vPortId);
165 + flag = true;
166 + log.info("The result of removing vPortId is {}", flag.toString());
167 + }
168 + }
169 + return flag;
170 + }
171 +
172 +}