Bug 5077: Codes break the security rules
[nemo.git] / nemo-impl / src / main / java / org / opendaylight / nemo / intent / condition / ConditionMonitor.java
1 /*
2  * Copyright (c) 2015 Huawei, Inc. 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.nemo.intent.condition;
10
11 import org.opendaylight.nemo.intent.IntentResolver;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.instance.ConditionSegment;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import java.util.List;
17 import java.util.TimerTask;
18
19 /**
20  * Created by hj on 11/19/15.
21  * TimerTask for one condition.
22  */
23 public class ConditionMonitor extends TimerTask {
24     private static Logger log = LoggerFactory.getLogger(ConditionMonitor.class);
25     private List<ConditionSegment> conditionSegments;
26     //Handle operation when condition state changed
27     private IntentResolver intentResolver;
28     private boolean preState;
29     private UserId userId;
30
31     public ConditionMonitor(IntentResolver intentResolver, UserId userId, List<ConditionSegment> conditionSegments) {
32         this.conditionSegments = conditionSegments;
33         this.intentResolver = intentResolver;
34         this.userId = userId;
35         preState = ConditionDeterminer.isConditionMet(conditionSegments);
36     }
37
38     @Override
39     public void run() {
40         boolean currentState = ConditionDeterminer.isConditionMet(conditionSegments);
41         if (currentState != preState) {
42             try {
43                 intentResolver.resolveIntent(userId);
44             } catch (Exception e) {
45                 //TODO Auto-generated catch block
46                 log.error("Exception:",e);
47             }
48             preState = currentState;
49         }
50     }
51 }