Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 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.vtnrsc.subnet.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.onlab.packet.IpAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.Serializer; |
| 29 | import org.onosproject.store.service.StorageService; |
| 30 | import org.onosproject.vtnrsc.AllocationPool; |
| 31 | import org.onosproject.vtnrsc.DefaultAllocationPool; |
| 32 | import org.onosproject.vtnrsc.DefaultHostRoute; |
| 33 | import org.onosproject.vtnrsc.DefaultSubnet; |
| 34 | import org.onosproject.vtnrsc.HostRoute; |
| 35 | import org.onosproject.vtnrsc.Subnet; |
| 36 | import org.onosproject.vtnrsc.SubnetId; |
| 37 | import org.onosproject.vtnrsc.TenantId; |
| 38 | import org.onosproject.vtnrsc.TenantNetworkId; |
| 39 | import org.onosproject.vtnrsc.subnet.SubnetService; |
| 40 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.Arrays; |
| 44 | import java.util.Collections; |
| 45 | import java.util.Map; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Provides implementation of the Subnet service. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | @Service |
| 55 | public class SubnetManager implements SubnetService { |
| 56 | |
| 57 | private static final String SUBNET_ID_NULL = "Subnet ID cannot be null"; |
| 58 | private static final String SUBNET_NOT_NULL = "Subnet cannot be null"; |
| 59 | private static final String SUBNET = "vtn-subnet-store"; |
| 60 | private static final String VTNRSC_APP = "org.onosproject.vtnrsc"; |
| 61 | |
| 62 | |
| 63 | private final Logger log = getLogger(getClass()); |
| 64 | |
| 65 | protected Map<SubnetId, Subnet> subnetStore; |
| 66 | protected ApplicationId appId; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected StorageService storageService; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected CoreService coreService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected TenantNetworkService tenantNetworkService; |
| 76 | |
| 77 | @Activate |
| 78 | public void activate() { |
| 79 | |
| 80 | appId = coreService.registerApplication(VTNRSC_APP); |
| 81 | |
| 82 | subnetStore = storageService.<SubnetId, Subnet>consistentMapBuilder() |
| 83 | .withName(SUBNET) |
| 84 | .withApplicationId(appId) |
| 85 | .withPurgeOnUninstall() |
| 86 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API), |
| 87 | Subnet.class, |
| 88 | SubnetId.class, |
| 89 | TenantNetworkId.class, |
| 90 | TenantId.class, |
| 91 | HostRoute.class, |
| 92 | DefaultHostRoute.class, |
| 93 | Subnet.Mode.class, |
| 94 | AllocationPool.class, |
| 95 | DefaultAllocationPool.class, |
| 96 | DefaultSubnet.class, |
| 97 | IpAddress.Version.class)) |
| 98 | .build().asJavaMap(); |
| 99 | |
| 100 | log.info("Started"); |
| 101 | } |
| 102 | |
| 103 | @Deactivate |
| 104 | public void deactivate() { |
| 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Iterable<Subnet> getSubnets() { |
| 110 | return Collections.unmodifiableCollection(subnetStore.values()); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Subnet getSubnet(SubnetId subnetId) { |
| 115 | checkNotNull(subnetId, SUBNET_ID_NULL); |
| 116 | return subnetStore.get(subnetId); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean exists(SubnetId subnetId) { |
| 121 | checkNotNull(subnetId, SUBNET_ID_NULL); |
| 122 | return subnetStore.containsKey(subnetId); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean createSubnets(Iterable<Subnet> subnets) { |
| 127 | checkNotNull(subnets, SUBNET_NOT_NULL); |
| 128 | for (Subnet subnet : subnets) { |
| 129 | if (!tenantNetworkService.exists(subnet.networkId())) { |
| 130 | log.debug("The network identifier that the subnet {} belong to is not exist", |
| 131 | subnet.networkId().toString(), subnet.id().toString()); |
| 132 | return false; |
| 133 | } |
| 134 | subnetStore.put(subnet.id(), subnet); |
| 135 | if (!subnetStore.containsKey(subnet.id())) { |
| 136 | log.debug("The identified subnet whose identifier is {} create failed", |
| 137 | subnet.id().toString()); |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean updateSubnets(Iterable<Subnet> subnets) { |
| 146 | checkNotNull(subnets, SUBNET_NOT_NULL); |
Satish K | ca0bc8b | 2015-11-28 16:06:54 +0530 | [diff] [blame^] | 147 | for (Subnet subnet : subnets) { |
| 148 | if (!subnetStore.containsKey(subnet.id())) { |
| 149 | log.debug("The subnet is not exist whose identifier is {}", |
| 150 | subnet.id().toString()); |
| 151 | return false; |
| 152 | } |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 153 | |
Satish K | ca0bc8b | 2015-11-28 16:06:54 +0530 | [diff] [blame^] | 154 | subnetStore.put(subnet.id(), subnet); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 155 | |
Satish K | ca0bc8b | 2015-11-28 16:06:54 +0530 | [diff] [blame^] | 156 | if (!subnet.equals(subnetStore.get(subnet.id()))) { |
| 157 | log.debug("The subnet is updated failed whose identifier is {}", |
| 158 | subnet.id().toString()); |
| 159 | return false; |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public boolean removeSubnets(Iterable<SubnetId> subnetIds) { |
| 167 | checkNotNull(subnetIds, SUBNET_ID_NULL); |
Satish K | ca0bc8b | 2015-11-28 16:06:54 +0530 | [diff] [blame^] | 168 | for (SubnetId subnetId : subnetIds) { |
| 169 | subnetStore.remove(subnetId); |
| 170 | if (subnetStore.containsKey(subnetId)) { |
| 171 | log.debug("The subnet created is failed whose identifier is {}", |
| 172 | subnetId.toString()); |
| 173 | return false; |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | } |