aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/faultmanagement/fmmgr/src/main/java/org/onosproject/faultmanagement/impl/AlarmsManager.java
blob: 74fe7072f7aaafaa1090f9db11251e7394129b4f (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.faultmanagement.impl;

import static com.google.common.base.Strings.isNullOrEmpty;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
import static org.onlab.util.Tools.nullIsNotFound;

import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent;
import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
import org.onosproject.incubator.net.faultmanagement.alarm.AlarmListener;
import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
import org.apache.felix.scr.annotations.Service;
import static org.onlab.util.Tools.get;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.IdGenerator;
import org.onosproject.core.CoreService;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.util.ItemNotFoundException;
import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
import org.osgi.service.component.ComponentContext;

/**
 * Implementation of the Alarm service.
 */
@Component(immediate = true)
@Service
public class AlarmsManager implements AlarmService {

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
    protected CoreService coreService;
    private final Logger log = getLogger(getClass());
    private ApplicationId appId;
    private IdGenerator idGenerator;


    @Property(name = "fmDevices", value = "127.0.0.1", label = "Instance-specific configurations")
    private String devConfigs;

    private final Map<AlarmId, Alarm> alarms = new ConcurrentHashMap<>();


    private final AtomicLong alarmIdGenerator = new AtomicLong(0);

    @Override
    public Alarm update(final Alarm replacement) {

        final Alarm found = alarms.get(replacement.id());
        if (found == null) {
            throw new ItemNotFoundException("Alarm with id " + replacement.id() + " found");
        }
        final Alarm updated = new DefaultAlarm.Builder(found).
                withAcknowledged(replacement.acknowledged()).
                withAssignedUser(replacement.assignedUser()).build();
        alarms.put(replacement.id(), updated);
        return updated;
    }

    @Override
    public int getActiveAlarmCount(final DeviceId deviceId) {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }
    private static final String NOT_SUPPORTED_YET = "Not supported yet.";

    @Override
    public Alarm getAlarm(final AlarmId alarmId) {
        return nullIsNotFound(
                alarms.get(alarmId),
                "Alarm is not found");
    }

    @Override
    public Set<Alarm> getAlarms() {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }

    @Override
    public Set<Alarm> getActiveAlarms() {
        // Enpty set if no values
        return alarms.isEmpty() ? new HashSet<>() : new HashSet<>(alarms.values());

    }

    private static DefaultAlarm generateFake(final DeviceId deviceId, final AlarmId alarmId) {

        return new DefaultAlarm.Builder(
                alarmId, deviceId, "NE is not reachable", Alarm.SeverityLevel.MAJOR, System.currentTimeMillis()).
                withTimeUpdated(System.currentTimeMillis()).
                withServiceAffecting(true)
                .withAcknowledged(true).
                withManuallyClearable(true)
                .withAssignedUser("user1").build();
    }

    @Override
    public Set<Alarm> getAlarms(final Alarm.SeverityLevel severity) {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }

    @Override
    public Set<Alarm> getAlarms(final DeviceId deviceId) {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }

    @Override
    public Set<Alarm> getAlarms(final DeviceId deviceId, final AlarmEntityId source) {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }

    @Override
    public Set<Alarm> getAlarmsForLink(final ConnectPoint src, final ConnectPoint dst) {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }

    @Override
    public Set<Alarm> getAlarmsForFlow(final DeviceId deviceId, final long flowId) {
        //TODO
        throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
    }

    private void discoverAlarmsForDevice(final DeviceId deviceId) {
        final AlarmId alarmId = new AlarmId(alarmIdGenerator.incrementAndGet());

        // TODO In a new thread invoke SNMP Provider with DeviceId and device type and when done update our of alarms
        //
        alarms.put(alarmId, generateFake(deviceId, alarmId));

    }

    private class InternalAlarmListener implements AlarmListener {

        @Override
        public void event(final AlarmEvent event) {
            // TODO
            throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
        }
    }

    @Activate
    public void activate(final ComponentContext context) {
        log.info("Activate ...");
        appId = coreService.registerApplication("org.onos.faultmanagement.alarms");
        idGenerator = coreService.getIdGenerator("alarm-ids");
        log.info("Started with appId={} idGenerator={}", appId, idGenerator);

        final boolean result = modified(context);
        log.info("modified result = {}", result);

    }

    @Deactivate
    public void deactivate(final ComponentContext context) {
        log.info("Deactivate ...");
        //     cfgService.unregisterProperties(getClass(), false);

        log.info("Stopped");
    }

    @Modified
    public boolean modified(final ComponentContext context) {
        log.info("context={}", context);
        if (context == null) {
            log.info("No configuration file");
            return false;
        }
        final Dictionary<?, ?> properties = context.getProperties();
        final String ipaddresses = get(properties, "fmDevices");
        log.info("Settings: devConfigs={}", ipaddresses);
        if (!isNullOrEmpty(ipaddresses)) {
            discover(ipaddresses);

        }
        return true;
    }

    private void discover(final String ipaddresses) {
        for (String deviceEntry : ipaddresses.split(",")) {
            final DeviceId deviceId = DeviceId.deviceId(deviceEntry);
            if (deviceId != null) {
                log.info("Device {} needs to have its alarms refreshed!", deviceId);
                discoverAlarmsForDevice(deviceId);
            }
        }
    }

}