jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.workflow.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.osgi.DefaultServiceDirectory; |
| 20 | import org.onlab.osgi.ServiceNotFoundException; |
| 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
| 24 | import java.util.stream.Collectors; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation of workplace. |
| 28 | */ |
| 29 | public class DefaultWorkplace extends Workplace { |
| 30 | |
| 31 | /** |
| 32 | * Name of workplace. |
| 33 | */ |
| 34 | private String name; |
| 35 | |
| 36 | /** |
| 37 | * Constructor of DefaultWorkplace. |
| 38 | * @param name name of workplace |
| 39 | * @param data data model tree |
| 40 | */ |
| 41 | public DefaultWorkplace(String name, DataModelTree data) { |
| 42 | super(data); |
| 43 | this.name = name; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String name() { |
| 48 | return this.name; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String distributor() { |
| 53 | return name(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Collection<WorkflowContext> getContexts() throws WorkflowException { |
| 58 | WorkplaceStore workplaceStore; |
| 59 | try { |
| 60 | workplaceStore = DefaultServiceDirectory.getService(WorkplaceStore.class); |
| 61 | } catch (ServiceNotFoundException e) { |
| 62 | throw new WorkflowException(e); |
| 63 | } |
| 64 | |
| 65 | return workplaceStore.getWorkplaceContexts(name()); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns collection of context names. |
| 70 | * @return collection of context names |
| 71 | */ |
| 72 | private Collection<String> getContextNames() { |
| 73 | Collection<WorkflowContext> ctx; |
| 74 | try { |
| 75 | ctx = getContexts(); |
| 76 | } catch (WorkflowException e) { |
| 77 | ctx = Collections.emptyList(); |
| 78 | } |
| 79 | |
| 80 | return ctx.stream().map(x -> x.name()).collect(Collectors.toList()); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | return MoreObjects.toStringHelper(getClass()) |
| 86 | .add("name", name()) |
| 87 | .add("triggernext", triggerNext()) |
| 88 | .add("context", data()) |
| 89 | .add("contexts", getContextNames()) |
| 90 | .toString(); |
| 91 | } |
| 92 | } |