2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnrsc.virtualport.impl;
18 import static com.google.common.base.Preconditions.checkNotNull;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
27 import java.util.stream.Collectors;
29 import org.apache.felix.scr.annotations.Activate;
30 import org.apache.felix.scr.annotations.Component;
31 import org.apache.felix.scr.annotations.Deactivate;
32 import org.apache.felix.scr.annotations.Reference;
33 import org.apache.felix.scr.annotations.ReferenceCardinality;
34 import org.apache.felix.scr.annotations.Service;
35 import org.onlab.packet.IpAddress;
36 import org.onosproject.core.ApplicationId;
37 import org.onosproject.core.CoreService;
38 import org.onosproject.net.DeviceId;
39 import org.onosproject.store.serializers.KryoNamespaces;
40 import org.onosproject.store.service.Serializer;
41 import org.onosproject.store.service.StorageService;
42 import org.onosproject.vtnrsc.AllowedAddressPair;
43 import org.onosproject.vtnrsc.BindingHostId;
44 import org.onosproject.vtnrsc.DefaultVirtualPort;
45 import org.onosproject.vtnrsc.FixedIp;
46 import org.onosproject.vtnrsc.SecurityGroup;
47 import org.onosproject.vtnrsc.SubnetId;
48 import org.onosproject.vtnrsc.TenantId;
49 import org.onosproject.vtnrsc.TenantNetworkId;
50 import org.onosproject.vtnrsc.VirtualPort;
51 import org.onosproject.vtnrsc.VirtualPortId;
52 import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
53 import org.onosproject.vtnrsc.virtualport.VirtualPortService;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
58 * Provides implementation of the VirtualPort APIs.
60 @Component(immediate = true)
62 public class VirtualPortManager implements VirtualPortService {
64 private final Logger log = LoggerFactory.getLogger(getClass());
66 private static final String VIRTUALPORT = "vtn-virtual-port";
67 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
69 private static final String VIRTUALPORT_ID_NULL = "VirtualPort ID cannot be null";
70 private static final String VIRTUALPORT_NOT_NULL = "VirtualPort cannot be null";
71 private static final String TENANTID_NOT_NULL = "TenantId cannot be null";
72 private static final String NETWORKID_NOT_NULL = "NetworkId cannot be null";
73 private static final String DEVICEID_NOT_NULL = "DeviceId cannot be null";
74 private static final String FIXEDIP_NOT_NULL = "FixedIp cannot be null";
76 protected Map<VirtualPortId, VirtualPort> vPortStore;
77 protected ApplicationId appId;
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected StorageService storageService;
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected TenantNetworkService networkService;
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected CoreService coreService;
89 public void activate() {
91 appId = coreService.registerApplication(VTNRSC_APP);
93 vPortStore = storageService.<VirtualPortId, VirtualPort>consistentMapBuilder()
94 .withName(VIRTUALPORT)
95 .withApplicationId(appId)
96 .withPurgeOnUninstall()
97 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
99 TenantNetworkId.class,
100 VirtualPort.State.class,
102 AllowedAddressPair.class,
108 DefaultVirtualPort.class))
109 .build().asJavaMap();
114 public void deactivate() {
116 log.info("Stoppped");
120 public boolean exists(VirtualPortId vPortId) {
121 checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
122 return vPortStore.containsKey(vPortId);
126 public VirtualPort getPort(VirtualPortId vPortId) {
127 checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
128 return vPortStore.get(vPortId);
132 public VirtualPort getPort(FixedIp fixedIP) {
133 checkNotNull(fixedIP, FIXEDIP_NOT_NULL);
134 List<VirtualPort> vPorts = new ArrayList<>();
135 vPortStore.values().stream().forEach(p -> {
136 Iterator<FixedIp> fixedIps = p.fixedIps().iterator();
137 while (fixedIps.hasNext()) {
138 if (fixedIps.next().equals(fixedIP)) {
144 if (vPorts.size() == 0) {
147 return vPorts.get(0);
151 public Collection<VirtualPort> getPorts() {
152 return Collections.unmodifiableCollection(vPortStore.values());
156 public Collection<VirtualPort> getPorts(TenantNetworkId networkId) {
157 checkNotNull(networkId, NETWORKID_NOT_NULL);
158 return vPortStore.values().stream().filter(d -> d.networkId().equals(networkId))
159 .collect(Collectors.toList());
163 public Collection<VirtualPort> getPorts(TenantId tenantId) {
164 checkNotNull(tenantId, TENANTID_NOT_NULL);
165 return vPortStore.values().stream().filter(d -> d.tenantId().equals(tenantId))
166 .collect(Collectors.toList());
170 public Collection<VirtualPort> getPorts(DeviceId deviceId) {
171 checkNotNull(deviceId, DEVICEID_NOT_NULL);
172 return vPortStore.values().stream().filter(d -> d.deviceId().equals(deviceId))
173 .collect(Collectors.toList());
177 public boolean createPorts(Iterable<VirtualPort> vPorts) {
178 checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
179 for (VirtualPort vPort : vPorts) {
180 log.debug("vPortId is {} ", vPort.portId().toString());
181 vPortStore.put(vPort.portId(), vPort);
182 if (!vPortStore.containsKey(vPort.portId())) {
183 log.debug("The virtualPort is created failed whose identifier is {} ",
184 vPort.portId().toString());
192 public boolean updatePorts(Iterable<VirtualPort> vPorts) {
193 checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
194 if (vPorts != null) {
195 for (VirtualPort vPort : vPorts) {
196 vPortStore.put(vPort.portId(), vPort);
197 if (!vPortStore.containsKey(vPort.portId())) {
198 log.debug("The virtualPort is not exist whose identifier is {}",
199 vPort.portId().toString());
203 vPortStore.put(vPort.portId(), vPort);
205 if (!vPort.equals(vPortStore.get(vPort.portId()))) {
206 log.debug("The virtualPort is updated failed whose identifier is {}",
207 vPort.portId().toString());
216 public boolean removePorts(Iterable<VirtualPortId> vPortIds) {
217 checkNotNull(vPortIds, VIRTUALPORT_ID_NULL);
218 if (vPortIds != null) {
219 for (VirtualPortId vPortId : vPortIds) {
220 vPortStore.remove(vPortId);
221 if (vPortStore.containsKey(vPortId)) {
222 log.debug("The virtualPort is removed failed whose identifier is {}",