2 * Copyright (c) 2017 Intel Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.hostconfig.utils;
11 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class NeutronHostconfigUtils {
25 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigUtils.class);
26 private final DataBroker dataBroker;
34 public NeutronHostconfigUtils(final DataBroker dataBroker) {
35 this.dataBroker = dataBroker;
38 public void updateMdsal(Hostconfig hostConfig, Action action) {
39 InstanceIdentifier<Hostconfig> hostConfigId;
40 if (hostConfig == null) {
47 final WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
48 hostConfigId = createInstanceIdentifier(hostConfig);
49 writeTx.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig, true);
50 writeTx.submit().get();
51 LOG.trace("Hostconfig updated for node {}", hostConfig.getHostId());
54 final WriteTransaction delTx = dataBroker.newWriteOnlyTransaction();
55 hostConfigId = createInstanceIdentifier(hostConfig);
56 delTx.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
57 LOG.trace("Hostconfig deleted for node {}", hostConfig.getHostId());
63 } catch (InterruptedException | ExecutionException e) {
64 LOG.warn("Hostconfig transaction commit failed to DS.", e);
68 public Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
69 HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
70 hostconfigBuilder.setHostId(hostId);
71 hostconfigBuilder.setHostType(hostType);
72 hostconfigBuilder.setConfig(hostConfig);
73 return hostconfigBuilder.build();
76 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
77 return InstanceIdentifier.create(Neutron.class).child(Hostconfigs.class)
78 .child(Hostconfig.class, hostconfig.getKey());