Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.intent.impl; |
| 17 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.intent.Intent; |
| 24 | import org.onlab.onos.net.intent.IntentEvent; |
| 25 | import org.onlab.onos.net.intent.IntentId; |
| 26 | import org.onlab.onos.net.intent.IntentState; |
| 27 | import org.onlab.onos.net.intent.IntentStore; |
| 28 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 29 | import org.onlab.onos.store.AbstractStore; |
| 30 | import org.slf4j.Logger; |
| 31 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.concurrent.ConcurrentHashMap; |
| 35 | |
| 36 | import static org.onlab.onos.net.intent.IntentState.*; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 38 | |
| 39 | //FIXME: I LIE I AM NOT DISTRIBUTED |
| 40 | @Component(immediate = true) |
| 41 | @Service |
| 42 | public class DistributedIntentStore |
| 43 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
| 44 | implements IntentStore { |
| 45 | |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | private final Map<IntentId, Intent> intents = new ConcurrentHashMap<>(); |
| 48 | private final Map<IntentId, IntentState> states = new ConcurrentHashMap<>(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 49 | private final Map<IntentId, List<Intent>> installable = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 50 | |
| 51 | @Activate |
| 52 | public void activate() { |
| 53 | log.info("Started"); |
| 54 | } |
| 55 | |
| 56 | @Deactivate |
| 57 | public void deactivate() { |
| 58 | log.info("Stopped"); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public IntentEvent createIntent(Intent intent) { |
| 63 | intents.put(intent.id(), intent); |
| 64 | return this.setState(intent, IntentState.SUBMITTED); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public IntentEvent removeIntent(IntentId intentId) { |
| 69 | Intent intent = intents.remove(intentId); |
| 70 | installable.remove(intentId); |
| 71 | IntentEvent event = this.setState(intent, WITHDRAWN); |
| 72 | states.remove(intentId); |
| 73 | return event; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public long getIntentCount() { |
| 78 | return intents.size(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Iterable<Intent> getIntents() { |
| 83 | return ImmutableSet.copyOf(intents.values()); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Intent getIntent(IntentId intentId) { |
| 88 | return intents.get(intentId); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public IntentState getIntentState(IntentId id) { |
| 93 | return states.get(id); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public IntentEvent setState(Intent intent, IntentState state) { |
| 98 | IntentId id = intent.id(); |
| 99 | states.put(id, state); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 100 | IntentEvent.Type type = null; |
| 101 | |
| 102 | switch (state) { |
| 103 | case SUBMITTED: |
| 104 | type = IntentEvent.Type.SUBMITTED; |
| 105 | break; |
| 106 | case INSTALLED: |
| 107 | type = IntentEvent.Type.INSTALLED; |
| 108 | break; |
| 109 | case FAILED: |
| 110 | type = IntentEvent.Type.FAILED; |
| 111 | break; |
| 112 | case WITHDRAWN: |
| 113 | type = IntentEvent.Type.WITHDRAWN; |
| 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
| 118 | if (type == null) { |
| 119 | return null; |
| 120 | } |
| 121 | return new IntentEvent(type, intent); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 125 | public void addInstallableIntents(IntentId intentId, List<Intent> result) { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 126 | installable.put(intentId, result); |
| 127 | } |
| 128 | |
| 129 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 130 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 131 | return installable.get(intentId); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void removeInstalledIntents(IntentId intentId) { |
| 136 | installable.remove(intentId); |
| 137 | } |
| 138 | |
| 139 | } |