summaryrefslogtreecommitdiffstats
path: root/vbd/gui/module/src/main/resources/vpp/services/inventory.service.js
blob: 6181ce6786324bc4a25568bed4b7b3759e5cfc47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
define(['app/vpp/vpp.module', 'next'], function(vpp) {
    vpp.register.factory('VppService', function(VPPRestangular, VPPRestangularXml, VppInterfaceService, $q) {
        var s = {};

        var Vpp = function(name, ipAddress, port, username, password, status) {
            this.name = name || null;
            this.ipAddress = ipAddress || null;
            this.port = port || null;
            this.username = username || null;
            this.password = password || null;
            this.status = status || null;
            this.interfaces = [];
        };

        s.createObj = function(name, ipAddress, port, username, password, status) {
            return new Vpp(name, ipAddress, port, username, password, status);
        };

        s.getVppList = function(successCallback, errorCallback) {
            var vppList = [];
            var promiseList = [];
            var restObj = VPPRestangular.one('restconf').one('operational').one('network-topology:network-topology').one('topology').one('topology-netconf');

            restObj.get().then(function(data) {
                data.topology[0].node.forEach(function(n) {
                    if(n['node-id'] !== 'controller-config') {
                        //create new object
                        var vppObj = s.createObj(n['node-id'], n['netconf-node-topology:host'], n['netconf-node-topology:port'], null, null, n['netconf-node-topology:connection-status']);
                        // register a promise
                        if (vppObj.status === 'connected') {
                            var promise = VppInterfaceService.getInterfaceListByVppName(n['node-id'],
                                function (interfaceList) {
                                    vppObj.interfaces = interfaceList;
                                },
                                function (error) { }
                            );
                            // add promise to array
                            promiseList.push(promise);
                            // when promise is resolved, push vpp into vppList
                            promise.then(
                                function () {
                                    vppList.push(vppObj);
                                },
                                function() {
                                    console.warn('error');
                                }
                            )
                        }
                        else {
                            vppList.push(vppObj);
                        }

                    }
                });

                // when all promises are resolved, call success callback
                $q.all(promiseList).then(function () {
                    successCallback(vppList);
                });
            }, function(res) {
                errorCallback(res);
            });
        };

        s.deleteVpp = function(vpp, finishedSuccessfullyCallback) {
            console.log(vpp);
            var restObj = VPPRestangular.one('restconf').one('config').one('network-topology:network-topology').one('topology').one('topology-netconf').one('node').one('controller-config').one('yang-ext:mount').one('config:modules').one('module').one('odl-sal-netconf-connector-cfg:sal-netconf-connector').one(vpp.name);

            restObj.remove().then(function() {
                finishedSuccessfullyCallback(true);
            }, function(res) {
                finishedSuccessfullyCallback(false);
            });
        };

        s.mountVpp = function(name,ip,port,un,pw,finishedSuccessfullyCallback) {

            var postData =  '\
            <module xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">\
            <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">prefix:sal-netconf-connector</type>\
            <name>'+name+'</name>\
            <address xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">'+ip+'</address>\
            <port xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">'+port+'</port>\
                <username xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">'+un+'</username>\
                <password xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">'+pw+'</password>\
                <tcp-only xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">false</tcp-only>\
                <event-executor xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">\
                <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:netty">prefix:netty-event-executor</type>\
            <name>global-event-executor</name>\
            </event-executor>\
            <binding-registry xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">\
                <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">prefix:binding-broker-osgi-registry</type>\
            <name>binding-osgi-broker</name>\
            </binding-registry>\
            <dom-registry xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">\
                <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom">prefix:dom-broker-osgi-registry</type>\
            <name>dom-broker</name>\
            </dom-registry>\
            <client-dispatcher xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">\
                <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:netconf">prefix:netconf-client-dispatcher</type>\
            <name>global-netconf-dispatcher</name>\
            </client-dispatcher>\
            <processing-executor xmlns="urn:opendaylight:params:xml:ns:yang:controller:md:sal:connector:netconf">\
                <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:threadpool">prefix:threadpool</type>\
            <name>global-netconf-processing-executor</name>\
            </processing-executor>\
            </module>';

            var restObj = VPPRestangularXml.one('restconf').one('config').one('opendaylight-inventory:nodes').one('node').one('controller-config').one('yang-ext:mount');

            restObj.post('config:modules', postData).then(function() {
                finishedSuccessfullyCallback(true);
            }, function(res) {
                finishedSuccessfullyCallback(false);
            });
        };

        return s;
    });

    vpp.register.factory('VppInterfaceService', function(VPPRestangular, $q) {
        var s = {};

        s.getInterfaceListByVppName = function(vppName, successCallback, errorCallback) {
            var interfaceList = [];
            var restObj = VPPRestangular.one('restconf').one('operational').one('network-topology:network-topology').one('topology').one('topology-netconf').one('node').one(vppName).one('yang-ext:mount').one('ietf-interfaces:interfaces-state');

            return restObj.get().then(
                function(data) {
                    if (data['interfaces-state'].interface) {
                        interfaceList = data['interfaces-state'].interface.filter(function(i) {
                            if (i.name != 'local0') {
                                return i;
                            }
                        });
                    }
                    successCallback(interfaceList);
                },
                function(res) {
                    errorCallback(res);
                }
            );
        };

        return s;
    });
});