blob: 124d3fb4f9d4e3b175d21fd0e843447f86fee97b [file] [log] [blame]
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -08003 *
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 */
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080016package org.onosproject.net.intent.impl.phase;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080017
Brian O'Connorf0c5a052015-04-27 00:34:53 -070018import org.onosproject.net.intent.Intent;
Brian O'Connor0e271dc2015-02-04 18:20:25 -080019import org.onosproject.net.intent.IntentData;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080020import org.onosproject.net.intent.IntentException;
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -080021import org.onosproject.net.intent.impl.IntentProcessor;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Brian O'Connorf0c5a052015-04-27 00:34:53 -070025import java.util.List;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080026import java.util.Optional;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
Sho SHIMIZUd3dcaa12015-02-13 13:48:51 -080030/**
Brian O'Connorf0c5a052015-04-27 00:34:53 -070031 * Represents a phase where an intent is being compiled or recompiled.
Sho SHIMIZUd3dcaa12015-02-13 13:48:51 -080032 */
Brian O'Connorf0c5a052015-04-27 00:34:53 -070033class Compiling implements IntentProcessPhase {
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080034
35 private static final Logger log = LoggerFactory.getLogger(Compiling.class);
36
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -080037 private final IntentProcessor processor;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080038 private final IntentData data;
Brian O'Connorf0c5a052015-04-27 00:34:53 -070039 private final Optional<IntentData> stored;
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080040
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041 /**
Brian O'Connorf0c5a052015-04-27 00:34:53 -070042 * Creates a intent recompiling phase.
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080043 *
Brian O'Connorf0c5a052015-04-27 00:34:53 -070044 * @param processor intent processor that does work for recompiling
45 * @param data intent data containing an intent to be recompiled
46 * @param stored intent data stored in the store
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047 */
Brian O'Connorf0c5a052015-04-27 00:34:53 -070048 Compiling(IntentProcessor processor, IntentData data, Optional<IntentData> stored) {
Sho SHIMIZUb0a47d42015-02-19 13:26:30 -080049 this.processor = checkNotNull(processor);
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050 this.data = checkNotNull(data);
Brian O'Connorf0c5a052015-04-27 00:34:53 -070051 this.stored = checkNotNull(stored);
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080052 }
53
54 @Override
Sho SHIMIZU36a8a6e2015-02-13 15:38:45 -080055 public Optional<IntentProcessPhase> execute() {
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080056 try {
Brian O'Connorf0c5a052015-04-27 00:34:53 -070057 List<Intent> compiled = processor.compile(data.intent(),
58 //TODO consider passing an optional here in the future
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080059 stored.map(IntentData::installables).orElse(null));
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -070060 return Optional.of(new Installing(processor, IntentData.compiled(data, compiled), stored));
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080061 } catch (IntentException e) {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -070062 log.warn("Unable to compile intent {} due to:", data.intent(), e);
Ray Milkey80ae97c2016-02-19 19:04:13 -080063 if (stored.filter(x -> !x.installables().isEmpty()).isPresent()) {
Brian O'Connorf0c5a052015-04-27 00:34:53 -070064 // removing orphaned flows and deallocating resources
Sho SHIMIZU2f26fb22016-01-19 14:04:46 -080065 return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables())));
Brian O'Connorf0c5a052015-04-27 00:34:53 -070066 } else {
67 return Optional.of(new Failed(data));
68 }
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080069 }
70 }
Sho SHIMIZU5cb438e2015-02-04 13:46:00 -080071}