Merge "Bug 7848: Allow neutron port create with security disabled."
[neutron.git] / neutron-hostconfig / utils / src / main / java / org / opendaylight / neutron / hostconfig / utils / NeutronHostconfigUtils.java
1 /*
2  * Copyright (c) 2017 Intel Corporation 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.neutron.hostconfig.utils;
10
11 import java.text.ParseException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 import java.util.concurrent.ExecutionException;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NeutronHostconfigUtils {
28     private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigUtils.class);
29     private final DataBroker dataBroker;
30
31     public enum Action {
32         ADD,
33         UPDATE,
34         DELETE
35     }
36
37     public NeutronHostconfigUtils(final DataBroker dataBroker) {
38         this.dataBroker = dataBroker;
39     }
40
41     public void updateMdsal(Hostconfig hostConfig, Action action) {
42         InstanceIdentifier<Hostconfig> hostConfigId;
43         if (hostConfig == null) {
44             return;
45         }
46         try {
47             switch (action) {
48                 case ADD:
49                 case UPDATE:
50                     final WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
51                     hostConfigId = createInstanceIdentifier(hostConfig);
52                     writeTx.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig, true);
53                     writeTx.submit().get();
54                     LOG.trace("Hostconfig updated for node {}", hostConfig.getHostId());
55                     break;
56                 case DELETE:
57                     final WriteTransaction delTx = dataBroker.newWriteOnlyTransaction();
58                     hostConfigId = createInstanceIdentifier(hostConfig);
59                     delTx.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
60                     LOG.trace("Hostconfig deleted for node {}", hostConfig.getHostId());
61                     delTx.submit().get();
62                     break;
63                 default:
64                     break;
65             }
66         } catch (InterruptedException | ExecutionException e) {
67             LOG.warn("Hostconfig transaction commit failed to DS.", e);
68         }
69     }
70
71     public Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
72         HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
73         hostconfigBuilder.setHostId(hostId);
74         hostconfigBuilder.setHostType(hostType);
75         hostconfigBuilder.setConfig(hostConfig);
76         return hostconfigBuilder.build();
77     }
78
79     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
80         return InstanceIdentifier.create(Neutron.class).child(Hostconfigs.class)
81                 .child(Hostconfig.class, hostconfig.getKey());
82     }
83
84     /**
85      * Used for parsing model revisions.
86      */
87     public static Date parseDate(final String strDate) {
88         final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
89         try {
90             return formatter.parse(strDate);
91         } catch (final ParseException e) {
92             throw new IllegalArgumentException("Date " + strDate + " not valid.", e);
93         }
94     }
95 }