blob: 21c848be834309d8f85633376310bcf6a6f67039 [file] [log] [blame]
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +05303 *
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 */
16package org.onosproject.vtnrsc.portchain.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Collections;
22
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
29import org.onlab.util.KryoNamespace;
Mahesh Poojary Sa1075a92015-11-29 15:53:56 +053030import org.onosproject.event.AbstractListenerManager;
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053031import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswala3f51b52015-12-11 01:09:02 +053033import org.onosproject.store.service.EventuallyConsistentMapEvent;
34import org.onosproject.store.service.EventuallyConsistentMapListener;
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053035import org.onosproject.store.service.MultiValuedTimestamp;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.WallClockTimestamp;
38import org.onosproject.vtnrsc.PortChain;
39import org.onosproject.vtnrsc.PortChainId;
Mahesh Poojary Sa1075a92015-11-29 15:53:56 +053040import org.onosproject.vtnrsc.portchain.PortChainEvent;
41import org.onosproject.vtnrsc.portchain.PortChainListener;
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053042import org.onosproject.vtnrsc.portchain.PortChainService;
43import org.slf4j.Logger;
44
45/**
46 * Provides implementation of the portChainService.
47 */
48@Component(immediate = true)
49@Service
Mahesh Poojary Sa1075a92015-11-29 15:53:56 +053050public class PortChainManager extends AbstractListenerManager<PortChainEvent, PortChainListener> implements
51 PortChainService {
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053052
53 private static final String PORT_CHAIN_ID_NULL = "PortChain ID cannot be null";
54 private static final String PORT_CHAIN_NULL = "PortChain cannot be null";
Bharat saraswala3f51b52015-12-11 01:09:02 +053055 private static final String EVENT_NOT_NULL = "event cannot be null";
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053056
Mahesh Poojary Sa1075a92015-11-29 15:53:56 +053057 private final Logger log = getLogger(getClass());
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053058 private EventuallyConsistentMap<PortChainId, PortChain> portChainStore;
59
Bharat saraswala3f51b52015-12-11 01:09:02 +053060 private EventuallyConsistentMapListener<PortChainId, PortChain> portChainListener =
61 new InnerPortChainStoreListener();
62
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected StorageService storageService;
65
66 @Activate
67 public void activate() {
68
Bharat saraswala3f51b52015-12-11 01:09:02 +053069 eventDispatcher.addSink(PortChainEvent.class, listenerRegistry);
70
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053071 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
72 .register(KryoNamespaces.API)
73 .register(MultiValuedTimestamp.class)
74 .register(PortChain.class);
75
76 portChainStore = storageService
77 .<PortChainId, PortChain>eventuallyConsistentMapBuilder()
78 .withName("portchainstore").withSerializer(serializer)
79 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
80
Bharat saraswala3f51b52015-12-11 01:09:02 +053081 portChainStore.addListener(portChainListener);
82
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053083 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
Bharat saraswala3f51b52015-12-11 01:09:02 +053088 eventDispatcher.removeSink(PortChainEvent.class);
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +053089 portChainStore.destroy();
90 log.info("Stopped");
91 }
92
93 @Override
94 public boolean exists(PortChainId portChainId) {
95 checkNotNull(portChainId, PORT_CHAIN_ID_NULL);
96 return portChainStore.containsKey(portChainId);
97 }
98
99 @Override
100 public int getPortChainCount() {
101 return portChainStore.size();
102 }
103
104 @Override
105 public Iterable<PortChain> getPortChains() {
106 return Collections.unmodifiableCollection(portChainStore.values());
107 }
108
109 @Override
110 public PortChain getPortChain(PortChainId portChainId) {
111 checkNotNull(portChainId, PORT_CHAIN_ID_NULL);
112 return portChainStore.get(portChainId);
113 }
114
115 @Override
116 public boolean createPortChain(PortChain portChain) {
117 checkNotNull(portChain, PORT_CHAIN_NULL);
118
119 portChainStore.put(portChain.portChainId(), portChain);
120 if (!portChainStore.containsKey(portChain.portChainId())) {
121 log.debug("The portChain is created failed which identifier was {}", portChain.portChainId()
122 .toString());
123 return false;
124 }
125 return true;
126 }
127
128 @Override
129 public boolean updatePortChain(PortChain portChain) {
130 checkNotNull(portChain, PORT_CHAIN_NULL);
131
132 if (!portChainStore.containsKey(portChain.portChainId())) {
133 log.debug("The portChain is not exist whose identifier was {} ",
134 portChain.portChainId().toString());
135 return false;
136 }
137
138 portChainStore.put(portChain.portChainId(), portChain);
139
140 if (!portChain.equals(portChainStore.get(portChain.portChainId()))) {
141 log.debug("The portChain is updated failed whose identifier was {} ",
142 portChain.portChainId().toString());
143 return false;
144 }
145 return true;
146 }
147
148 @Override
149 public boolean removePortChain(PortChainId portChainId) {
150 checkNotNull(portChainId, PORT_CHAIN_NULL);
151
152 portChainStore.remove(portChainId);
153 if (portChainStore.containsKey(portChainId)) {
154 log.debug("The portChain is removed failed whose identifier was {}",
155 portChainId.toString());
156 return false;
157 }
158 return true;
159 }
Bharat saraswala3f51b52015-12-11 01:09:02 +0530160
161 private class InnerPortChainStoreListener
162 implements
163 EventuallyConsistentMapListener<PortChainId, PortChain> {
164
165 @Override
166 public void event(EventuallyConsistentMapEvent<PortChainId, PortChain> event) {
167 checkNotNull(event, EVENT_NOT_NULL);
168 PortChain portChain = event.value();
169 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
170 notifyListeners(new PortChainEvent(
171 PortChainEvent.Type.PORT_CHAIN_PUT,
172 portChain));
173 }
174 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
175 notifyListeners(new PortChainEvent(
176 PortChainEvent.Type.PORT_CHAIN_DELETE,
177 portChain));
178 }
179 }
180 }
181
182 /**
183 * Notifies specify event to all listeners.
184 *
185 * @param event port chain event
186 */
187 private void notifyListeners(PortChainEvent event) {
188 checkNotNull(event, EVENT_NOT_NULL);
189 post(event);
190 }
Phaneendra Mandaf5c14e62015-10-29 19:29:52 +0530191}