blob: 911139c06da0c0d0114e7f22020f3628e1b427cd [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.net.intent.impl.phase;
17
18import org.onosproject.net.intent.IntentData;
19import org.onosproject.net.intent.IntentException;
20import org.onosproject.net.intent.impl.IntentProcessor;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.util.Optional;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Represents a phase to replace an intent.
30 */
31final class Replacing implements IntentProcessPhase {
32
33 private static final Logger log = LoggerFactory.getLogger(Replacing.class);
34
35 private final IntentProcessor processor;
36 private final IntentData data;
37 private final IntentData stored;
38
39 /**
40 * Creates a replacing phase.
41 *
42 * @param processor intent processor that does work for replacing
43 * @param data intent data containing an intent to be replaced
44 * @param stored intent data stored in the store
45 */
46 Replacing(IntentProcessor processor, IntentData data, IntentData stored) {
47 this.processor = checkNotNull(processor);
48 this.data = checkNotNull(data);
49 this.stored = checkNotNull(stored);
50 }
51
52 @Override
53 public Optional<IntentProcessPhase> execute() {
54 try {
55 processor.uninstall(stored);
56 return Optional.of(new Installing(processor, data));
57 } catch (IntentException e) {
58 log.warn("Unable to generate a FlowRuleOperations from intent {} due to:", data.intent().id(), e);
59 return Optional.of(new ReplaceFailed(data));
60 }
61 }
62}