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.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
24 import java.util.stream.Collectors;
26 import org.apache.felix.scr.annotations.Activate;
27 import org.apache.felix.scr.annotations.Component;
28 import org.apache.felix.scr.annotations.Deactivate;
29 import org.apache.felix.scr.annotations.Reference;
30 import org.apache.felix.scr.annotations.ReferenceCardinality;
31 import org.apache.felix.scr.annotations.Service;
32 import org.onlab.packet.IpAddress;
33 import org.onosproject.core.ApplicationId;
34 import org.onosproject.core.CoreService;
35 import org.onosproject.net.DeviceId;
36 import org.onosproject.store.serializers.KryoNamespaces;
37 import org.onosproject.store.service.Serializer;
38 import org.onosproject.store.service.StorageService;
39 import org.onosproject.vtnrsc.AllowedAddressPair;
40 import org.onosproject.vtnrsc.BindingHostId;
41 import org.onosproject.vtnrsc.DefaultVirtualPort;
42 import org.onosproject.vtnrsc.FixedIp;
43 import org.onosproject.vtnrsc.SecurityGroup;
44 import org.onosproject.vtnrsc.SubnetId;
45 import org.onosproject.vtnrsc.TenantId;
46 import org.onosproject.vtnrsc.TenantNetworkId;
47 import org.onosproject.vtnrsc.VirtualPort;
48 import org.onosproject.vtnrsc.VirtualPortId;
49 import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
50 import org.onosproject.vtnrsc.virtualport.VirtualPortService;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Provides implementation of the VirtualPort APIs.
57 @Component(immediate = true)
59 public class VirtualPortManager implements VirtualPortService {
61 private final Logger log = LoggerFactory.getLogger(getClass());
63 private static final String VIRTUALPORT = "vtn-virtual-port";
64 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
66 private static final String VIRTUALPORT_ID_NULL = "VirtualPort ID cannot be null";
67 private static final String VIRTUALPORT_NOT_NULL = "VirtualPort cannot be null";
68 private static final String TENANTID_NOT_NULL = "TenantId cannot be null";
69 private static final String NETWORKID_NOT_NULL = "NetworkId cannot be null";
70 private static final String DEVICEID_NOT_NULL = "DeviceId cannot be null";
72 protected Map<VirtualPortId, VirtualPort> vPortStore;
73 protected ApplicationId appId;
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected StorageService storageService;
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected TenantNetworkService networkService;
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected CoreService coreService;
85 public void activate() {
87 appId = coreService.registerApplication(VTNRSC_APP);
89 vPortStore = storageService.<VirtualPortId, VirtualPort>consistentMapBuilder()
90 .withName(VIRTUALPORT)
91 .withApplicationId(appId)
92 .withPurgeOnUninstall()
93 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
95 TenantNetworkId.class,
96 VirtualPort.State.class,
98 AllowedAddressPair.class,
104 DefaultVirtualPort.class))
105 .build().asJavaMap();
110 public void deactivate() {
112 log.info("Stoppped");
116 public boolean exists(VirtualPortId vPortId) {
117 checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
118 return vPortStore.containsKey(vPortId);
122 public VirtualPort getPort(VirtualPortId vPortId) {
123 checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
124 return vPortStore.get(vPortId);
128 public Collection<VirtualPort> getPorts() {
129 return Collections.unmodifiableCollection(vPortStore.values());
133 public Collection<VirtualPort> getPorts(TenantNetworkId networkId) {
134 checkNotNull(networkId, NETWORKID_NOT_NULL);
135 return vPortStore.values().stream().filter(d -> d.networkId().equals(networkId))
136 .collect(Collectors.toList());
140 public Collection<VirtualPort> getPorts(TenantId tenantId) {
141 checkNotNull(tenantId, TENANTID_NOT_NULL);
142 return vPortStore.values().stream().filter(d -> d.tenantId().equals(tenantId))
143 .collect(Collectors.toList());
147 public Collection<VirtualPort> getPorts(DeviceId deviceId) {
148 checkNotNull(deviceId, DEVICEID_NOT_NULL);
149 return vPortStore.values().stream().filter(d -> d.deviceId().equals(deviceId))
150 .collect(Collectors.toList());
154 public boolean createPorts(Iterable<VirtualPort> vPorts) {
155 checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
156 for (VirtualPort vPort : vPorts) {
157 log.debug("vPortId is {} ", vPort.portId().toString());
158 vPortStore.put(vPort.portId(), vPort);
159 if (!vPortStore.containsKey(vPort.portId())) {
160 log.debug("The virtualPort is created failed whose identifier is {} ",
161 vPort.portId().toString());
169 public boolean updatePorts(Iterable<VirtualPort> vPorts) {
170 checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
171 if (vPorts != null) {
172 for (VirtualPort vPort : vPorts) {
173 vPortStore.put(vPort.portId(), vPort);
174 if (!vPortStore.containsKey(vPort.portId())) {
175 log.debug("The virtualPort is not exist whose identifier is {}",
176 vPort.portId().toString());
180 vPortStore.put(vPort.portId(), vPort);
182 if (!vPort.equals(vPortStore.get(vPort.portId()))) {
183 log.debug("The virtualPort is updated failed whose identifier is {}",
184 vPort.portId().toString());
193 public boolean removePorts(Iterable<VirtualPortId> vPortIds) {
194 checkNotNull(vPortIds, VIRTUALPORT_ID_NULL);
195 if (vPortIds != null) {
196 for (VirtualPortId vPortId : vPortIds) {
197 vPortStore.remove(vPortId);
198 if (vPortStore.containsKey(vPortId)) {
199 log.debug("The virtualPort is removed failed whose identifier is {}",