5201a2ca62788839ca9861a3478d4dbd9697fca4
[onosfw.git] /
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.portchain.impl;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.slf4j.LoggerFactory.getLogger;
20
21 import java.util.Collections;
22
23 import org.apache.felix.scr.annotations.Activate;
24 import org.apache.felix.scr.annotations.Component;
25 import org.apache.felix.scr.annotations.Deactivate;
26 import org.apache.felix.scr.annotations.Reference;
27 import org.apache.felix.scr.annotations.ReferenceCardinality;
28 import org.apache.felix.scr.annotations.Service;
29 import org.onlab.util.KryoNamespace;
30 import org.onosproject.store.serializers.KryoNamespaces;
31 import org.onosproject.store.service.EventuallyConsistentMap;
32 import org.onosproject.store.service.MultiValuedTimestamp;
33 import org.onosproject.store.service.StorageService;
34 import org.onosproject.store.service.WallClockTimestamp;
35 import org.onosproject.vtnrsc.PortChain;
36 import org.onosproject.vtnrsc.PortChainId;
37 import org.onosproject.vtnrsc.portchain.PortChainService;
38 import org.slf4j.Logger;
39
40 /**
41  * Provides implementation of the portChainService.
42  */
43 @Component(immediate = true)
44 @Service
45 public class PortChainManager implements PortChainService {
46
47     private final Logger log = getLogger(getClass());
48
49     private static final String PORT_CHAIN_ID_NULL = "PortChain ID cannot be null";
50     private static final String PORT_CHAIN_NULL = "PortChain cannot be null";
51
52     private EventuallyConsistentMap<PortChainId, PortChain> portChainStore;
53
54     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55     protected StorageService storageService;
56
57     @Activate
58     public void activate() {
59
60         KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
61                 .register(KryoNamespaces.API)
62                 .register(MultiValuedTimestamp.class)
63                 .register(PortChain.class);
64
65         portChainStore = storageService
66                 .<PortChainId, PortChain>eventuallyConsistentMapBuilder()
67                 .withName("portchainstore").withSerializer(serializer)
68                 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
69
70         log.info("Started");
71     }
72
73     @Deactivate
74     public void deactivate() {
75         portChainStore.destroy();
76         log.info("Stopped");
77     }
78
79     @Override
80     public boolean exists(PortChainId portChainId) {
81         checkNotNull(portChainId, PORT_CHAIN_ID_NULL);
82         return portChainStore.containsKey(portChainId);
83     }
84
85     @Override
86     public int getPortChainCount() {
87         return portChainStore.size();
88     }
89
90     @Override
91     public Iterable<PortChain> getPortChains() {
92         return Collections.unmodifiableCollection(portChainStore.values());
93     }
94
95     @Override
96     public PortChain getPortChain(PortChainId portChainId) {
97         checkNotNull(portChainId, PORT_CHAIN_ID_NULL);
98         return portChainStore.get(portChainId);
99     }
100
101     @Override
102     public boolean createPortChain(PortChain portChain) {
103         checkNotNull(portChain, PORT_CHAIN_NULL);
104
105         portChainStore.put(portChain.portChainId(), portChain);
106         if (!portChainStore.containsKey(portChain.portChainId())) {
107             log.debug("The portChain is created failed which identifier was {}", portChain.portChainId()
108                       .toString());
109             return false;
110         }
111         return true;
112     }
113
114     @Override
115     public boolean updatePortChain(PortChain portChain) {
116         checkNotNull(portChain, PORT_CHAIN_NULL);
117
118         if (!portChainStore.containsKey(portChain.portChainId())) {
119             log.debug("The portChain is not exist whose identifier was {} ",
120                       portChain.portChainId().toString());
121             return false;
122         }
123
124         portChainStore.put(portChain.portChainId(), portChain);
125
126         if (!portChain.equals(portChainStore.get(portChain.portChainId()))) {
127             log.debug("The portChain is updated failed whose identifier was {} ",
128                       portChain.portChainId().toString());
129             return false;
130         }
131         return true;
132     }
133
134     @Override
135     public boolean removePortChain(PortChainId portChainId) {
136         checkNotNull(portChainId, PORT_CHAIN_NULL);
137
138         portChainStore.remove(portChainId);
139         if (portChainStore.containsKey(portChainId)) {
140             log.debug("The portChain is removed failed whose identifier was {}",
141                       portChainId.toString());
142             return false;
143         }
144         return true;
145     }
146 }