Pier Luigi Ventre | 0a023f4 | 2016-04-30 11:03:15 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.sdxl2; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 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.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.store.primitives.DefaultDistributedSet; |
| 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.store.service.DistributedPrimitive; |
| 30 | import org.onosproject.store.service.DistributedSet; |
| 31 | import org.onosproject.store.service.Serializer; |
| 32 | import org.onosproject.store.service.StorageService; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Set; |
| 37 | |
| 38 | /** |
| 39 | * SDXL2 Store implementation backed by different distributed primitives. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class SdxL2DistributedStore implements SdxL2Store { |
| 44 | |
| 45 | private static Logger log = LoggerFactory.getLogger(SdxL2DistributedStore.class); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | private StorageService storageService; |
| 49 | |
| 50 | private DistributedSet<String> sdxL2s; |
| 51 | |
| 52 | private static String errorAddSdx = "It is not possible to add "; |
| 53 | private static String errorRemoveSdx = "It is not possible to remove "; |
| 54 | |
| 55 | @Activate |
| 56 | public void activate() { |
| 57 | |
| 58 | KryoNamespace custom = KryoNamespace.newBuilder() |
| 59 | .register(KryoNamespaces.API) |
| 60 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 61 | .register(new SdxL2ConnectionPointSerializer(), new Class[]{SdxL2ConnectionPoint.class}) |
| 62 | .build(); |
| 63 | |
| 64 | sdxL2s = new DefaultDistributedSet<>(this.storageService |
| 65 | .<String>setBuilder() |
| 66 | .withSerializer(Serializer.using(custom)) |
| 67 | .withName("sdxl2s") |
| 68 | .build(), DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS); |
| 69 | |
| 70 | log.info("Started"); |
| 71 | } |
| 72 | |
| 73 | @Deactivate |
| 74 | public void deactivate() { |
| 75 | log.info("Stopped"); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Create a named sdx-l2. |
| 80 | * |
| 81 | * @param sdxl2 sdx-l2 name |
| 82 | * @throws SdxL2Exception if sdxl2 exists |
| 83 | */ |
| 84 | @Override |
| 85 | public void putSdxL2(String sdxl2) throws SdxL2Exception { |
| 86 | boolean inserted = sdxL2s.add(sdxl2); |
| 87 | if (!inserted) { |
| 88 | throw new SdxL2Exception(errorAddSdx + sdxl2); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Remove a named sdx-l2. |
| 94 | * |
| 95 | * @param sdxl2 sdx-l2 name |
| 96 | * @throws SdxL2Exception if sdxl2 does not exist |
| 97 | */ |
| 98 | @Override |
| 99 | public void removeSdxL2(String sdxl2) throws SdxL2Exception { |
| 100 | boolean removed = sdxL2s.remove(sdxl2); |
| 101 | if (!removed) { |
| 102 | throw new SdxL2Exception(errorRemoveSdx + sdxl2); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns a set of sdxl2 names. |
| 108 | * |
| 109 | * @return a set of sdxl2 names |
| 110 | */ |
| 111 | @Override |
| 112 | public Set<String> getSdxL2s() { |
| 113 | return ImmutableSet.copyOf(sdxL2s); |
| 114 | } |
| 115 | |
| 116 | } |