2 * Copyright (c) 2015 NEC Corporation
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this
7 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.vtn.manager.internal.util.pathpolicy;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
16 import org.opendaylight.vtn.manager.PathCost;
17 import org.opendaylight.vtn.manager.PathPolicy;
18 import org.opendaylight.vtn.manager.PortLocation;
19 import org.opendaylight.vtn.manager.VTNException;
21 import org.opendaylight.vtn.manager.internal.util.DataStoreUtils;
22 import org.opendaylight.vtn.manager.internal.util.MiscUtils;
23 import org.opendaylight.vtn.manager.internal.util.NodeUtils;
24 import org.opendaylight.vtn.manager.internal.util.rpc.RpcException;
26 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.VtnPathCostConfig;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.VtnPathPolicies;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.VtnPathPolicyConfig;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.vtn.path.policies.VtnPathPolicy;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.vtn.path.policies.VtnPathPolicyKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.vtn.path.policy.config.VtnPathCost;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.pathpolicy.rev150209.vtn.path.policy.config.VtnPathCostKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.types.rev150209.VtnPortDesc;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.types.rev150209.VtnUpdateOperationType;
42 * {@code PathPolicyUtils} class is a collection of utility class methods
45 public final class PathPolicyUtils {
47 * Default link cost used when the path policy does not exist.
49 public static final Long DEFAULT_LINK_COST = Long.valueOf(1L);
52 * Private constructor that protects this class from instantiating.
54 private PathPolicyUtils() {}
57 * Return a new {@link RpcException} that indicates the specified path
58 * policy is not present.
60 * @param id The identifier of the path policy.
61 * @return An {@link RpcException}.
63 public static RpcException getNotFoundException(int id) {
64 return getNotFoundException(id, null);
68 * Return a new {@link RpcException} that indicates the specified path
69 * policy is not present.
71 * @param id The identifier of the path policy.
72 * @param cause A {@link Throwable} which indicates the cause of error.
73 * @return An {@link RpcException}.
75 public static RpcException getNotFoundException(int id, Throwable cause) {
76 String msg = MiscUtils.joinColon(id, "Path policy does not exist.");
77 return RpcException.getNotFoundException(msg, cause);
81 * Return a new {@link RpcException} that indicates the given path policy
84 * @param id The identifier of the path policy.
85 * @return An {@link RpcException}.
87 public static RpcException getInvalidPolicyIdException(Integer id) {
88 String msg = MiscUtils.joinColon("Invalid path policy ID", id);
89 return RpcException.getBadArgumentException(msg);
93 * Return a new {@link RpcException} that indicates the given default cost
96 * @param cost Value for default cost.
97 * @return An {@link RpcException}.
99 public static RpcException getInvalidDefaultCostException(Long cost) {
100 String msg = MiscUtils.joinColon("Invalid default cost", cost);
101 return RpcException.getBadArgumentException(msg);
105 * Return a new {@link RpcException} that indicates the given link cost
108 * @param cost Link cost.
109 * @return An {@link RpcException}.
111 public static RpcException getInvalidCostException(Long cost) {
112 String msg = MiscUtils.joinColon("Invalid cost value", cost);
113 return RpcException.getBadArgumentException(msg);
117 * Return a new {@link RpcException} that indicates the path policy ID
120 * @return An {@link RpcException}.
122 public static RpcException getNullPolicyIdException() {
123 return MiscUtils.getNullArgumentException("Path policy ID");
127 * Return a new {@link RpcException} that indicates the path cost
128 * configuration is null.
130 * @return An {@link RpcException}.
132 public static RpcException getNullPathCostException() {
133 return MiscUtils.getNullArgumentException("Path cost");
137 * Return a new {@link RpcException} that indicates the port descriptor
140 * @return An {@link RpcException}.
142 public static RpcException getNullPortDescException() {
143 return MiscUtils.getNullArgumentException("Port descriptor");
147 * Return a new {@link RpcException} that indicates duplicate port
148 * descriptor is detected.
150 * @param loc An object that represents the switch port location.
151 * @return An {@link RpcException}.
153 public static RpcException getDuplicatePortException(Object loc) {
154 String msg = "Duplicate port descriptor: " + loc;
155 return RpcException.getBadArgumentException(msg);
159 * Return a new {@link RpcException} that indicates no switch port is
162 * @return An {@link RpcException}.
164 public static RpcException getNoSwitchPortException() {
165 return RpcException.getMissingArgumentException(
166 "At least one switch port must be specified.");
170 * Create the instance identifier for the specified path policy.
172 * @param id The identifier of the path policy.
173 * @return An {@link InstanceIdentifier} instance.
175 public static InstanceIdentifier<VtnPathPolicy> getIdentifier(Integer id) {
176 VtnPathPolicyKey key = new VtnPathPolicyKey(id);
177 return InstanceIdentifier.builder(VtnPathPolicies.class).
178 child(VtnPathPolicy.class, key).build();
182 * Create the instance identifier for the given {@link VtnPathPolicyConfig}
185 * @param vpp A {@link VtnPathPolicyConfig} instance.
186 * @return An {@link InstanceIdentifier} instance.
188 public static InstanceIdentifier<VtnPathPolicy> getIdentifier(
189 VtnPathPolicyConfig vpp) {
190 return getIdentifier(vpp.getId());
194 * Create the instance identifier for the specified path cost
197 * @param id The identifier of the path policy.
198 * @param vdesc A {@link VtnPortDesc} instance.
199 * @return An {@link InstanceIdentifier} instance.
201 public static InstanceIdentifier<VtnPathCost> getIdentifier(
202 Integer id, VtnPortDesc vdesc) {
203 VtnPathPolicyKey key = new VtnPathPolicyKey(id);
204 VtnPathCostKey ckey = new VtnPathCostKey(vdesc);
205 return InstanceIdentifier.builder(VtnPathPolicies.class).
206 child(VtnPathPolicy.class, key).
207 child(VtnPathCost.class, ckey).build();
211 * Create the instance identifier for the specified path cost information.
213 * @param id The identifier of the path policy.
214 * @param vpc A {@link VtnPathCostConfig} instance.
215 * @return An {@link InstanceIdentifier} instance.
217 public static InstanceIdentifier<VtnPathCost> getIdentifier(
218 int id, VtnPathCostConfig vpc) {
219 return getIdentifier(Integer.valueOf(id), vpc.getPortDesc());
223 * Create a {@link PathCost} instance which describes the given
224 * {@link VtnPathCost} instance.
226 * @param vpc A {@link VtnPathCost} instance.
227 * @return A {@link PathCost} instance or {@code null}.
229 public static PathCost toPathCost(VtnPathCost vpc) {
230 Long c = vpc.getCost();
232 c = DEFAULT_LINK_COST;
235 PortLocation ploc = NodeUtils.toPortLocation(vpc.getPortDesc());
240 return new PathCost(ploc, c.longValue());
244 * Create a {@link PathPolicy} instance which describes the given
245 * {@link VtnPathPolicy} instance.
247 * @param vpp A {@link VtnPathPolicy} instance.
248 * @return A {@link PathPolicy} instance.
250 public static PathPolicy toPathPolicy(VtnPathPolicy vpp) {
251 Long c = vpp.getDefaultCost();
252 long defc = (c == null) ? PathPolicy.COST_UNDEF : c.longValue();
253 List<PathCost> costs = new ArrayList<>();
254 List<VtnPathCost> vlist = vpp.getVtnPathCost();
256 for (VtnPathCost vpc: vlist) {
257 PathCost pc = toPathCost(vpc);
264 return new PathPolicy(vpp.getId(), defc, costs);
268 * Read all the path policies from the MD-SAL datastore.
270 * @param rtx A {@link ReadTransaction} instance.
271 * @return A list of {@link VtnPathPolicy} instances.
272 * @throws VTNException An error occurred.
274 public static List<VtnPathPolicy> readVtnPathPolicies(ReadTransaction rtx)
275 throws VTNException {
276 InstanceIdentifier<VtnPathPolicies> path =
277 InstanceIdentifier.create(VtnPathPolicies.class);
278 LogicalDatastoreType store = LogicalDatastoreType.OPERATIONAL;
279 VtnPathPolicies policies = DataStoreUtils.read(rtx, store, path).
281 List<VtnPathPolicy> vlist = null;
282 if (policies != null) {
283 vlist = policies.getVtnPathPolicy();
286 vlist = Collections.<VtnPathPolicy>emptyList();
293 * Read path policy configuration specified by the given ID.
295 * @param rtx A {@link ReadTransaction} instance.
296 * @param id The path policy identifier.
297 * @return A {@link VtnPathPolicy} instance.
298 * @throws VTNException An error occurred.
300 public static VtnPathPolicy readVtnPathPolicy(ReadTransaction rtx, int id)
301 throws VTNException {
302 InstanceIdentifier<VtnPathPolicy> path =
303 getIdentifier(Integer.valueOf(id));
304 LogicalDatastoreType store = LogicalDatastoreType.OPERATIONAL;
305 VtnPathPolicy vpp = DataStoreUtils.read(rtx, store, path).orNull();
307 throw getNotFoundException(id);
314 * Read the cost information associated with the given port location
315 * in the given path policy.
317 * @param rtx A {@link ReadTransaction} instance.
318 * @param id The path policy identifier.
319 * @param vdesc A {@link VtnPortDesc} instance if found.
320 * {@code null} if not found.
321 * @return A {@link VtnPathCost} instance.
322 * @throws VTNException An error occurred.
324 public static VtnPathCost readVtnPathCost(ReadTransaction rtx, int id,
326 throws VTNException {
327 VtnPathCost vpc = null;
329 InstanceIdentifier<VtnPathCost> path =
330 getIdentifier(Integer.valueOf(id), vdesc);
331 LogicalDatastoreType store = LogicalDatastoreType.OPERATIONAL;
332 vpc = DataStoreUtils.read(rtx, store, path).orNull();
335 // Check to see if the path policy is present.
336 readVtnPathPolicy(rtx, id);
343 * Create an RPC input builder used to update existing path policy
346 * @param id The path policy ID.
347 * @return Builder instance for RPC input.
348 * @throws RpcException
349 * The given path policy ID is invalid.
351 public static PathPolicyConfigBuilder.Rpc createRpcInput(int id)
352 throws RpcException {
353 PathPolicyConfigBuilder.Rpc builder =
354 new PathPolicyConfigBuilder.Rpc();
355 Integer pid = Integer.valueOf(id);
359 } catch (VTNException e) {
360 // Invalid path policy ID is specified.
361 throw getNotFoundException(id, e);
364 builder.getBuilder().setOperation(VtnUpdateOperationType.ADD).
365 setPresent(Boolean.TRUE);