Rework IOVisor model and validate IovisorModuleInstance on Endpoint created events
[groupbasedpolicy.git] / renderers / iovisor / src / main / java / org / opendaylight / groupbasedpolicy / renderer / iovisor / utils / IovisorModuleUtils.java
1 /*
2  * Copyright (c) 2015 Inocybe Technologies and others.  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.groupbasedpolicy.renderer.iovisor.utils;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModule;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleInstances;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance;
18
19 import com.google.common.base.Optional;
20
21 public class IovisorModuleUtils {
22
23     private IovisorModuleUtils() {
24     }
25
26     /**
27      * Make sure the specified IOvisor module Uri exists in the datastore.
28      * @param dataBroker An instance of the {@link DataBroker}
29      * @param iovisorModuleUri The Uri of the {@link IovisorModule} we want to validate
30      * @return <code>true</code> if validated, else, <code>false</code>
31      */
32     public static boolean validateIovisorModuleInstance(DataBroker dataBroker, Uri iovisorModuleUri) {
33         Optional<IovisorModuleInstances> res = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
34                                                                          IovisorIidFactory.iovisorModuleInstancesIid(),
35                                                                          dataBroker.newReadOnlyTransaction());
36         if (res.isPresent()) {
37             for (IovisorModuleInstance instance : res.get().getIovisorModuleInstance()) {
38                 if (instance.getUri().equals(iovisorModuleUri)) {
39                     return true;
40                 }
41             }
42         }
43         return false;
44     }
45 }