d6f9d91d7bb75e1605cb7ff117019b7e66651a0c
[vtn.git] /
1 /*
2  * Copyright (c) 2015 NEC Corporation. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.vtn.manager.internal.routing;
10
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.opendaylight.vtn.manager.VTNException;
18
19 import org.opendaylight.vtn.manager.internal.FlowRemover;
20 import org.opendaylight.vtn.manager.internal.TxContext;
21 import org.opendaylight.vtn.manager.internal.VTNManagerProvider;
22 import org.opendaylight.vtn.manager.internal.flow.remove.AllFlowRemover;
23 import org.opendaylight.vtn.manager.internal.flow.remove.TenantFlowRemover;
24 import org.opendaylight.vtn.manager.internal.util.pathmap.PathMapUtils;
25 import org.opendaylight.vtn.manager.internal.util.rpc.RpcException;
26 import org.opendaylight.vtn.manager.internal.util.rpc.RpcOutputGenerator;
27 import org.opendaylight.vtn.manager.internal.util.rpc.RpcUtils;
28 import org.opendaylight.vtn.manager.internal.util.tx.CompositeTxTask;
29 import org.opendaylight.vtn.manager.internal.util.vnode.VTenantIdentifier;
30
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathmap.rev150328.RemovePathMapInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathmap.rev150328.RemovePathMapOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathmap.rev150328.RemovePathMapOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathmap.rev150328.remove.path.map.output.RemovePathMapResult;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathmap.rev150328.remove.path.map.output.RemovePathMapResultBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathmap.rev150328.vtn.path.map.list.VtnPathMap;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.types.rev150209.VtnUpdateType;
40
41 /**
42  * {@code RemovePathMapTask} describes the MD-SAL datastore transaction task
43  * that deletes all the specified path map configurations from the global or
44  * VTN path map.
45  *
46  * @see #create(RemovePathMapInput)
47  */
48 public final class RemovePathMapTask
49     extends CompositeTxTask<VtnUpdateType, RemoveMapTask>
50     implements RpcOutputGenerator<List<VtnUpdateType>, RemovePathMapOutput> {
51     /**
52      * The identifier for the target VTN.
53      *
54      * <p>
55      *   {@code null} means that the global path map is targeted.
56      * </p>
57      */
58     private final VTenantIdentifier  identifier;
59
60     /**
61      * Construct a new task that removes all the given path map configurations
62      * from the global or VTN path map.
63      *
64      * @param input  A {@link RemovePathMapInput} instance.
65      * @return  A {@link RemovePathMapTask} instance associated with the task
66      *          that removes the given path map configuration.
67      * @throws RpcException
68      *     The given input contains invalid value.
69      */
70     public static RemovePathMapTask create(RemovePathMapInput input)
71         throws RpcException {
72         if (input == null) {
73             throw RpcUtils.getNullInputException();
74         }
75
76         String tname = input.getTenantName();
77         VTenantIdentifier ident = (tname == null)
78             ? null
79             : VTenantIdentifier.create(tname, true);
80
81         List<Integer> indexList = input.getMapIndex();
82         if (indexList == null || indexList.isEmpty()) {
83             throw PathMapUtils.getNullMapIndexException();
84         }
85
86         Set<Integer> idxSet = new HashSet<>();
87         List<RemoveMapTask> taskList = new ArrayList<>();
88         for (Integer index: indexList) {
89             if (idxSet.add(index)) {
90                 InstanceIdentifier<VtnPathMap> path = (ident == null)
91                     ? PathMapUtils.getIdentifier(index)
92                     : PathMapUtils.getIdentifier(ident, index);
93                 taskList.add(new RemoveMapTask(path, index));
94             }
95         }
96
97         return new RemovePathMapTask(ident, taskList);
98     }
99
100     /**
101      * Construct a new instance.
102      *
103      * @param ident  The identifier for the target VTN.
104      *               {@code null} means that the global path map is targeted.
105      * @param tasks  A list of tasks that delete path map configuration.
106      */
107     private RemovePathMapTask(VTenantIdentifier ident,
108                               List<RemoveMapTask> tasks) {
109         super(tasks);
110         identifier = ident;
111     }
112
113     // CompositeTxTask
114
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     protected void onStarted(TxContext ctx) throws VTNException {
120         if (identifier != null) {
121             // Ensure that the target VTN is present.
122             identifier.fetch(ctx.getReadWriteTransaction());
123         }
124     }
125
126     // TxTask
127
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void onSuccess(VTNManagerProvider provider,
133                           List<VtnUpdateType> result) {
134         for (VtnUpdateType status: result) {
135             if (status != null) {
136                 // REVISIT: Select flow entries affected by the change.
137                 FlowRemover remover = (identifier == null)
138                     ? new AllFlowRemover()
139                     : new TenantFlowRemover(identifier);
140                 addBackgroundTask(provider.removeFlows(remover));
141                 break;
142             }
143         }
144     }
145
146     // RpcOutputGenerator
147
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public Class<RemovePathMapOutput> getOutputType() {
153         return RemovePathMapOutput.class;
154     }
155
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public RemovePathMapOutput createOutput(
161         List<VtnUpdateType> result) {
162         List<RemovePathMapResult> list = new ArrayList<>();
163         Iterator<RemoveMapTask> taskIterator = getSubTasks().iterator();
164         for (VtnUpdateType status: result) {
165             RemoveMapTask task = taskIterator.next();
166             RemovePathMapResult res = new RemovePathMapResultBuilder().
167                 setIndex(task.getIndex()).setStatus(status).build();
168             list.add(res);
169         }
170
171         return new RemovePathMapOutputBuilder().
172             setRemovePathMapResult(list).build();
173     }
174 }