Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 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.net.intent; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Sets; |
| 22 | |
| 23 | import java.util.Optional; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * Installation context for a high level Intent. |
| 28 | * Records pending and error operation contexts of installable Intents for the |
| 29 | * high level Intent. |
| 30 | */ |
| 31 | public class IntentInstallationContext { |
| 32 | private IntentData toUninstall; |
| 33 | private IntentData toInstall; |
| 34 | private Set<IntentOperationContext> pendingContexts = Sets.newConcurrentHashSet(); |
| 35 | private Set<IntentOperationContext> errorContexts = Sets.newConcurrentHashSet(); |
| 36 | |
| 37 | /** |
| 38 | * Creates an Intent installation context by given information. |
| 39 | * |
| 40 | * @param toUninstall the Intent to uninstall |
| 41 | * @param toInstall the Intent to install |
| 42 | */ |
| 43 | public IntentInstallationContext(IntentData toUninstall, IntentData toInstall) { |
| 44 | this.toUninstall = toUninstall; |
| 45 | this.toInstall = toInstall; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Removes a pending operation context. |
| 50 | * |
| 51 | * @param context the operation context to be added |
| 52 | */ |
| 53 | public void removePendingContext(IntentOperationContext context) { |
| 54 | this.pendingContexts.remove(context); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Adds a pending context. |
| 59 | * |
| 60 | * @param context the operation context to be added |
| 61 | */ |
| 62 | public void addPendingContext(IntentOperationContext context) { |
| 63 | this.pendingContexts.add(context); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Adds an error context. |
| 68 | * |
| 69 | * @param context the error context to be added. |
| 70 | */ |
| 71 | public void addErrorContext(IntentOperationContext context) { |
| 72 | this.errorContexts.add(context); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Retrieves the pending contexts. |
| 77 | * |
| 78 | * @return the pending contexts |
| 79 | */ |
| 80 | public Set<IntentOperationContext> pendingContexts() { |
| 81 | return ImmutableSet.copyOf(pendingContexts); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Retrieves the error contexts. |
| 86 | * |
| 87 | * @return the error contexts |
| 88 | */ |
| 89 | public Set<IntentOperationContext> errorContexts() { |
| 90 | return ImmutableSet.copyOf(errorContexts); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Check if pending context is empty. |
| 95 | * |
| 96 | * @return true if pending contexts is empty; false otherwise |
| 97 | */ |
| 98 | public synchronized boolean isPendingContextsEmpty() { |
| 99 | return pendingContexts.isEmpty(); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return MoreObjects.toStringHelper(this) |
| 105 | .add("pendingContexts", pendingContexts) |
| 106 | .add("errorContexts", errorContexts) |
| 107 | .toString(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Retrieves the Intent data which to be uninstalled. |
| 112 | * |
| 113 | * @return the Intent data; empty value if not exists |
| 114 | */ |
| 115 | public Optional<IntentData> toUninstall() { |
| 116 | return Optional.ofNullable(toUninstall); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Retrieves the Intent data which to be installed. |
| 121 | * |
| 122 | * @return the Intent data; empty value if not exists |
| 123 | */ |
| 124 | public Optional<IntentData> toInstall() { |
| 125 | return Optional.ofNullable(toInstall); |
| 126 | } |
| 127 | |
| 128 | } |